src/Entity/CartProduct.php line 48
<?php
namespace App\Entity;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Entity\Trait\IdTrait;
use App\Entity\Trait\UuidTrait;
use App\Enum\Language;
use App\Filter\IriFilter;
use App\Repository\CartProductRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[ApiResource(
description: 'Cart products',
operations: [
new GetCollection(),
new Post(
denormalizationContext: ['groups' => ['write', 'post']],
),
new Get(),
new Put(),
new Delete(),
],
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
]],
denormalizationContext: ['groups' => ['write']],
order: ['count' => 'desc'],
security: 'is_granted("' . self::class . '")',
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(IriFilter::class, properties: ['parent'])]
#[ApiFilter(OrderFilter::class, properties: ['count'])]
#[ORM\Entity(repositoryClass: CartProductRepository::class)]
class CartProduct
{
use IdTrait;
use UuidTrait;
#[ApiProperty(description: 'Parent', writableLink: false)]
#[Groups(['read:' . self::class, 'post'])]
#[ORM\ManyToOne(inversedBy: 'products')]
#[ORM\JoinColumn(nullable: false)]
private ?Cart $parent = null;
#[ApiProperty(description: 'Article', writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\ManyToOne(inversedBy: 'cartProducts')]
private ?JournalArticle $article = null;
#[ApiProperty(description: 'Issue', writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\ManyToOne(inversedBy: 'cartProducts')]
private ?JournalIssue $issue = null;
#[ApiProperty(description: 'Count', writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(options: ['default' => 1])]
private int $count = 1;
#[ApiProperty(description: 'Article', writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(options: ['default' => 0])]
private int $vat = 0;
#[ApiProperty(description: 'Price')]
#[Groups(['read:' . self::class])]
#[ORM\Column(type: Types::INTEGER, nullable: true)]
private ?int $price = null;
public function __construct()
{
$this->setUuid();
}
public function getParent(): ?Cart
{
return $this->parent;
}
public function setParent(?Cart $parent): self
{
$this->parent = $parent;
return $this;
}
public function getArticle(): ?JournalArticle
{
return $this->article;
}
public function setArticle(?JournalArticle $article): self
{
$this->article = $article;
if ($article) {
//$this->setVat($article->getParent()->getData()->getArticleVat() ?? 0);
$this->setVat($this->getParent()->getCountry()?->getRegion()?->getVat() ?? 0);
$this->setPrice($article->getPrice());
}
return $this;
}
public function getIssue(): ?JournalIssue
{
return $this->issue;
}
public function setIssue(?JournalIssue $issue): self
{
$this->issue = $issue;
if ($issue) {
//$this->setVat($issue->getParent()->getData()->getIssueVat() ?? 0);
$this->setVat($this->getParent()->getCountry()?->getRegion()?->getVat() ?? 0);
$this->setPrice($issue->getPrice());
}
return $this;
}
public function getCount(): int
{
return $this->count;
}
public function setCount(int $count): self
{
$this->count = $count;
return $this;
}
public function getVat(): int
{
return $this->vat;
}
public function setVat(int $vat): self
{
$this->vat = $vat;
return $this;
}
public function getPrice(): ?int
{
return $this->price;
}
#[ApiProperty(description: 'Title')]
#[Groups(['read:' . self::class])]
public function getTitle(?Language $language = null): ?string
{
if ($this->getArticle()) {
if ($this->getArticle()->getSlugTitleLanguage() && $this->getArticle()->readAvailableMetadataTranslation($this->getArticle()->getSlugTitleLanguage(), 'title')) {
return $this->getArticle()->readAvailableMetadataTranslation($this->getArticle()->getSlugTitleLanguage(), 'title');
} else {
return $this->getArticle()->readAvailableMetadataTranslation($language ?? Language::PL, 'title');
}
} else {
// product.issue.readAvailableTranslation(app.request.locale, 'title')
return $this->getIssue()->readAvailableTranslation($language ?? Language::PL, 'fullTitle');
}
}
#[Groups(['read:' . self::class])]
public function getPriceReal(): ?float
{
return $this->price ? round($this->price / 1000, 2) : null;
}
public function setPrice(?int $price): self
{
$this->price = $price;
return $this;
}
#[Groups(['write'])]
public function setPriceReal(?float $priceReal): self
{
$this->price = (int) ($priceReal * 1000);
return $this;
}
#[Assert\Callback]
public function validate(ExecutionContextInterface $context, mixed $payload): void
{
if (!$this->getArticle() && !$this->getIssue()) {
$context->buildViolation('Either article or issue must be set.')
->atPath('article')
->atPath('issue')
->addViolation();
}
if ($this->getArticle() && $this->getIssue()) {
$context->buildViolation('Only one of article or issue must be set.')
->atPath('article')
->atPath('issue')
->addViolation();
}
}
}