src/Entity/Product/Product.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Product;
  4. use App\Entity\PackSample;
  5. use App\Enum\ProductCode;
  6. use DateInterval;
  7. use DateTime;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use JoppeDc\SyliusBetterSeoPlugin\Entity\HasSeoInterface;
  12. use JoppeDc\SyliusBetterSeoPlugin\Entity\Traits\SeoTrait;
  13. use Setono\SyliusGiftCardPlugin\Model\ProductInterface as SetonoSyliusGiftCardProductInterface;
  14. use Setono\SyliusGiftCardPlugin\Model\ProductTrait as SetonoSyliusGiftCardProductTrait;
  15. use Sylius\Component\Core\Model\Product as BaseProduct;
  16. use Sylius\Component\Product\Model\ProductTranslationInterface;
  17. use Symfony\Component\HttpFoundation\File\UploadedFile;
  18. use function get_object_vars;
  19. use function in_array;
  20. use function rtrim;
  21. /**
  22.  * @ORM\Entity
  23.  * @ORM\Table(name="sylius_product")
  24.  */
  25. class Product extends BaseProduct implements SetonoSyliusGiftCardProductInterfaceHasSeoInterfaceProductInterface
  26. {
  27.     use SetonoSyliusGiftCardProductTrait;
  28.     use SeoTrait;
  29.     /** @ORM\Column(name="aws_pack_link", type="string", length=255, nullable=true) */
  30.     public string $awsPackLink;
  31.     //awsPackFileSize -> saving it in MB
  32.     /** @ORM\Column(name="aws_pack_file_size", type="text", length=500,nullable=true) */
  33.     private string|null $awsPackFileSize null;
  34.     // $packFile is only used for the front-end $awsPackLink is the field that actually contains the aws link,
  35.     public UploadedFile $packFile;
  36.     //for sample form field processing only
  37.     public UploadedFile $sampleImage;
  38.     public string $sampleTitle;
  39.     public string $sampleDescription;
  40.     public UploadedFile $sampleFile;
  41.     /**  @var mixed[] $packSampleArray */
  42.     public $packSampleArray;
  43.     /**
  44.      * @ORM\OneToMany(targetEntity=PackSample::class, mappedBy="product", orphanRemoval=true, cascade={"all"})
  45.      *
  46.      * @var  Collection<int, PackSample>
  47.      */
  48.     private Collection $packSamples;
  49.     /** @ORM\Column(name="search_enabled", type="boolean", options={"default":true}) */
  50.     private bool $searchEnabled true;
  51.     /** @ORM\Column(name="season", type="string", length=4, nullable=true) */
  52.     private string|null $season null;
  53.     public function __construct()
  54.     {
  55.         parent::__construct();
  56.         $this->packSamples = new ArrayCollection();
  57.     }
  58.     public function getAwsPackLink(): string
  59.     {
  60.         return $this->awsPackLink ?? '';
  61.     }
  62.     public function setAwsPackLink(string $awsPackLink): void
  63.     {
  64.         $this->awsPackLink $awsPackLink;
  65.     }
  66.     protected function createTranslation(): ProductTranslationInterface
  67.     {
  68.         return new ProductTranslation();
  69.     }
  70.     /** @return Collection<int, PackSample> */
  71.     public function getPackSamples(): Collection
  72.     {
  73.         return $this->packSamples;
  74.     }
  75.     public function addPackSample(PackSample $packSample): self
  76.     {
  77.         if (! $this->packSamples->contains($packSample)) {
  78.             $this->packSamples[] = $packSample;
  79.             $packSample->setProduct($this);
  80.         }
  81.         return $this;
  82.     }
  83.     public function removePackSample(PackSample $packSample): self
  84.     {
  85.         if ($this->packSamples->removeElement($packSample)) {
  86.             // set the owning side to null (unless already changed)
  87.             if ($packSample->getProduct() === $this) {
  88.                 $packSample->setProduct($this);
  89.             }
  90.         }
  91.         return $this;
  92.     }
  93.     /** @return array<string, mixed> */
  94.     public function toArray(): array
  95.     {
  96.         return get_object_vars($this);
  97.     }
  98.     public function getAwsPackFileSize(): string|null
  99.     {
  100.         return $this->awsPackFileSize;
  101.     }
  102.     public function setAwsPackFileSize(string $awsPackFileSize): self
  103.     {
  104.         $this->awsPackFileSize $awsPackFileSize;
  105.         return $this;
  106.     }
  107.     public function isNew(DateInterval|null $dateInterval): bool
  108.     {
  109.         if (! $dateInterval instanceof DateInterval) {
  110.             return false;
  111.         }
  112.         /** @var DateTime $createdAt */
  113.         $createdAt $this->getCreatedAt();
  114.         $endNewDate = clone $createdAt;
  115.         $endNewDate->add($dateInterval);
  116.         return $endNewDate > new DateTime();
  117.     }
  118.     public function isSearchEnabled(): bool
  119.     {
  120.         return $this->searchEnabled;
  121.     }
  122.     public function setSearchEnabled(bool $searchEnabled): void
  123.     {
  124.         $this->searchEnabled $searchEnabled;
  125.     }
  126.     public function isPhysical(): bool
  127.     {
  128.         return in_array($this->codeProductCode::PHYSICAL_PRODUCTS);
  129.     }
  130.     public function getMainImage(): string
  131.     {
  132.         return $this->getImagesByType('thumbnail')[0]?->getPath() ?? $this->getImages()[0]?->getPath() ?? '';
  133.     }
  134.     public function getAttributesAsString(): string
  135.     {
  136.         $attributes '';
  137.         foreach ($this->getAttributes() as $attribute) {
  138.             $attributes .= $attribute->getValue() . ', ';
  139.         }
  140.         return rtrim($attributes', ');
  141.     }
  142.     public function getSeason(): string|null
  143.     {
  144.         return $this->season;
  145.     }
  146.     public function setSeason(string|null $season): self
  147.     {
  148.         $this->season $season;
  149.         return $this;
  150.     }
  151. }