src/Entity/Book.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Controller\Api\BookUnits;
  6. use App\Controller\Api\BookBonus;
  7. use App\Controller\Api\BookVideos;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\Common\Collections\Criteria;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. /**
  14.  * @ApiResource(
  15.  *     attributes={"normalization_context"={"groups"={"Get"}},"order"={"position": "ASC"}},
  16.  *     collectionOperations={"get"},
  17.  *     itemOperations={
  18.  *         "get",
  19.  *         "units"={
  20.  *             "method"="GET",
  21.  *             "path"="/books/{id}/units",
  22.  *             "controller"=BookUnits::class,
  23.  *         },
  24.  *         "bonus"={
  25.  *             "method"="GET",
  26.  *             "path"="/books/{id}/bonus",
  27.  *             "controller"=BookBonus::class,
  28.  *         },
  29.  *         "videos"={
  30.  *             "method"="GET",
  31.  *             "path"="/books/{id}/videos",
  32.  *             "controller"=BookVideos::class,
  33.  *         }
  34.  *     },
  35.  * )
  36.  *
  37.  * @ORM\Table(name="wow_book")
  38.  * @ORM\Entity(repositoryClass="App\Repository\BookRepository")
  39.  */
  40. class Book
  41. {
  42.     /**
  43.      * @Groups({"Get"})
  44.      *
  45.      * @ApiProperty(
  46.      *     attributes={
  47.      *         "swagger_context"={
  48.      *             "name"="BookId",
  49.      *             "type"="int",
  50.      *         }
  51.      *     }
  52.      * )
  53.      *
  54.      * @var int The identifier of a Book
  55.      *
  56.      * @ORM\Id()
  57.      * @ORM\GeneratedValue()
  58.      * @ORM\Column(type="integer")
  59.      */
  60.     private $id;
  61.     /**
  62.      * @Groups({"Get"})
  63.      *
  64.      * @ApiProperty(
  65.      *     attributes={
  66.      *         "swagger_context"={
  67.      *             "name"="BookId",
  68.      *             "type"="int",
  69.      *         }
  70.      *     }
  71.      * )
  72.      *
  73.      * @var string The Books title (for example: red)
  74.      *
  75.      * @ORM\Column(type="string", length=100, unique=true)
  76.      */
  77.     private $title;
  78.     /**
  79.      * @var int The Books position for sorting
  80.      *
  81.      * @ORM\Column(type="integer")
  82.      */
  83.     private $position;
  84.     /**
  85.      * @var Collection|Unit[]
  86.      *
  87.      * @ORM\OneToMany(targetEntity="App\Entity\Unit", mappedBy="book", cascade={"merge", "persist"}, orphanRemoval=true)
  88.      * @ORM\OrderBy({"number" = "ASC"})
  89.      */
  90.     private $units;
  91.     /**
  92.      * @var Collection|Video[]
  93.      *
  94.      * @ORM\OneToMany(targetEntity="App\Entity\Video", mappedBy="book", cascade={"merge", "persist"}, orphanRemoval=true)
  95.      */
  96.     private $videos;
  97.     /**
  98.      * @Groups({"Get"})
  99.      *
  100.      * @ApiProperty(
  101.      *     attributes={
  102.      *         "swagger_context"={
  103.      *             "name"="IconURIs",
  104.      *             "type"="collection",
  105.      *         }
  106.      *     }
  107.      * )
  108.      *
  109.      * @var Collection|BookIcon[] A collection of icon uris KeyPair<string, string>
  110.      *
  111.      * @ORM\OneToMany(targetEntity="App\Entity\BookIcon", mappedBy="book", cascade={"merge", "persist"}, orphanRemoval=true)
  112.      */
  113.     private $bookIcons;
  114.     /**
  115.      * @var Collection|Subscription[]
  116.      *
  117.      * @ORM\OneToMany(targetEntity="App\Entity\Subscription", mappedBy="book", cascade={"persist"})
  118.      */
  119.     private $subscriptions;
  120.     /**
  121.      * Book constructor.
  122.      */
  123.     public function __construct()
  124.     {
  125.         $this->units = new ArrayCollection();
  126.         $this->videos = new ArrayCollection();
  127.         $this->bookIcons = new ArrayCollection();
  128.         $this->subscriptions = new ArrayCollection();
  129.     }
  130.     public function __toString()
  131.     {
  132.         return $this->title;
  133.     }
  134.     /*
  135.      * Getters
  136.      */
  137.     /**
  138.      * @return int
  139.      */
  140.     public function getId(): int
  141.     {
  142.         return $this->id;
  143.     }
  144.     /**
  145.      * @return string
  146.      */
  147.     public function getTitle(): string
  148.     {
  149.         return $this->title;
  150.     }
  151.     /**
  152.      * @return int
  153.      */
  154.     public function getPosition(): int
  155.     {
  156.         return $this->position;
  157.     }
  158.     /**
  159.      * @return Collection|Unit[]
  160.      */
  161.     public function getUnits(): Collection
  162.     {
  163.         return $this->units;
  164.     }
  165.     /**
  166.      * @return Collection|Unit[]
  167.      */
  168.     public function getBonusUnits(): Collection
  169.     {
  170.         return $this->units->filter(function (Unit $unit) {
  171.             return $unit->getType() === Unit::TYPE_BONUS;
  172.         });
  173.     }
  174.     /**
  175.      * @return Collection|Unit[]
  176.      */
  177.     public function getEducationUnits(): Collection
  178.     {
  179.         return $this->units->filter(function (Unit $unit) {
  180.             return $unit->getType() === Unit::TYPE_EDUCATION;
  181.         });
  182.     }
  183.     /**
  184.      * @return Collection|Video[]
  185.      */
  186.     public function getVideos(): Collection
  187.     {
  188.         return $this->videos;
  189.     }
  190.     /**
  191.      * @return Collection|BookIcon[]
  192.      */
  193.     public function getBookIcons(): Collection
  194.     {
  195.         return $this->bookIcons;
  196.     }
  197.     /**
  198.      * @param $resolution
  199.      * @return null|BookIcon
  200.      */
  201.     public function findBookIconByResolution($resolution): ?BookIcon
  202.     {
  203.         $criteria Criteria::create()->where(Criteria::expr()->eq('resolution'$resolution));
  204.         return $this->bookIcons->matching($criteria)->get(0);
  205.     }
  206.     /**
  207.      * @param User $user
  208.      * @return array
  209.      */
  210.     public function getVideosByUser(User $user)
  211.     {
  212.         if ($user->findActiveSubscriptionByBook($this)) {
  213.             return $this->videos;
  214.         }
  215.         return $this->videos->filter(function (Video $video) {
  216.             return $video->getUnit()->getNumber() === Unit::FREE_UNIT_NUMBER && $video->getUnit()->getType() === Unit::TYPE_EDUCATION;
  217.         })->getValues();
  218.     }
  219.     /*
  220.      * Setters
  221.      */
  222.     /**
  223.      * @param string $title
  224.      * @return Book
  225.      */
  226.     public function setTitle(string $title): self
  227.     {
  228.         $this->title $title;
  229.         return $this;
  230.     }
  231.     /**
  232.      * @param int $position
  233.      * @return Book
  234.      */
  235.     public function setPosition(int $position): self
  236.     {
  237.         $this->position $position;
  238.         return $this;
  239.     }
  240.     /**
  241.      * @param Unit $unit
  242.      * @return Book
  243.      */
  244.     public function addUnit(Unit $unit): self
  245.     {
  246.         if (!$this->units->contains($unit)) {
  247.             $this->units[] = $unit;
  248.             $unit->setBook($this);
  249.         }
  250.         return $this;
  251.     }
  252.     /**
  253.      * @param Unit $unit
  254.      * @return Book
  255.      */
  256.     public function removeUnit(Unit $unit): self
  257.     {
  258.         if ($this->units->contains($unit)) {
  259.             $this->units->removeElement($unit);
  260.             // set the owning side to null (unless already changed)
  261.             if ($unit->getBook() === $this) {
  262.                 $unit->setBook(null);
  263.             }
  264.         }
  265.         return $this;
  266.     }
  267.     /**
  268.      * @param Video $video
  269.      * @return Book
  270.      */
  271.     public function addVideo(Video $video): self
  272.     {
  273.         if (!$this->videos->contains($video)) {
  274.             $this->videos[] = $video;
  275.             $video->setBook($this);
  276.         }
  277.         return $this;
  278.     }
  279.     /**
  280.      * @param Video $video
  281.      * @return Book
  282.      */
  283.     public function removeVideo(Video $video): self
  284.     {
  285.         if ($this->videos->contains($video)) {
  286.             $this->videos->removeElement($video);
  287.             // set the owning side to null (unless already changed)
  288.             if ($video->getBook() === $this) {
  289.                 $video->setBook(null);
  290.             }
  291.         }
  292.         return $this;
  293.     }
  294.     /**
  295.      * @param BookIcon $bookIcon
  296.      * @return Book
  297.      */
  298.     public function addBookIcon(BookIcon $bookIcon): self
  299.     {
  300.         if (!$this->bookIcons->contains($bookIcon)) {
  301.             $this->bookIcons[] = $bookIcon;
  302.             $bookIcon->setBook($this);
  303.         }
  304.         return $this;
  305.     }
  306.     /**
  307.      * @param BookIcon $bookIcon
  308.      * @return Book
  309.      */
  310.     public function removeBookIcon(BookIcon $bookIcon): self
  311.     {
  312.         if ($this->bookIcons->contains($bookIcon)) {
  313.             $this->bookIcons->removeElement($bookIcon);
  314.             // set the owning side to null (unless already changed)
  315.             if ($bookIcon->getBook() === $this) {
  316.                 $bookIcon->setBook(null);
  317.             }
  318.         }
  319.         return $this;
  320.     }
  321. }