src/Entity/Playlist.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiSubresource;
  6. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  7. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use App\Repository\PlaylistRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\Common\Collections\Criteria;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. /**
  17.  * @ORM\Entity(repositoryClass=PlaylistRepository::class)
  18.  * @ApiResource(
  19.  *     normalizationContext={"playlist:read"},
  20.  *     denormalizationContext={"playlist:write"},
  21.  *     collectionOperations={
  22.  *          "get"={
  23.  *              "security"="is_granted('ROLE_USER')"
  24.  *          },
  25.  *          "post"={
  26.  *              "security"="is_granted('ROLE_USER')"
  27.  *          }
  28.  *     },
  29.  *     itemOperations={
  30.  *          "get"={
  31.  *              "security"="is_granted('ROLE_USER')"
  32.  *          },
  33.  *          "patch"={
  34.  *              "security"="is_granted('ROLE_ADMIN') or (is_granted('ROLE_USER') and object.getUser() == user)"
  35.  *          },
  36.  *          "delete"={
  37.  *              "security"="is_granted('ROLE_ADMIN') or (is_granted('ROLE_USER') and object.getUser() == user)"
  38.  *          },
  39.  *          "user_patch"={
  40.  *              "method"="PATCH",
  41.  *              "security"="is_granted('ROLE_ADMIN') or (is_granted('ROLE_USER') and object.getUser() == user)",
  42.  *              "controller"="App\Controller\Api\PlaylistController::userUpdate",
  43.  *              "path"="/users/{user}/playlists/{id}",
  44.  *              "openapi_context"={"summary"="Update playlist of a User"}
  45.  *          },
  46.  *          "user_remove"={
  47.  *              "method"="DELETE",
  48.  *              "security"="is_granted('ROLE_USER') and object.getUser() == user",
  49.  *              "controller"="App\Controller\Api\PlaylistController::userRemove",
  50.  *              "path"="/users/{user}/playlists/{id}",
  51.  *              "openapi_context"={"summary"="Delete playlist of a User"}
  52.  *          }
  53.  *     }
  54.  * )
  55.  * @ApiFilter(SearchFilter::class, properties={"name": "partial", "user.id"})
  56.  * @ApiFilter(PropertyFilter::class, 
  57.  *      arguments={
  58.  *          "parameterName"="fields", 
  59.  *          "overrideDefaultProperties"=true
  60.  *     }
  61.  * )
  62.  * @ApiFilter(GroupFilter::class, arguments={
  63.  *      "parameterName": "groups", 
  64.  *      "overrideDefaultGroups": true
  65.  * })
  66.  * @ORM\HasLifecycleCallbacks()
  67.  */
  68. class Playlist
  69. {
  70.     /**
  71.      * @ORM\Id
  72.      * @ORM\GeneratedValue
  73.      * @ORM\Column(type="integer")
  74.      * @Groups({"channel:read", "playlist:read", "playlist:read:id", "day:read", "objective:read", "video:read"})
  75.      */
  76.     private $id;
  77.     /**
  78.      * @ORM\ManyToMany(targetEntity=Video::class, inversedBy="playlists")
  79.      * @Groups({"single_channel:read", "playlist:read", "playlist:read:videos", "playlist:write", "day:read", "homepage:read", "program_single_list", "objective:read", "objective:write", "objective_form_complete", "program:read", "program:write", "day:write", "channel:read"})
  80.      * @ApiSubresource(maxDepth=1)
  81.      */
  82.     private $videos;
  83.     /**
  84.      * @ORM\Column(type="datetime")
  85.      * @Gedmo\Timestampable(on="create")
  86.      * @Groups({"playlist:read", "playlist:read:createdAt"})
  87.      */
  88.     private $createdAt;
  89.     /**
  90.      * @ORM\Column(type="datetime", nullable=true)
  91.      * @Gedmo\Timestampable(on="update")
  92.      * @Groups({"playlist:read", "playlist:read:updatedAt"})
  93.      */
  94.     private $updatedAt;
  95.     
  96.     /**
  97.      * @ORM\OneToMany(targetEntity=VideoLastValidate::class, mappedBy="playlist", cascade={"persist", "refresh", "remove"}, orphanRemoval=true)
  98.      */
  99.     private $videoLastValidates;
  100.     
  101.     /**
  102.      * @ORM\OneToMany(targetEntity=VideoLastSeen::class, mappedBy="playlist", cascade={"persist", "refresh", "remove"}, orphanRemoval=true)
  103.      */
  104.     private $videoLastSeens;
  105.     
  106.     /**
  107.      * @ORM\OneToMany(targetEntity=Channel::class, mappedBy="playlist", cascade={"persist", "refresh", "remove"}, orphanRemoval=true)
  108.      */
  109.     private $channels;
  110.     /**
  111.      * @var int
  112.      * @Groups({"single_channel:read", "playlist:read", "day:read", "homepage:read"})
  113.      */
  114.     public $activeVideoCount 0;
  115.     /**
  116.      * @var string|null
  117.      * @ORM\Column(type="string", length=255, nullable=true)
  118.      * @Groups({"video:read", "video_form_complete", "objective_form_complete", "playlist:read", "playlist:read:name", "playlist:write"})
  119.      */
  120.     private $name null;
  121.     /**
  122.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="playlists")
  123.      * @Groups({"channel:read", "channel:read:user", "channel:write", "category:read", "search_engine", "playlist:read", "playlist:read:user"})
  124.      */
  125.     private $user;
  126.     public function __construct()
  127.     {
  128.         $this->createdAt = new \DateTime();
  129.         $this->videos = new ArrayCollection();
  130.         $this->videoLastValidates = new ArrayCollection();
  131.         $this->videoLastSeens = new ArrayCollection();
  132.         $this->channels = new ArrayCollection();
  133.     }
  134.     public function __clone()
  135.     {
  136.         $this->id null;
  137.         $this->videoLastValidates = new ArrayCollection();
  138.         $this->videoLastSeens = new ArrayCollection();
  139.         $this->channels = new ArrayCollection();
  140.         $this->createdAt = new \DateTime();
  141.     }
  142.     public function getId(): ?int
  143.     {
  144.         return $this->id;
  145.     }
  146.     public function setId(?int $id): self
  147.     {
  148.         $this->id $id;
  149.         return $this;
  150.     }
  151.     /**
  152.      * @return Collection|Video[]
  153.      * @Groups({"objective_form_complete"})
  154.      */
  155.     public function getVideos(): Collection
  156.     {
  157.         return $this->videos;
  158.     }
  159.     public function addVideo(Video $video): self
  160.     {
  161.         if (!$this->videos->contains($video)) {
  162.             $this->videos[] = $video;
  163.         }
  164.         return $this;
  165.     }
  166.     public function removeVideo(Video $video): self
  167.     {
  168.         $this->videos->removeElement($video);
  169.         return $this;
  170.     }
  171.     /**
  172.      * @Groups({"playlist:write"})
  173.      */
  174.     public function setAddVideo(Video $video): self
  175.     {
  176.         $this->setUpdatedAt(new \DateTime('now'));
  177.         return $this->addVideo($video);
  178.     }
  179.     /**
  180.      * @Groups({"playlist:write"})
  181.      */
  182.     public function setRemoveVideo(Video $video): self
  183.     {
  184.         $this->setUpdatedAt(new \DateTime('now'));
  185.         
  186.         return $this->removeVideo($video);
  187.     }
  188.     public function getCreatedAt(): ?\DateTimeInterface
  189.     {
  190.         return $this->createdAt;
  191.     }
  192.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  193.     {
  194.         $this->createdAt $createdAt;
  195.         return $this;
  196.     }
  197.     public function getUpdatedAt(): ?\DateTimeInterface
  198.     {
  199.         return $this->updatedAt;
  200.     }
  201.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  202.     {
  203.         $this->updatedAt $updatedAt;
  204.         return $this;
  205.     }
  206.     /**
  207.      * @param ArrayCollection|array $videos
  208.      * @return Playlist
  209.      */
  210.     public function setVideos($videos): Playlist
  211.     {
  212.         if (is_array($videos)){
  213.             $videos = new ArrayCollection($videos);
  214.         }
  215.         $this->videos $videos;
  216.         return $this;
  217.     }
  218.     /**
  219.      * Slices the videos list from offset to a limit
  220.      * @param int $limit Limit to which slice the videos
  221.      * @param int $offset Offset to start the slice
  222.      */
  223.     public function limitTo(int $limit 10int $offset 0)
  224.     {
  225.         $this->videos = new ArrayCollection($this->videos->slice($offset$limit));
  226.     }
  227.     /**
  228.      * @ORM\PostLoad()
  229.      */
  230.     public function updateActiveCount()
  231.     {
  232.         $criteria Criteria::create()->andWhere(Criteria::expr()->eq('active'true));
  233.         $this->activeVideoCount $this->videos->matching($criteria)->count();
  234.     }
  235.     /**
  236.      * @return string|null
  237.      */
  238.     public function getName(): ?string
  239.     {      
  240.         if(empty($this->name)) {
  241.             if($this->channels->count() > 0) {
  242.                 $this->name $this->channels->first()->getName();
  243.             }
  244.         }
  245.         return $this->name;
  246.     }
  247.     /**
  248.      * @param string|null $name
  249.      * @return Playlist
  250.      */
  251.     public function setName(?string $name): Playlist
  252.     {
  253.         $this->name $name;
  254.         return $this;
  255.     }
  256.     public static function buildFromArray(array $data): Playlist
  257.     {
  258.         $pl = new Playlist();
  259.         $pl->id $data['id'];
  260.         $pl->createdAt $data['createdAt'];
  261.         $pl->name $data['name'];
  262.         return $pl;
  263.     }
  264.     /**
  265.      * @Groups({"channel:read", "day:read", "program:read", "video:read"})
  266.      */
  267.     public function getStringifyTags()
  268.     {
  269.         $stringifyTags = [];
  270.         if(!empty($this->videos)) {
  271.             foreach($this->videos as $video) {
  272.                 if($video->getActive() == && !empty($video->getTvTags())) {
  273.                     foreach($video->getTvTags() as $tvTag) {
  274.                         if(!in_array($tvTag->__toString(), $stringifyTags) && $tvTag->getActive() == 1) {
  275.                             $stringifyTags[] = $tvTag->__toString();
  276.                         }
  277.                     }
  278.                 }
  279.             }
  280.             return implode(""array_unique($stringifyTagsSORT_STRING));
  281.         }
  282.         
  283.         return null;
  284.     }
  285.     /**
  286.      * @return Collection|VideoLastValidate[]
  287.      */
  288.     public function getVideoLastValidates(): Collection
  289.     {
  290.         return $this->videoLastValidates;
  291.     }
  292.     public function addVideoLastValidate(VideoLastValidate $videoLastValidate): self
  293.     {
  294.         if (!$this->videoLastValidates->contains($videoLastValidate)) {
  295.             $this->videoLastValidates[] = $videoLastValidate;
  296.             $videoLastValidate->setPlaylist($this);
  297.         }
  298.         return $this;
  299.     }
  300.     public function removeVideoLastValidate(VideoLastValidate $videoLastValidate): self
  301.     {
  302.         if ($this->videoLastValidates->removeElement($videoLastValidate)) {
  303.             // set the owning side to null (unless already changed)
  304.             if ($videoLastValidate->getPlaylist() === $this) {
  305.                 $videoLastValidate->setPlaylist(null);
  306.             }
  307.         }
  308.         return $this;
  309.     }
  310.     /**
  311.      * @return Collection|VideoLastSeen[]
  312.      */
  313.     public function getVideoLastSeens(): Collection
  314.     {
  315.         return $this->videoLastSeens;
  316.     }
  317.     public function addVideoLastSeen(VideoLastSeen $videoLastSeen): self
  318.     {
  319.         if (!$this->videoLastSeens->contains($videoLastSeen)) {
  320.             $this->videoLastSeens[] = $videoLastSeen;
  321.             $videoLastSeen->setPlaylist($this);
  322.         }
  323.         return $this;
  324.     }
  325.     public function removeVideoLastSeen(VideoLastSeen $videoLastSeen): self
  326.     {
  327.         if ($this->videoLastSeens->removeElement($videoLastSeen)) {
  328.             // set the owning side to null (unless already changed)
  329.             if ($videoLastSeen->getPlaylist() === $this) {
  330.                 $videoLastSeen->setPlaylist(null);
  331.             }
  332.         }
  333.         return $this;
  334.     }
  335.     
  336.     /**
  337.      * @return Collection|Channel[]
  338.      */
  339.     public function getChannels(): Collection
  340.     {
  341.         return $this->channels;
  342.     }
  343.     public function addChannel(Channel $channel): self
  344.     {
  345.         if (!$this->channels->contains($channel)) {
  346.             $this->channels[] = $channel;
  347.             $channel->setPlaylist($this);
  348.         }
  349.         return $this;
  350.     }
  351.     public function removeChannel(Channel $channel): self
  352.     {
  353.         if ($this->channels->removeElement($channel)) {
  354.             // set the owning side to null (unless already changed)
  355.             if ($channel->getPlaylist() === $this) {
  356.                 $channel->setPlaylist(null);
  357.             }
  358.         }
  359.         return $this;
  360.     }
  361.     public function getUser(): ?User
  362.     {
  363.         return $this->user;
  364.     }
  365.     public function setUser(?User $user): self
  366.     {
  367.         $this->user $user;
  368.         return $this;
  369.     }
  370. }