<?php
declare(strict_types=1);
namespace App\Entity\Product;
use App\Entity\PackSample;
use App\Enum\ProductCode;
use DateInterval;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JoppeDc\SyliusBetterSeoPlugin\Entity\HasSeoInterface;
use JoppeDc\SyliusBetterSeoPlugin\Entity\Traits\SeoTrait;
use Setono\SyliusGiftCardPlugin\Model\ProductInterface as SetonoSyliusGiftCardProductInterface;
use Setono\SyliusGiftCardPlugin\Model\ProductTrait as SetonoSyliusGiftCardProductTrait;
use Sylius\Component\Core\Model\Product as BaseProduct;
use Sylius\Component\Product\Model\ProductTranslationInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use function get_object_vars;
use function in_array;
use function rtrim;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_product")
*/
class Product extends BaseProduct implements SetonoSyliusGiftCardProductInterface, HasSeoInterface, ProductInterface
{
use SetonoSyliusGiftCardProductTrait;
use SeoTrait;
/** @ORM\Column(name="aws_pack_link", type="string", length=255, nullable=true) */
public string $awsPackLink;
//awsPackFileSize -> saving it in MB
/** @ORM\Column(name="aws_pack_file_size", type="text", length=500,nullable=true) */
private string|null $awsPackFileSize = null;
// $packFile is only used for the front-end $awsPackLink is the field that actually contains the aws link,
public UploadedFile $packFile;
//for sample form field processing only
public UploadedFile $sampleImage;
public string $sampleTitle;
public string $sampleDescription;
public UploadedFile $sampleFile;
/** @var mixed[] $packSampleArray */
public $packSampleArray;
/**
* @ORM\OneToMany(targetEntity=PackSample::class, mappedBy="product", orphanRemoval=true, cascade={"all"})
*
* @var Collection<int, PackSample>
*/
private Collection $packSamples;
/** @ORM\Column(name="search_enabled", type="boolean", options={"default":true}) */
private bool $searchEnabled = true;
public function __construct()
{
parent::__construct();
$this->packSamples = new ArrayCollection();
}
public function getAwsPackLink(): string
{
return $this->awsPackLink ?? '';
}
public function setAwsPackLink(string $awsPackLink): void
{
$this->awsPackLink = $awsPackLink;
}
protected function createTranslation(): ProductTranslationInterface
{
return new ProductTranslation();
}
/** @return Collection<int, PackSample> */
public function getPackSamples(): Collection
{
return $this->packSamples;
}
public function addPackSample(PackSample $packSample): self
{
if (! $this->packSamples->contains($packSample)) {
$this->packSamples[] = $packSample;
$packSample->setProduct($this);
}
return $this;
}
public function removePackSample(PackSample $packSample): self
{
if ($this->packSamples->removeElement($packSample)) {
// set the owning side to null (unless already changed)
if ($packSample->getProduct() === $this) {
$packSample->setProduct($this);
}
}
return $this;
}
/** @return array<string, mixed> */
public function toArray(): array
{
return get_object_vars($this);
}
public function getAwsPackFileSize(): string|null
{
return $this->awsPackFileSize;
}
public function setAwsPackFileSize(string $awsPackFileSize): self
{
$this->awsPackFileSize = $awsPackFileSize;
return $this;
}
public function isNew(DateInterval|null $dateInterval): bool
{
if (! $dateInterval instanceof DateInterval) {
return false;
}
/** @var DateTime $createdAt */
$createdAt = $this->getCreatedAt();
$endNewDate = clone $createdAt;
$endNewDate->add($dateInterval);
return $endNewDate > new DateTime();
}
public function isSearchEnabled(): bool
{
return $this->searchEnabled;
}
public function setSearchEnabled(bool $searchEnabled): void
{
$this->searchEnabled = $searchEnabled;
}
public function isPhysical(): bool
{
return in_array($this->code, ProductCode::PHYSICAL_PRODUCTS);
}
public function getMainImage(): string
{
return $this->getImagesByType('thumbnail')[0]?->getPath() ?? $this->getImages()[0]?->getPath() ?? '';
}
public function getAttributesAsString(): string
{
$attributes = '';
foreach ($this->getAttributes() as $attribute) {
$attributes .= $attribute->getValue() . ', ';
}
return rtrim($attributes, ', ');
}
}