src/Shop/Doctrine/Entity/Product.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Shop\Doctrine\Entity;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use App\Program\Doctrine\Entity\Profile;
  7. use App\Shop\Doctrine\Repository\ProductRepository;
  8. use App\Shop\Filter\ProductFilter;
  9. use App\Supplier\Doctrine\Entity\Brand as SupplierBrand;
  10. use App\Supplier\Doctrine\Entity\Supplier;
  11. use App\Supplier\Doctrine\Entity\Taxonomy as SupplierTaxonomy;
  12. use DateTime;
  13. use DateTimeImmutable;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\ORM\Mapping\Column;
  17. use Doctrine\ORM\Mapping\Entity;
  18. use Doctrine\ORM\Mapping\GeneratedValue;
  19. use Doctrine\ORM\Mapping\Id;
  20. use Doctrine\ORM\Mapping\Index;
  21. use Doctrine\ORM\Mapping\ManyToOne;
  22. use Doctrine\ORM\Mapping\OneToMany;
  23. use Doctrine\ORM\Mapping\OneToOne;
  24. use Doctrine\ORM\Mapping\OrderBy;
  25. use Doctrine\ORM\Mapping\Table;
  26. use Symfony\Component\HttpFoundation\File\File;
  27. use Symfony\Component\Serializer\Annotation\Groups;
  28. use Symfony\Component\Validator\Constraints\Image;
  29. use Symfony\Component\Validator\Constraints\NotBlank;
  30. use Symfony\Component\Validator\Constraints\NotNull;
  31. use Symfony\Component\Validator\Constraints\Valid;
  32. use Vich\UploaderBundle\Mapping\Annotation\Uploadable;
  33. use Vich\UploaderBundle\Mapping\Annotation\UploadableField;
  34. #[ApiResource(
  35.     collectionOperations: ['get'],
  36.     itemOperations: ['get'],
  37.     normalizationContext: ['groups' => ['product''Default']],
  38. )]
  39. #[Uploadable]
  40. #[ApiFilter(ProductFilter::class)]
  41. #[Entity(repositoryClassProductRepository::class)]
  42. #[Table(name'`shop_product`')]
  43. #[Index(columns: ['supplier_id''`reference`'], name'supplier_id_reference')]
  44. #[Index(columns: ['supplier_id''state'], name'supplier_id_state')]
  45. class Product
  46. {
  47.     #[Id]
  48.     #[Column(type'integer')]
  49.     #[GeneratedValue]
  50.     #[Groups('product')]
  51.     private ?int $id null;
  52.     #[ManyToOne(targetEntitySupplier::class)]
  53.     private ?Supplier $supplier null;
  54.     #[Column(name'`reference`')]
  55.     #[Groups('product')]
  56.     private string $reference;
  57.     #[Column(nullabletruename'`reference_comm`')]
  58.     #[Groups('product')]
  59.     private ?string $referenceComm null;
  60.     #[Column(type'datetime_immutable')]
  61.     private DateTimeImmutable $updatedAt;
  62.     #[Column]
  63.     #[NotBlank]
  64.     #[Groups('product')]
  65.     private string $name;
  66.     #[Column(type'text')]
  67.     #[NotBlank]
  68.     #[Groups('product')]
  69.     private string $description;
  70.     #[Column(nullabletrue)]
  71.     #[Groups('product')]
  72.     private ?string $image null;
  73.     #[Column(nullabletruetype'text')]
  74.     private ?string $imageUrl null;
  75.     #[UploadableField(mapping'images'fileNameProperty'image')]
  76.     #[NotNull(groups: ['create'])]
  77.     #[Image(groups: ['create'])]
  78.     private ?File $imageFile null;
  79.     #[ManyToOne(targetEntityBrand::class, inversedBy'products')]
  80.     #[Groups('product')]
  81.     private ?Brand $brand null;
  82.     #[ManyToOne(targetEntityTaxonomy::class, inversedBy'products')]
  83.     #[Groups('product')]
  84.     private ?Taxonomy $taxonomy null;
  85.     #[ManyToOne(targetEntitySupplierBrand::class, inversedBy'products')]
  86.     private ?SupplierBrand $supplierBrand null;
  87.     #[ManyToOne(targetEntitySupplierTaxonomy::class, inversedBy'products')]
  88.     private ?SupplierTaxonomy $supplierTaxonomy null;
  89.     /**
  90.      * @var Collection<int, FeatureValue>
  91.      */
  92.     #[OneToMany(mappedBy'product'targetEntityFeatureValue::class, cascade: ['persist'], orphanRemovaltrue)]
  93.     #[Groups('product')]
  94.     private Collection $features;
  95.     /**
  96.      * @var Collection<int, Price>
  97.      */
  98.     #[OneToMany(mappedBy'product'targetEntityPrice::class)]
  99.     #[OrderBy(['createdAt' => 'DESC'])]
  100.     private Collection $pricingHistory;
  101.     #[OneToOne(targetEntityPrice::class, cascade: ['persist''remove'])]
  102.     #[Valid]
  103.     private ?Price $currentPrice null;
  104.     #[Column]
  105.     private string $state 'draft';
  106.     /**
  107.      * @var array<array-key, array<string, string>>
  108.      */
  109.     #[Column(type'array')]
  110.     private array $statesHistory = [];
  111.     #[Column(type'integer')]
  112.     private int $stock 0;
  113.     #[Column(nullabletrue)]
  114.     private ?string $lastState null;
  115.     #[Groups(['product'])]
  116.     private int $points 0;
  117.     #[Column(type'boolean'options: ['default' => false])]
  118.     private $toEdit false;
  119.     #[Column(type'datetime'nullabletrue)]
  120.     private ?DateTime $creationDate null;
  121.     #[Column(nullabletrue)]
  122.     private ?string $shippingMode null;
  123.     public function __construct()
  124.     {
  125.         $this->statesHistory[] = ['date' => date('Y-m-d H:i:s'), 'state' => 'draft'];
  126.         $this->features = new ArrayCollection();
  127.         $this->pricingHistory = new ArrayCollection();
  128.         $this->updatedAt = new DateTimeImmutable();
  129.     }
  130.     public function __toString(): string
  131.     {
  132.         return $this->getName();
  133.     }
  134.     public function getId(): ?int
  135.     {
  136.         return $this->id;
  137.     }
  138.     public function getReference(): string
  139.     {
  140.         return $this->reference;
  141.     }
  142.     public function setReference(string $reference): void
  143.     {
  144.         $this->reference $reference;
  145.     }
  146.     public function getName(): string
  147.     {
  148.         return $this->name;
  149.     }
  150.     public function setName(string $name): void
  151.     {
  152.         $this->name $name;
  153.     }
  154.     public function getDescription(): string
  155.     {
  156.         return $this->description;
  157.     }
  158.     public function setDescription(string $description): void
  159.     {
  160.         $this->description $description;
  161.     }
  162.     public function getImage(): ?string
  163.     {
  164.         return $this->image;
  165.     }
  166.     public function setImage(?string $image): void
  167.     {
  168.         $this->image $image;
  169.     }
  170.     public function getBrand(): ?Brand
  171.     {
  172.         return $this->brand;
  173.     }
  174.     public function setBrand(?Brand $brand): void
  175.     {
  176.         $this->brand $brand;
  177.     }
  178.     public function getTaxonomy(): ?Taxonomy
  179.     {
  180.         return $this->taxonomy;
  181.     }
  182.     public function setTaxonomy(?Taxonomy $taxonomy): void
  183.     {
  184.         $this->taxonomy $taxonomy;
  185.     }
  186.     /**
  187.      * @return Collection<int, FeatureValue>
  188.      */
  189.     public function getFeatures(): Collection
  190.     {
  191.         return $this->features;
  192.     }
  193.     public function addFeature(FeatureValue $featureValue): void
  194.     {
  195.         if ($this->features->contains($featureValue)) {
  196.             return;
  197.         }
  198.         $featureValue->setProduct($this);
  199.         $this->features->add($featureValue);
  200.     }
  201.     public function removeFeature(FeatureValue $featureValue): void
  202.     {
  203.         if (!$this->features->contains($featureValue)) {
  204.             return;
  205.         }
  206.         $featureValue->setProduct(null);
  207.         $this->features->removeElement($featureValue);
  208.     }
  209.     public function getFeature(Feature|string $feature): ?FeatureValue
  210.     {
  211.         /** @var Collection<int, FeatureValue> $features */
  212.         $features $this->features
  213.             ->filter(static fn (FeatureValue $featureValue) => is_string($feature)
  214.                 ? $featureValue->getFeature()->getCode() === $feature
  215.                 $featureValue->getFeature() === $feature);
  216.         return false === $features->first() ? null $features->first();
  217.     }
  218.     /**
  219.      * @return Collection<int, Price>
  220.      */
  221.     public function getPricingHistory(): Collection
  222.     {
  223.         return $this->pricingHistory;
  224.     }
  225.     public function getCurrentPrice(): ?Price
  226.     {
  227.         return $this->currentPrice;
  228.     }
  229.     public function setCurrentPrice(?Price $currentPrice): void
  230.     {
  231.         $this->currentPrice $currentPrice;
  232.         if (null !== $this->currentPrice) {
  233.             $this->currentPrice->setProduct($this);
  234.         }
  235.     }
  236.     public function getImageFile(): ?File
  237.     {
  238.         return $this->imageFile;
  239.     }
  240.     public function setImageFile(?File $imageFile): void
  241.     {
  242.         $this->imageFile $imageFile;
  243.     }
  244.     public function getUpdatedAt(): DateTimeImmutable
  245.     {
  246.         return $this->updatedAt;
  247.     }
  248.     public function setUpdatedAt(DateTimeImmutable $updatedAt): void
  249.     {
  250.         $this->updatedAt $updatedAt;
  251.     }
  252.     public function getState(): string
  253.     {
  254.         return $this->state;
  255.     }
  256.     public function setState(string $state): void
  257.     {
  258.         $this->state $state;
  259.     }
  260.     /**
  261.      * @return array<array-key, array<string, string>>
  262.      */
  263.     public function getStatesHistory(): array
  264.     {
  265.         return $this->statesHistory;
  266.     }
  267.     public function addStateHistory(string $state): void
  268.     {
  269.         $this->statesHistory[] = ['date' => date('Y-m-d H:i:s'), 'state' => $state];
  270.     }
  271.     public function getSupplier(): ?Supplier
  272.     {
  273.         return $this->supplier;
  274.     }
  275.     public function setSupplier(?Supplier $supplier): void
  276.     {
  277.         $this->supplier $supplier;
  278.     }
  279.     public function getSupplierBrand(): ?SupplierBrand
  280.     {
  281.         return $this->supplierBrand;
  282.     }
  283.     public function setSupplierBrand(?SupplierBrand $supplierBrand): void
  284.     {
  285.         $this->supplierBrand $supplierBrand;
  286.     }
  287.     public function getSupplierTaxonomy(): ?SupplierTaxonomy
  288.     {
  289.         return $this->supplierTaxonomy;
  290.     }
  291.     public function setSupplierTaxonomy(?SupplierTaxonomy $supplierTaxonomy): void
  292.     {
  293.         $this->supplierTaxonomy $supplierTaxonomy;
  294.     }
  295.     public function getStock(): int
  296.     {
  297.         return $this->stock;
  298.     }
  299.     public function setStock(int $stock): void
  300.     {
  301.         $this->stock $stock;
  302.     }
  303.     public function getLastState(): ?string
  304.     {
  305.         return $this->lastState;
  306.     }
  307.     public function setLastState(?string $lastState): void
  308.     {
  309.         $this->lastState $lastState;
  310.     }
  311.     public function setPoints(Shop $shopProfile $profile): void
  312.     {
  313.         $price $shop->getPriceValuation()->evaluate($this);
  314.         $this->points intval($profile->getProgram()->getCurrencyToPoint()->evaluate($price));
  315.     }
  316.     public function getPoints(): int
  317.     {
  318.         return $this->points;
  319.     }
  320.     public function getImageUrl(): ?string
  321.     {
  322.         return $this->imageUrl;
  323.     }
  324.     public function setImageUrl(?string $imageUrl): void
  325.     {
  326.         $this->imageUrl $imageUrl;
  327.     }
  328.     public function isToEdit(): bool
  329.     {
  330.         return $this->toEdit;
  331.     }
  332.     public function setToEdit(bool $toEdit): void
  333.     {
  334.         $this->toEdit $toEdit;
  335.     }
  336.     public function getCreationDate(): ?DateTime
  337.     {
  338.         return $this->creationDate;
  339.     }
  340.     public function setCreationDate(?DateTime $creationDate): void
  341.     {
  342.         $this->creationDate $creationDate;
  343.     }
  344.     public function getShippingMode(): ?string
  345.     {
  346.         return $this->shippingMode;
  347.     }
  348.     public function setShippingMode(?string $shippingMode): void
  349.     {
  350.         $this->shippingMode $shippingMode;
  351.     }
  352.     public function getReferenceComm(): ?string
  353.     {
  354.         return $this->referenceComm;
  355.     }
  356.     public function setReferenceComm(?string $referenceComm): void
  357.     {
  358.         $this->referenceComm $referenceComm;
  359.     }
  360. }