src/Entity/CustomerDirectoryItem.php line 23
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert,
Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use App\Entity\Trait\IdTrait,
App\Entity\Trait\UuidTrait,
App\Entity\Trait\TimestampableTrait,
App\Repository\CustomerDirectoryItemRepository;
use App\Front\Exception\FrontApiException;
#[Assert\Expression(
expression: 'this.isValid()',
message: 'Only one resource must be assigned.'
)]
#[UniqueEntity(fields: ['parent', 'volume', 'issue', 'article'], ignoreNull: false, message: 'You have already added that resource.')]
#[ORM\UniqueConstraint(fields: ['parent', 'volume', 'issue', 'article'])]
#[ORM\Entity(repositoryClass: CustomerDirectoryItemRepository::class)]
class CustomerDirectoryItem
{
use IdTrait,
UuidTrait,
TimestampableTrait;
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: CustomerDirectory::class, inversedBy: 'items')]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private ?CustomerDirectory $parent = null;
// TODO - validate only free and bought ones
#[ORM\ManyToOne(targetEntity: JournalVolume::class)]
#[ORM\JoinColumn(onDelete: 'cascade')]
private ?JournalVolume $volume = null;
// TODO - validate only free and bought ones
#[ORM\ManyToOne(targetEntity: JournalIssue::class)]
#[ORM\JoinColumn(onDelete: 'cascade')]
private ?JournalIssue $issue = null;
// TODO - validate only free and bought ones
#[ORM\ManyToOne(targetEntity: JournalArticle::class)]
#[ORM\JoinColumn(onDelete: 'cascade')]
private ?JournalArticle $article = null;
public function __construct()
{
$this->setUuid();
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
}
public function getParent(): ?CustomerDirectory
{
return $this->parent;
}
public function setParent(CustomerDirectory $parent): self
{
if ($this->parent && $this->parent->getParent() !== $parent->getParent()) {
throw new FrontApiException(403, 'Forbidden.');
}
$this->parent = $parent;
$parent->addItem($this);
return $this;
}
public function getCustomer(): ?Customer
{
return $this->parent?->getParent();
}
public function getVolume(): ?JournalVolume
{
return $this->volume;
}
public function setVolume(?JournalVolume $volume): self
{
$this->volume = $volume;
return $this;
}
public function getIssue(): ?JournalIssue
{
return $this->issue;
}
public function setIssue(?JournalIssue $issue): self
{
$this->issue = $issue;
return $this;
}
public function getArticle(): ?JournalArticle
{
return $this->article;
}
public function setArticle(?JournalArticle $article): self
{
$this->article = $article;
return $this;
}
public function isValid(): bool
{
$assigned = 0;
$assigned += $this->volume ? 1 : 0;
$assigned += $this->issue ? 1 : 0;
$assigned += $this->article ? 1 : 0;
return $assigned === 1;
}
public function isArticle(): bool
{
return $this->article !== null;
}
public function isVolume(): bool
{
return $this->volume !== null;
}
public function isIssue(): bool
{
return $this->issue !== null;
}
}