src/Entity/CartProduct.php line 48

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  4. use ApiPlatform\Metadata\ApiFilter;
  5. use ApiPlatform\Metadata\ApiProperty;
  6. use ApiPlatform\Metadata\ApiResource;
  7. use ApiPlatform\Metadata\Delete;
  8. use ApiPlatform\Metadata\Get;
  9. use ApiPlatform\Metadata\GetCollection;
  10. use ApiPlatform\Metadata\Post;
  11. use ApiPlatform\Metadata\Put;
  12. use App\Entity\Trait\IdTrait;
  13. use App\Entity\Trait\UuidTrait;
  14. use App\Enum\Language;
  15. use App\Filter\IriFilter;
  16. use App\Repository\CartProductRepository;
  17. use Doctrine\DBAL\Types\Types;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Symfony\Component\Validator\Constraints as Assert;
  21. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  22. #[ApiResource(
  23.     description'Cart products',
  24.     operations: [
  25.         new GetCollection(),
  26.         new Post(
  27.             denormalizationContext: ['groups' => ['write''post']],
  28.         ),
  29.         new Get(),
  30.         new Put(),
  31.         new Delete(),
  32.     ],
  33.     normalizationContext: ['groups' => [
  34.         'read',
  35.         'read:' self::class,
  36.     ]],
  37.     denormalizationContext: ['groups' => ['write']],
  38.     order: ['count' => 'desc'],
  39.     security'is_granted("' self::class . '")',
  40.     extraProperties: ['standard_put' => false],
  41. )]
  42. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  43. #[ApiFilter(OrderFilter::class, properties: ['count'])]
  44. #[ORM\Entity(repositoryClassCartProductRepository::class)]
  45. class CartProduct
  46. {
  47.     use IdTrait;
  48.     use UuidTrait;
  49.     #[ApiProperty(description'Parent'writableLinkfalse)]
  50.     #[Groups(['read:' self::class, 'post'])]
  51.     #[ORM\ManyToOne(inversedBy'products')]
  52.     #[ORM\JoinColumn(nullablefalse)]
  53.     private ?Cart $parent null;
  54.     #[ApiProperty(description'Article'writableLinkfalse)]
  55.     #[Groups(['read:' self::class, 'write'])]
  56.     #[ORM\ManyToOne(inversedBy'cartProducts')]
  57.     private ?JournalArticle $article null;
  58.     #[ApiProperty(description'Issue'writableLinkfalse)]
  59.     #[Groups(['read:' self::class, 'write'])]
  60.     #[ORM\ManyToOne(inversedBy'cartProducts')]
  61.     private ?JournalIssue $issue null;
  62.     #[ApiProperty(description'Count'writableLinkfalse)]
  63.     #[Groups(['read:' self::class, 'write'])]
  64.     #[ORM\Column(options: ['default' => 1])]
  65.     private int $count 1;
  66.     #[ApiProperty(description'Article'writableLinkfalse)]
  67.     #[Groups(['read:' self::class, 'write'])]
  68.     #[ORM\Column(options: ['default' => 0])]
  69.     private int $vat 0;
  70.     #[ApiProperty(description'Price')]
  71.     #[Groups(['read:' self::class])]
  72.     #[ORM\Column(typeTypes::INTEGERnullabletrue)]
  73.     private ?int $price null;
  74.     public function __construct()
  75.     {
  76.         $this->setUuid();
  77.     }
  78.     public function getParent(): ?Cart
  79.     {
  80.         return $this->parent;
  81.     }
  82.     public function setParent(?Cart $parent): self
  83.     {
  84.         $this->parent $parent;
  85.         return $this;
  86.     }
  87.     public function getArticle(): ?JournalArticle
  88.     {
  89.         return $this->article;
  90.     }
  91.     public function setArticle(?JournalArticle $article): self
  92.     {
  93.         $this->article $article;
  94.         if ($article) {
  95.             //$this->setVat($article->getParent()->getData()->getArticleVat() ?? 0);
  96.             $this->setVat($this->getParent()->getCountry()?->getRegion()?->getVat() ?? 0);
  97.             $this->setPrice($article->getPrice());
  98.         }
  99.         return $this;
  100.     }
  101.     public function getIssue(): ?JournalIssue
  102.     {
  103.         return $this->issue;
  104.     }
  105.     public function setIssue(?JournalIssue $issue): self
  106.     {
  107.         $this->issue $issue;
  108.         if ($issue) {
  109.             //$this->setVat($issue->getParent()->getData()->getIssueVat() ?? 0);
  110.             $this->setVat($this->getParent()->getCountry()?->getRegion()?->getVat() ?? 0);
  111.             $this->setPrice($issue->getPrice());
  112.         }
  113.         return $this;
  114.     }
  115.     public function getCount(): int
  116.     {
  117.         return $this->count;
  118.     }
  119.     public function setCount(int $count): self
  120.     {
  121.         $this->count $count;
  122.         return $this;
  123.     }
  124.     public function getVat(): int
  125.     {
  126.         return $this->vat;
  127.     }
  128.     public function setVat(int $vat): self
  129.     {
  130.         $this->vat $vat;
  131.         return $this;
  132.     }
  133.     public function getPrice(): ?int
  134.     {
  135.         return $this->price;
  136.     }
  137.     #[ApiProperty(description'Title')]
  138.     #[Groups(['read:' self::class])]
  139.     public function getTitle(?Language $language null): ?string
  140.     {
  141.         if ($this->getArticle()) {
  142.             if ($this->getArticle()->getSlugTitleLanguage() && $this->getArticle()->readAvailableMetadataTranslation($this->getArticle()->getSlugTitleLanguage(), 'title')) {
  143.                 return $this->getArticle()->readAvailableMetadataTranslation($this->getArticle()->getSlugTitleLanguage(), 'title');
  144.             } else {
  145.                 return $this->getArticle()->readAvailableMetadataTranslation($language ?? Language::PL'title');
  146.             }
  147.         } else {
  148.             // product.issue.readAvailableTranslation(app.request.locale, 'title')
  149.             return $this->getIssue()->readAvailableTranslation($language ?? Language::PL'fullTitle');
  150.         }
  151.     }
  152.     #[Groups(['read:' self::class])]
  153.     public function getPriceReal(): ?float
  154.     {
  155.         return $this->price round($this->price 10002) : null;
  156.     }
  157.     public function setPrice(?int $price): self
  158.     {
  159.         $this->price $price;
  160.         return $this;
  161.     }
  162.     #[Groups(['write'])]
  163.     public function setPriceReal(?float $priceReal): self
  164.     {
  165.         $this->price = (int) ($priceReal 1000);
  166.         return $this;
  167.     }
  168.     #[Assert\Callback]
  169.     public function validate(ExecutionContextInterface $contextmixed $payload): void
  170.     {
  171.         if (!$this->getArticle() && !$this->getIssue()) {
  172.             $context->buildViolation('Either article or issue must be set.')
  173.                 ->atPath('article')
  174.                 ->atPath('issue')
  175.                 ->addViolation();
  176.         }
  177.         if ($this->getArticle() && $this->getIssue()) {
  178.             $context->buildViolation('Only one of article or issue must be set.')
  179.                 ->atPath('article')
  180.                 ->atPath('issue')
  181.                 ->addViolation();
  182.         }
  183.     }
  184. }