<?php
declare(strict_types=1);
namespace BitBag\OpenMarketplace\Component\Taxonomy\Entity;
use BitBag\OpenMarketplace\Component\SEO\Entity\SEOContent;
use BitBag\OpenMarketplace\Component\SEO\Entity\SEOContentTranslationInterface;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableInterface;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableTrait;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetSubjectInterface;
use Dedi\SyliusSEOPlugin\Entity\SEOContentInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\Taxon as BaseTaxon;
use Sylius\Component\Taxonomy\Model\TaxonInterface as BaseTaxonInterface;
class Taxon extends BaseTaxon implements TaxonInterface, ReferenceableInterface
{
use ReferenceableTrait {
getMetadataTitle as getBaseMetadataTitle;
getMetadataDescription as getBaseMetadataDescription;
getOpenGraphMetadataImage as getBaseOpenGraphMetadataImage;
}
/**
* @var Collection<int,TaxonAttributeInterface>
*
* @psalm-var Collection<array-key, TaxonAttributeInterface>
*/
private Collection $attributes;
private bool $buyable = false;
public function __construct(
) {
parent::__construct();
$this->attributes = new ArrayCollection();
}
public function getAttributes(): Collection
{
return $this->attributes;
}
public function addAttribute(TaxonAttributeInterface $taxonAttribute): void
{
if (!$this->taxonContainsAttribute($taxonAttribute)) {
$taxonAttribute->setTaxon($this);
$this->attributes->add($taxonAttribute);
}
}
public function removeAttribute(TaxonAttributeInterface $taxonAttribute): void
{
if ($this->attributes->contains($taxonAttribute)) {
$this->attributes->removeElement($taxonAttribute);
$taxonAttribute->setTaxon(null);
}
}
public function getRequiredAttributes(): Collection
{
return $this->attributes->filter(
function (TaxonAttributeInterface $taxonAttribute) {
return $taxonAttribute->isRequired();
}
);
}
public function isBuyable(): bool
{
return $this->buyable;
}
public function setBuyable(bool $buyable): void
{
$this->buyable = $buyable;
}
public function taxonContainsAttribute(
TaxonAttributeInterface $taxonAttribute
): bool {
foreach ($this->attributes as $attribute) {
if ($attribute->getProductAttribute() === $taxonAttribute->getProductAttribute()) {
return true;
}
}
return false;
}
public function getRichSnippetSubjectParent(): ?RichSnippetSubjectInterface
{
/** @var ?TaxonInterface $parent */
$parent = $this->getParent();
return $parent;
}
public function getRichSnippetSubjectType(): string
{
return 'taxon';
}
public function getEnabledChildren(): Collection
{
return $this->children->filter(
/** @var TaxonInterface $taxon */
function (BaseTaxonInterface $taxon) {
return $taxon->isEnabled();
}
);
}
public function getMetadataTitle(): ?string
{
/** @var Taxon|null $referenceableContent */
$referenceableContent = $this->getReferenceableContent();
if (null !== $referenceableContent) {
$translation = $referenceableContent->getTranslation();
if (null === $referenceableContent->getMetadataTitle() && null !== $translation) {
$locale = $translation->getLocale();
if (null !== $locale) {
$this->setCurrentLocale($locale);
}
return $this->getName();
}
}
return $this->getBaseMetadataTitle();
}
public function getMetadataDescription(): ?string
{
/** @var Taxon|null $referenceableContent */
$referenceableContent = $this->getReferenceableContent();
if (null !== $referenceableContent) {
$translation = $referenceableContent->getTranslation();
if (null === $referenceableContent->getMetadataDescription() && null !== $translation) {
$locale = $translation->getLocale();
if (null !== $locale) {
$this->setCurrentLocale($locale);
}
return null;
}
}
return $this->getBaseMetadataDescription();
}
public function getOpenGraphMetadataImage(): ?string
{
if (
null === $this->getReferenceableContent()->getOpenGraphMetadataImage() &&
null != $this->getImages()->first()
) {
return $this->getImages()->first()->getPath();
}
return $this->getBaseOpenGraphMetadataImage();
}
public function getKeywords(): ?string
{
/** @var SEOContentInterface $referenceableContent */
$referenceableContent = $this->getReferenceableContent();
/** @var SEOContentTranslationInterface $translation */
$translation = $referenceableContent->getTranslation();
return $translation->getKeywords();
}
public function getEnabledChildrenByTranslationName(?string $locale): array
{
$children = $this->children->filter(
/** @var TaxonInterface $taxon */
function (BaseTaxonInterface $taxon) use ($locale) {
return $taxon->isEnabled() && null !== $taxon->getTranslation($locale);
}
)->toArray();
usort($children, function (BaseTaxonInterface $a, BaseTaxonInterface $b) use ($locale) {
return $a->getTranslation($locale)->getName() <=> $b->getTranslation($locale)->getName();
});
return $children;
}
protected function createReferenceableContent(): ReferenceableInterface
{
return new SEOContent();
}
}