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