src/Entity/Channel.php line 19

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ChannelRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use ApiPlatform\Action\NotFoundAction;
  8. use ApiPlatform\Metadata\Get;
  9. use ApiPlatform\Metadata\GetCollection;
  10. use ApiPlatform\Metadata\ApiResource;
  11. use Symfony\Component\Serializer\Annotation\MaxDepth;
  12. #[ApiResource(
  13.     normalizationContext: ['enable_max_depth'=>true],
  14. )]
  15. #[ORM\Entity(repositoryClassChannelRepository::class)]
  16. class Channel
  17. {
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue]
  20.     #[ORM\Column]
  21.     private ?int $id null;
  22.     #[MaxDepth(1)]
  23.     #[ORM\OneToMany(mappedBy'channel'targetEntityUser::class)]
  24.     private Collection $users;
  25.     #[MaxDepth(1)]
  26.     #[ORM\OneToMany(mappedBy'channel'targetEntityRefill::class)]
  27.     private Collection $refills;
  28.     #[MaxDepth(1)]
  29.     #[ORM\OneToOne(mappedBy'channel'cascade: ['persist''remove'])]
  30.     private ?RefillSetting $refillSetting null;
  31.     public function __construct()
  32.     {
  33.         $this->users = new ArrayCollection();
  34.         $this->refills = new ArrayCollection();
  35.     }
  36.     public function getId(): ?int
  37.     {
  38.         return $this->id;
  39.     }
  40.     /**
  41.      * Gets the total amount of tokens belonging to the channel, not taking into consideration entity type and action
  42.      */
  43.     public function getBalance(): ?int
  44.     {
  45.         $balance 0;
  46.         foreach($this->refills as $refill)
  47.             $balance += ($refill->getQuantity() - $refill->getUsed());
  48.         return $balance;
  49.     }
  50.     /**
  51.      * @return Collection<int, User>
  52.      */
  53.     public function getUsers(): Collection
  54.     {
  55.         return $this->users;
  56.     }
  57.     public function addUser(User $user): self
  58.     {
  59.         if (!$this->users->contains($user)) {
  60.             $this->users->add($user);
  61.             $user->setChannel($this);
  62.         }
  63.         return $this;
  64.     }
  65.     public function removeUser(User $user): self
  66.     {
  67.         if ($this->users->removeElement($user)) {
  68.             // set the owning side to null (unless already changed)
  69.             if ($user->getChannel() === $this) {
  70.                 $user->setChannel(null);
  71.             }
  72.         }
  73.         return $this;
  74.     }
  75.     /**
  76.      * @return Collection<int, Refill>
  77.      */
  78.     public function getRefills(): Collection
  79.     {
  80.         return $this->refills;
  81.     }
  82.     public function addRefill(Refill $refill): self
  83.     {
  84.         if (!$this->refills->contains($refill)) {
  85.             $this->refills->add($refill);
  86.             $refill->setChannel($this);
  87.         }
  88.         return $this;
  89.     }
  90.     public function removeRefill(Refill $refill): self
  91.     {
  92.         if ($this->refills->removeElement($refill)) {
  93.             // set the owning side to null (unless already changed)
  94.             if ($refill->getChannel() === $this) {
  95.                 $refill->setChannel(null);
  96.             }
  97.         }
  98.         return $this;
  99.     }
  100.     public function getRefillSetting(): ?RefillSetting
  101.     {
  102.         return $this->refillSetting;
  103.     }
  104.     public function setRefillSetting(?RefillSetting $refillSetting): self
  105.     {
  106.         // unset the owning side of the relation if necessary
  107.         if ($refillSetting === null && $this->refillSetting !== null) {
  108.             $this->refillSetting->setChannel(null);
  109.         }
  110.         // set the owning side of the relation if necessary
  111.         if ($refillSetting !== null && $refillSetting->getChannel() !== $this) {
  112.             $refillSetting->setChannel($this);
  113.         }
  114.         $this->refillSetting $refillSetting;
  115.         return $this;
  116.     }
  117.     public function __toString()
  118.     {
  119.         return $this->id;
  120.     }
  121. }