<?php
declare(strict_types=1);
namespace App\Entity\Product;
use App\Entity\Brand\Brand;
use App\Entity\Supplier\Supplier;
use Arobases\SyliusTransporterLabelGenerationPlugin\Entity\HsCodeTrait;
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundlesAwareInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundlesAwareTrait;
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableInterface;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableTrait;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetProductSubjectTrait;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetSubjectInterface;
use Dedi\SyliusSEOPlugin\Entity\SEOContent;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Setono\SyliusCalloutPlugin\Model\CalloutsAwareTrait as SetonoSyliusCalloutCalloutsAwareTrait;
use Setono\SyliusCalloutPlugin\Model\ProductInterface as SetonoSyliusCalloutProductInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\Product as BaseProduct;
use Sylius\Component\Core\Model\ProductImageInterface;
use Sylius\Component\Product\Model\ProductTranslationInterface;
use Sylius\Component\Resource\Model\ArchivableTrait;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_product")
*/
class Product extends BaseProduct implements SetonoSyliusCalloutProductInterface, ReferenceableInterface, RichSnippetSubjectInterface, ProductBundlesAwareInterface, ProductInterface
{
use RichSnippetProductSubjectTrait;
use ReferenceableTrait;
use ArchivableTrait;
use HsCodeTrait;
use SetonoSyliusCalloutCalloutsAwareTrait {
SetonoSyliusCalloutCalloutsAwareTrait::__construct as private __calloutsTraitConstruct;
}
use ProductBundlesAwareTrait;
/** @var string|null */
protected $variantSelectionMethod = self::VARIANT_SELECTION_MATCH;
public function __construct()
{
$this->__calloutsTraitConstruct();
parent::__construct();
}
/**
* @ORM\Column(name="old_id", type="integer", nullable=true)
*/
private ?int $oldId;
/**
* @var ProductBundleInterface
* @ORM\OneToOne(
* targetEntity="BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface",
* mappedBy="product",
* cascade={"all"},)
*/
protected $productBundle;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Brand\Brand", cascade={"persist"}, fetch="EAGER", inversedBy="products")
* @ORM\JoinColumn(name="brand_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected ?Brand $brand = null;
/**
* @var \DateTimeInterface|null
*
* @ORM\Column(name="archived_at", type="datetime", nullable=true)
*/
protected $archivedAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Supplier\Supplier", inversedBy="Product")
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
protected ?Supplier $supplier = null;
/**
* @ORM\Column(name="is_digital_product", type="boolean", options={"default" : 0} )
*/
private bool $isDigitalProduct = false;
protected function createReferenceableContent(): ReferenceableInterface
{
return new SEOContent();
}
protected function createTranslation(): ProductTranslationInterface
{
return new ProductTranslation();
}
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
public function setSupplier(?Supplier $supplier): void
{
$this->supplier = $supplier;
}
public function getRichSnippetSubjectParent(): ?RichSnippetSubjectInterface
{
return $this->getMainTaxon();
}
public function getRichSnippetSubjectType(): string
{
return 'product';
}
public function getBrand(): ?Brand
{
return $this->brand;
}
public function setBrand(?Brand $brand): void
{
$this->brand = $brand;
}
/**
* @return bool
*/
public function isDigitalProduct(): bool
{
return $this->isDigitalProduct;
}
/**
* @param bool $isDigitalProduct
*/
public function setIsDigitalProduct(bool $isDigitalProduct): void
{
$this->isDigitalProduct = $isDigitalProduct;
}
/**
* @return int|null
*/
public function getOldId(): ?int
{
return $this->oldId;
}
/**
* @param int|null $oldId
*/
public function setOldId(?int $oldId): void
{
$this->oldId = $oldId;
}
public function getImagesWithoutOneType(?string $type = "digital") : Collection
{
return $this->images->filter(function (ImageInterface $image) use ($type): bool {
return $image->getType() !== $type && $image->getType() !== "ia";
});
}
public function getOneImageWithType(?string $type = 'ia'): ?ImageInterface
{
$image = $this->images->filter(function (ImageInterface $image) use ($type): bool {
return $image->getType() === $type;
})->first();
return $image ?: null;
}
public function getImagesByPosition(): array
{
$images = $this->getImages()->toArray();
usort($images, static function (ProductImageInterface $a, ProductImageInterface $b): int {
return $a->getPosition() <=> $b->getPosition();
});
return $images;
}
}