src/Entity/Song.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\SongRepository")
  11. */
  12. class Song
  13. {
  14. public const MEDIA_DIR = '/code/var/media';
  15. public const THUMBNAILS_DIR = '/code/var/media/thumbnails';
  16. public const MAX_SONG_DISPLAY = 10000;
  17. public static function path($name){
  18. return self::MEDIA_DIR."/".$name.".mp3";
  19. }
  20. /**
  21. * @ORM\Id()
  22. * @ORM\GeneratedValue()
  23. * @ORM\Column(type="integer")
  24. * @Groups("list")
  25. */
  26. private $id;
  27. /**
  28. * @ORM\Column(type="string", length=255)
  29. * @Groups("list")
  30. */
  31. private $name;
  32. /**
  33. * @ORM\Column(type="string", length=30)
  34. * @Groups("list")
  35. */
  36. private $playtime;
  37. /**
  38. * @ORM\Column(type="datetime")
  39. */
  40. private $created;
  41. /**
  42. * @ORM\Column(type="integer")
  43. * @Groups("list")
  44. */
  45. private $plays;
  46. /**
  47. * @ORM\Column(type="integer")
  48. * @Groups("list")
  49. */
  50. private $rate;
  51. /**
  52. * @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="songs")
  53. */
  54. private $tags;
  55. /**
  56. * @ORM\OneToMany(targetEntity="App\Entity\SongStation", mappedBy="song", orphanRemoval=true)
  57. */
  58. private $songStations;
  59. /**
  60. * @ORM\Column(type="boolean")
  61. */
  62. private $deleted;
  63. /**
  64. * @ORM\Column(type="string", length=255, nullable=true)
  65. */
  66. private $artistLink;
  67. /**
  68. * @ORM\Column(type="string", length=255, nullable=true)
  69. */
  70. private $thumbnailImage;
  71. /**
  72. * @ORM\Column(type="text", nullable=true)
  73. */
  74. private $trackListText;
  75. public function __construct()
  76. {
  77. $this->created = new DateTime();
  78. $this->tags = new ArrayCollection();
  79. $this->rate = 0;
  80. $this->songStations = new ArrayCollection();
  81. $this->deleted = false;
  82. }
  83. public function getId(): ?int
  84. {
  85. return $this->id;
  86. }
  87. public function getName(): ?string
  88. {
  89. return $this->name;
  90. }
  91. public function getNameWithExtension(): ?string
  92. {
  93. return $this->name.'.mp3';
  94. }
  95. public function getFullPath(): ?string
  96. {
  97. return self::MEDIA_DIR.'/'.$this->getNameWithExtension();
  98. }
  99. public function setName(string $name): self
  100. {
  101. $this->name = $name;
  102. return $this;
  103. }
  104. public function getPlaytime(): ?string
  105. {
  106. return $this->playtime;
  107. }
  108. public function getLength() : int
  109. {
  110. list($minutes, $seconds) = explode(':', $this->playtime);
  111. return ((int) $minutes * 60) + (int) $seconds;
  112. }
  113. public function setPlaytime(string $playtime): self
  114. {
  115. $this->playtime = $playtime;
  116. return $this;
  117. }
  118. /**
  119. * @return \DateTimeInterface|null
  120. */
  121. public function getCreated(): ?\DateTimeInterface
  122. {
  123. return $this->created;
  124. }
  125. /**
  126. * @Groups("list")
  127. * @SerializedName("added")
  128. * @return string|null
  129. */
  130. public function getCreatedFormatted(): ?string
  131. {
  132. return $this->created->format('d.m.y');
  133. }
  134. public function setCreated(\DateTimeInterface $created): self
  135. {
  136. $this->created = $created;
  137. return $this;
  138. }
  139. public function getPlays(): ?int
  140. {
  141. return $this->plays;
  142. }
  143. public function setPlays(int $plays): self
  144. {
  145. $this->plays = $plays;
  146. return $this;
  147. }
  148. public function getRate(): ?int
  149. {
  150. return $this->rate;
  151. }
  152. public function setRate(int $rate): self
  153. {
  154. $this->rate = $rate;
  155. return $this;
  156. }
  157. /**
  158. * @return Collection|Tag[]
  159. */
  160. public function getTags(): Collection
  161. {
  162. return $this->tags;
  163. }
  164. /**
  165. * @Groups("list")
  166. * @SerializedName("tags")
  167. * @return array
  168. */
  169. public function getTagJson(): array
  170. {
  171. $tags = $this->tags->getValues();
  172. usort($tags, function (Tag $tag){
  173. if ($tag->getParent() !== NULL){
  174. return -1;
  175. }
  176. });
  177. return array_map(function (Tag $tag){
  178. return [
  179. $tag->getId(),
  180. $tag->getName(),
  181. $tag->getParent() === NULL ? $tag->getColor() : NULL
  182. ]; }, $tags);
  183. }
  184. public function addTag(Tag $tag): self
  185. {
  186. if (!$this->tags->contains($tag)) {
  187. $this->tags[] = $tag;
  188. }
  189. return $this;
  190. }
  191. public function removeTag(Tag $tag): self
  192. {
  193. if ($this->tags->contains($tag)) {
  194. $this->tags->removeElement($tag);
  195. }
  196. return $this;
  197. }
  198. /**
  199. * @return Collection|SongStation[]
  200. */
  201. public function getSongStations(): Collection
  202. {
  203. return $this->songStations;
  204. }
  205. public function addSongStation(SongStation $songStation): self
  206. {
  207. if (!$this->songStations->contains($songStation)) {
  208. $this->songStations[] = $songStation;
  209. $songStation->setSong($this);
  210. }
  211. return $this;
  212. }
  213. public function removeSongStation(SongStation $songStation): self
  214. {
  215. if ($this->songStations->contains($songStation)) {
  216. $this->songStations->removeElement($songStation);
  217. // set the owning side to null (unless already changed)
  218. if ($songStation->getSong() === $this) {
  219. $songStation->setSong(null);
  220. }
  221. }
  222. return $this;
  223. }
  224. public function getDeleted(): ?bool
  225. {
  226. return $this->deleted;
  227. }
  228. public function setDeleted(bool $deleted): self
  229. {
  230. $this->deleted = $deleted;
  231. return $this;
  232. }
  233. public function getArtistLink(): ?string
  234. {
  235. return $this->artistLink;
  236. }
  237. public function setArtistLink(?string $artistLink): self
  238. {
  239. $this->artistLink = $artistLink;
  240. return $this;
  241. }
  242. public function getThumbnailImage(): ?string
  243. {
  244. return $this->thumbnailImage;
  245. }
  246. public function setThumbnailImage(?string $thumbnailImage): self
  247. {
  248. $this->thumbnailImage = $thumbnailImage;
  249. return $this;
  250. }
  251. public function getTrackListText(): ?string
  252. {
  253. return $this->trackListText;
  254. }
  255. public function setTrackListText(?string $trackListText): self
  256. {
  257. $this->trackListText = $trackListText;
  258. return $this;
  259. }
  260. }