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 SetonoSyliusGiftCardProductInterface, HasSeoInterface, ProductInterface
  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.     /** @ORM\Column(name="offered_only", type="boolean", options={"default":false}) */
  54.     private bool $offeredOnly = false;
  55.     public function __construct()
  56.     {
  57.         parent::__construct();
  58.         $this->packSamples = new ArrayCollection();
  59.     }
  60.     public function getAwsPackLink(): string
  61.     {
  62.         return $this->awsPackLink ?? '';
  63.     }
  64.     public function setAwsPackLink(string $awsPackLink): void
  65.     {
  66.         $this->awsPackLink = $awsPackLink;
  67.     }
  68.     protected function createTranslation(): ProductTranslationInterface
  69.     {
  70.         return new ProductTranslation();
  71.     }
  72.     /** @return Collection<int, PackSample> */
  73.     public function getPackSamples(): Collection
  74.     {
  75.         return $this->packSamples;
  76.     }
  77.     public function addPackSample(PackSample $packSample): self
  78.     {
  79.         if (! $this->packSamples->contains($packSample)) {
  80.             $this->packSamples[] = $packSample;
  81.             $packSample->setProduct($this);
  82.         }
  83.         return $this;
  84.     }
  85.     public function removePackSample(PackSample $packSample): self
  86.     {
  87.         if ($this->packSamples->removeElement($packSample)) {
  88.             // set the owning side to null (unless already changed)
  89.             if ($packSample->getProduct() === $this) {
  90.                 $packSample->setProduct($this);
  91.             }
  92.         }
  93.         return $this;
  94.     }
  95.     /** @return array<string, mixed> */
  96.     public function toArray(): array
  97.     {
  98.         return get_object_vars($this);
  99.     }
  100.     public function getAwsPackFileSize(): string|null
  101.     {
  102.         return $this->awsPackFileSize;
  103.     }
  104.     public function setAwsPackFileSize(string $awsPackFileSize): self
  105.     {
  106.         $this->awsPackFileSize = $awsPackFileSize;
  107.         return $this;
  108.     }
  109.     public function isNew(DateInterval|null $dateInterval): bool
  110.     {
  111.         if (! $dateInterval instanceof DateInterval) {
  112.             return false;
  113.         }
  114.         /** @var DateTime $createdAt */
  115.         $createdAt = $this->getCreatedAt();
  116.         $endNewDate = clone $createdAt;
  117.         $endNewDate->add($dateInterval);
  118.         return $endNewDate > new DateTime();
  119.     }
  120.     public function isSearchEnabled(): bool
  121.     {
  122.         return $this->searchEnabled;
  123.     }
  124.     public function setSearchEnabled(bool $searchEnabled): void
  125.     {
  126.         $this->searchEnabled = $searchEnabled;
  127.     }
  128.     public function isPhysical(): bool
  129.     {
  130.         return in_array($this->code, ProductCode::PHYSICAL_PRODUCTS);
  131.     }
  132.     public function getMainImage(): string
  133.     {
  134.         return $this->getImagesByType('thumbnail')[0]?->getPath() ?? $this->getImages()[0]?->getPath() ?? '';
  135.     }
  136.     public function getAttributesAsString(): string
  137.     {
  138.         $attributes = '';
  139.         foreach ($this->getAttributes() as $attribute) {
  140.             $attributes .= $attribute->getValue() . ', ';
  141.         }
  142.         return rtrim($attributes, ', ');
  143.     }
  144.     public function getSeason(): string|null
  145.     {
  146.         return $this->season;
  147.     }
  148.     public function setSeason(string|null $season): self
  149.     {
  150.         $this->season = $season;
  151.         return $this;
  152.     }
  153.     public function isOfferedOnly(): bool
  154.     {
  155.         return $this->offeredOnly;
  156.     }
  157.     public function setOfferedOnly(bool $offeredOnly): self
  158.     {
  159.         $this->offeredOnly = $offeredOnly;
  160.         return $this;
  161.     }
  162. }