src/Entity/CustomerDirectoryItem.php line 23

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert,
  5.     Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use App\Entity\Trait\IdTrait,
  7.     App\Entity\Trait\UuidTrait,
  8.     App\Entity\Trait\TimestampableTrait,
  9.     App\Repository\CustomerDirectoryItemRepository;
  10. use App\Front\Exception\FrontApiException;
  11. #[Assert\Expression(
  12.     expression'this.isValid()',
  13.     message'Only one resource must be assigned.'
  14. )]
  15. #[UniqueEntity(fields: ['parent''volume''issue''article'], ignoreNullfalsemessage'You have already added that resource.')]
  16. #[ORM\UniqueConstraint(fields: ['parent''volume''issue''article'])]
  17. #[ORM\Entity(repositoryClassCustomerDirectoryItemRepository::class)]
  18. class CustomerDirectoryItem
  19. {
  20.     use IdTrait,
  21.         UuidTrait,
  22.         TimestampableTrait;
  23.     #[Assert\NotNull]
  24.     #[ORM\ManyToOne(targetEntityCustomerDirectory::class, inversedBy'items')]
  25.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  26.     private ?CustomerDirectory $parent null;
  27.     // TODO - validate only free and bought ones
  28.     #[ORM\ManyToOne(targetEntityJournalVolume::class)]
  29.     #[ORM\JoinColumn(onDelete'cascade')]
  30.     private ?JournalVolume $volume null;
  31.     // TODO - validate only free and bought ones
  32.     #[ORM\ManyToOne(targetEntityJournalIssue::class)]
  33.     #[ORM\JoinColumn(onDelete'cascade')]
  34.     private ?JournalIssue $issue null;
  35.     // TODO - validate only free and bought ones
  36.     #[ORM\ManyToOne(targetEntityJournalArticle::class)]
  37.     #[ORM\JoinColumn(onDelete'cascade')]
  38.     private ?JournalArticle $article null;
  39.     public function __construct()
  40.     {
  41.         $this->setUuid();
  42.         $this->createdAt = new \DateTimeImmutable();
  43.         $this->updatedAt = new \DateTimeImmutable();
  44.     }
  45.     public function getParent(): ?CustomerDirectory
  46.     {
  47.         return $this->parent;
  48.     }
  49.     public function setParent(CustomerDirectory $parent): self
  50.     {
  51.         if ($this->parent && $this->parent->getParent() !== $parent->getParent()) {
  52.             throw new FrontApiException(403'Forbidden.');
  53.         }
  54.         $this->parent $parent;
  55.         $parent->addItem($this);
  56.         return $this;
  57.     }
  58.     public function getCustomer(): ?Customer
  59.     {
  60.         return $this->parent?->getParent();
  61.     }
  62.     public function getVolume(): ?JournalVolume
  63.     {
  64.         return $this->volume;
  65.     }
  66.     public function setVolume(?JournalVolume $volume): self
  67.     {
  68.         $this->volume $volume;
  69.         return $this;
  70.     }
  71.     public function getIssue(): ?JournalIssue
  72.     {
  73.         return $this->issue;
  74.     }
  75.     public function setIssue(?JournalIssue $issue): self
  76.     {
  77.         $this->issue $issue;
  78.         return $this;
  79.     }
  80.     public function getArticle(): ?JournalArticle
  81.     {
  82.         return $this->article;
  83.     }
  84.     public function setArticle(?JournalArticle $article): self
  85.     {
  86.         $this->article $article;
  87.         return $this;
  88.     }
  89.     public function isValid(): bool
  90.     {
  91.         $assigned 0;
  92.         $assigned += $this->volume 0;
  93.         $assigned += $this->issue 0;
  94.         $assigned += $this->article 0;
  95.         return $assigned === 1;
  96.     }
  97.     public function isArticle(): bool
  98.     {
  99.         return $this->article !== null;
  100.     }
  101.     public function isVolume(): bool
  102.     {
  103.         return $this->volume !== null;
  104.     }
  105.     public function isIssue(): bool
  106.     {
  107.         return $this->issue !== null;
  108.     }
  109. }