src/Entity/Channel.php line 19
<?php
namespace App\Entity;
use App\Repository\ChannelRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Action\NotFoundAction;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Serializer\Annotation\MaxDepth;
#[ApiResource(
normalizationContext: ['enable_max_depth'=>true],
)]
#[ORM\Entity(repositoryClass: ChannelRepository::class)]
class Channel
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[MaxDepth(1)]
#[ORM\OneToMany(mappedBy: 'channel', targetEntity: User::class)]
private Collection $users;
#[MaxDepth(1)]
#[ORM\OneToMany(mappedBy: 'channel', targetEntity: Refill::class)]
private Collection $refills;
#[MaxDepth(1)]
#[ORM\OneToOne(mappedBy: 'channel', cascade: ['persist', 'remove'])]
private ?RefillSetting $refillSetting = null;
public function __construct()
{
$this->users = new ArrayCollection();
$this->refills = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* Gets the total amount of tokens belonging to the channel, not taking into consideration entity type and action
*/
public function getBalance(): ?int
{
$balance = 0;
foreach($this->refills as $refill)
$balance += ($refill->getQuantity() - $refill->getUsed());
return $balance;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users->add($user);
$user->setChannel($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getChannel() === $this) {
$user->setChannel(null);
}
}
return $this;
}
/**
* @return Collection<int, Refill>
*/
public function getRefills(): Collection
{
return $this->refills;
}
public function addRefill(Refill $refill): self
{
if (!$this->refills->contains($refill)) {
$this->refills->add($refill);
$refill->setChannel($this);
}
return $this;
}
public function removeRefill(Refill $refill): self
{
if ($this->refills->removeElement($refill)) {
// set the owning side to null (unless already changed)
if ($refill->getChannel() === $this) {
$refill->setChannel(null);
}
}
return $this;
}
public function getRefillSetting(): ?RefillSetting
{
return $this->refillSetting;
}
public function setRefillSetting(?RefillSetting $refillSetting): self
{
// unset the owning side of the relation if necessary
if ($refillSetting === null && $this->refillSetting !== null) {
$this->refillSetting->setChannel(null);
}
// set the owning side of the relation if necessary
if ($refillSetting !== null && $refillSetting->getChannel() !== $this) {
$refillSetting->setChannel($this);
}
$this->refillSetting = $refillSetting;
return $this;
}
public function __toString()
{
return $this->id;
}
}