src/Entity/Video.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Serializer\Annotation\Groups;
  8. use Symfony\Component\Serializer\Annotation\SerializedName;
  9. /**
  10. * @ORM\Entity(repositoryClass="App\Repository\VideoRepository")
  11. */
  12. class Video
  13. {
  14. public const VIDEO_DIR = '/code/var/videos';
  15. public const THUMBNAILS_DIR = '/code/var/videos/thumbnails';
  16. public const OVERLAYS_DIR = '/code/var/videos/overlays';
  17. public const WEB_VIDEO_URL = '/videos';
  18. public const WEB_THUMBNAIL_URL = '/videos/thumbnails';
  19. public const WEB_OVERLAY_URL = '/videos/overlays';
  20. public const MAX_VIDEO_DISPLAY = 10000;
  21. public static function path($name): string
  22. {
  23. if (substr(strtolower($name), -4) === '.mp4') {
  24. return self::VIDEO_DIR.'/'.$name;
  25. }
  26. return self::VIDEO_DIR.'/'.$name.'.mp4';
  27. }
  28. /**
  29. * @ORM\Id()
  30. * @ORM\GeneratedValue()
  31. * @ORM\Column(type="integer")
  32. * @Groups("list")
  33. */
  34. private $id;
  35. /**
  36. * @ORM\Column(type="string", length=255)
  37. * @Groups("list")
  38. */
  39. private $name;
  40. /**
  41. * @ORM\Column(type="string", length=255)
  42. * @Groups("list")
  43. */
  44. private $filename;
  45. /**
  46. * @ORM\Column(type="string", length=30)
  47. * @Groups("list")
  48. */
  49. private $duration;
  50. /**
  51. * @ORM\Column(type="datetime")
  52. */
  53. private $created;
  54. /**
  55. * @ORM\Column(type="integer")
  56. * @Groups("list")
  57. */
  58. private $plays;
  59. /**
  60. * @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="videos")
  61. */
  62. private $tags;
  63. /**
  64. * @ORM\Column(type="boolean")
  65. */
  66. private $deleted;
  67. /**
  68. * @ORM\Column(type="string", length=255, nullable=true)
  69. * @Groups("list")
  70. */
  71. private $thumbnailImage;
  72. /**
  73. * @ORM\Column(type="string", length=255, nullable=true)
  74. * @Groups("list")
  75. */
  76. private $overlayImage;
  77. /**
  78. * @ORM\Column(type="string", length=255, nullable=true)
  79. * @Groups("list")
  80. */
  81. private $linkUrl;
  82. /**
  83. * @ORM\Column(type="text", nullable=true)
  84. */
  85. private $description;
  86. public function __construct()
  87. {
  88. $this->created = new DateTime();
  89. $this->tags = new ArrayCollection();
  90. $this->plays = 0;
  91. $this->deleted = false;
  92. }
  93. public function getId(): ?int
  94. {
  95. return $this->id;
  96. }
  97. public function getName(): ?string
  98. {
  99. return $this->name;
  100. }
  101. public function setName(string $name): self
  102. {
  103. $this->name = $name;
  104. return $this;
  105. }
  106. public function getFilename(): ?string
  107. {
  108. return $this->filename;
  109. }
  110. public function getFilenameWithExtension(): ?string
  111. {
  112. return $this->filename.'.mp4';
  113. }
  114. public function getFullPath(): ?string
  115. {
  116. return self::VIDEO_DIR.'/'.$this->getFilenameWithExtension();
  117. }
  118. public function setFilename(string $filename): self
  119. {
  120. $this->filename = $filename;
  121. return $this;
  122. }
  123. public function getDuration(): ?string
  124. {
  125. return $this->duration;
  126. }
  127. public function getDurationSeconds() : int
  128. {
  129. list($minutes, $seconds) = explode(':', $this->duration);
  130. return ((int) $minutes * 60) + (int) $seconds;
  131. }
  132. public function setDuration(string $duration): self
  133. {
  134. $this->duration = $duration;
  135. return $this;
  136. }
  137. /**
  138. * @return \DateTimeInterface|null
  139. */
  140. public function getCreated(): ?\DateTimeInterface
  141. {
  142. return $this->created;
  143. }
  144. /**
  145. * @Groups("list")
  146. * @SerializedName("added")
  147. * @return string|null
  148. */
  149. public function getCreatedFormatted(): ?string
  150. {
  151. return $this->created->format('d.m.y');
  152. }
  153. public function setCreated(\DateTimeInterface $created): self
  154. {
  155. $this->created = $created;
  156. return $this;
  157. }
  158. public function getPlays(): ?int
  159. {
  160. return $this->plays;
  161. }
  162. public function setPlays(int $plays): self
  163. {
  164. $this->plays = $plays;
  165. return $this;
  166. }
  167. /**
  168. * @return Collection|Tag[]
  169. */
  170. public function getTags(): Collection
  171. {
  172. return $this->tags;
  173. }
  174. /**
  175. * @Groups("list")
  176. * @SerializedName("tags")
  177. * @return array
  178. */
  179. public function getTagJson(): array
  180. {
  181. $tags = $this->tags->getValues();
  182. usort($tags, function (Tag $tag){
  183. if ($tag->getParent() !== NULL){
  184. return -1;
  185. }
  186. });
  187. return array_map(function (Tag $tag){
  188. return [
  189. $tag->getId(),
  190. $tag->getName(),
  191. $tag->getParent() === NULL ? $tag->getColor() : NULL
  192. ]; }, $tags);
  193. }
  194. public function addTag(Tag $tag): self
  195. {
  196. if (!$this->tags->contains($tag)) {
  197. $this->tags[] = $tag;
  198. }
  199. return $this;
  200. }
  201. public function removeTag(Tag $tag): self
  202. {
  203. if ($this->tags->contains($tag)) {
  204. $this->tags->removeElement($tag);
  205. }
  206. return $this;
  207. }
  208. public function getDeleted(): ?bool
  209. {
  210. return $this->deleted;
  211. }
  212. public function setDeleted(bool $deleted): self
  213. {
  214. $this->deleted = $deleted;
  215. return $this;
  216. }
  217. public function getThumbnailImage(): ?string
  218. {
  219. return $this->thumbnailImage;
  220. }
  221. public function getThumbnailPath(): ?string
  222. {
  223. if ($this->thumbnailImage) {
  224. return self::THUMBNAILS_DIR.'/'.$this->thumbnailImage;
  225. }
  226. return null;
  227. }
  228. public function setThumbnailImage(?string $thumbnailImage): self
  229. {
  230. $this->thumbnailImage = $thumbnailImage;
  231. return $this;
  232. }
  233. public function getOverlayImage(): ?string
  234. {
  235. return $this->overlayImage;
  236. }
  237. public function getOverlayPath(): ?string
  238. {
  239. if ($this->overlayImage) {
  240. return self::OVERLAYS_DIR.'/'.$this->overlayImage;
  241. }
  242. return null;
  243. }
  244. public function setOverlayImage(?string $overlayImage): self
  245. {
  246. $this->overlayImage = $overlayImage;
  247. return $this;
  248. }
  249. public function getLinkUrl(): ?string
  250. {
  251. return $this->linkUrl;
  252. }
  253. public function setLinkUrl(?string $linkUrl): self
  254. {
  255. $this->linkUrl = $linkUrl;
  256. return $this;
  257. }
  258. public function getDescription(): ?string
  259. {
  260. return $this->description;
  261. }
  262. /**
  263. * @Groups("list")
  264. * @SerializedName("descriptionShort")
  265. * @return string|null
  266. */
  267. public function getDescriptionShort(): ?string
  268. {
  269. if (!$this->description) {
  270. return null;
  271. }
  272. return strlen($this->description) > 100 ? substr($this->description, 0, 100) . '...' : $this->description;
  273. }
  274. public function setDescription(?string $description): self
  275. {
  276. $this->description = $description;
  277. return $this;
  278. }
  279. }