src/Component/Taxonomy/Entity/Taxon.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\Taxonomy\Entity;
  4. use BitBag\OpenMarketplace\Component\SEO\Entity\SEOContent;
  5. use BitBag\OpenMarketplace\Component\SEO\Entity\SEOContentTranslationInterface;
  6. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableInterface;
  7. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableTrait;
  8. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetSubjectInterface;
  9. use Dedi\SyliusSEOPlugin\Entity\SEOContentInterface;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Sylius\Component\Core\Model\Taxon as BaseTaxon;
  13. use Sylius\Component\Taxonomy\Model\TaxonInterface as BaseTaxonInterface;
  14. class Taxon extends BaseTaxon implements TaxonInterfaceReferenceableInterface
  15. {
  16.     use ReferenceableTrait {
  17.         getMetadataTitle as getBaseMetadataTitle;
  18.         getMetadataDescription as getBaseMetadataDescription;
  19.         getOpenGraphMetadataImage as getBaseOpenGraphMetadataImage;
  20.     }
  21.     /**
  22.      * @var Collection<int,TaxonAttributeInterface>
  23.      *
  24.      * @psalm-var Collection<array-key, TaxonAttributeInterface>
  25.      */
  26.     private Collection $attributes;
  27.     private bool $buyable false;
  28.     public function __construct(
  29.     ) {
  30.         parent::__construct();
  31.         $this->attributes = new ArrayCollection();
  32.     }
  33.     public function getAttributes(): Collection
  34.     {
  35.         return $this->attributes;
  36.     }
  37.     public function addAttribute(TaxonAttributeInterface $taxonAttribute): void
  38.     {
  39.         if (!$this->taxonContainsAttribute($taxonAttribute)) {
  40.             $taxonAttribute->setTaxon($this);
  41.             $this->attributes->add($taxonAttribute);
  42.         }
  43.     }
  44.     public function removeAttribute(TaxonAttributeInterface $taxonAttribute): void
  45.     {
  46.         if ($this->attributes->contains($taxonAttribute)) {
  47.             $this->attributes->removeElement($taxonAttribute);
  48.             $taxonAttribute->setTaxon(null);
  49.         }
  50.     }
  51.     public function getRequiredAttributes(): Collection
  52.     {
  53.         return $this->attributes->filter(
  54.             function (TaxonAttributeInterface $taxonAttribute) {
  55.                 return $taxonAttribute->isRequired();
  56.             }
  57.         );
  58.     }
  59.     public function isBuyable(): bool
  60.     {
  61.         return $this->buyable;
  62.     }
  63.     public function setBuyable(bool $buyable): void
  64.     {
  65.         $this->buyable $buyable;
  66.     }
  67.     public function taxonContainsAttribute(
  68.         TaxonAttributeInterface $taxonAttribute
  69.     ): bool {
  70.         foreach ($this->attributes as $attribute) {
  71.             if ($attribute->getProductAttribute() === $taxonAttribute->getProductAttribute()) {
  72.                 return true;
  73.             }
  74.         }
  75.         return false;
  76.     }
  77.     public function getRichSnippetSubjectParent(): ?RichSnippetSubjectInterface
  78.     {
  79.         /** @var ?TaxonInterface $parent */
  80.         $parent $this->getParent();
  81.         return $parent;
  82.     }
  83.     public function getRichSnippetSubjectType(): string
  84.     {
  85.         return 'taxon';
  86.     }
  87.     public function getEnabledChildren(): Collection
  88.     {
  89.         return $this->children->filter(
  90.             /** @var TaxonInterface $taxon */
  91.             function (BaseTaxonInterface $taxon) {
  92.                 return $taxon->isEnabled();
  93.             }
  94.         );
  95.     }
  96.     public function getMetadataTitle(): ?string
  97.     {
  98.         /** @var Taxon|null $referenceableContent */
  99.         $referenceableContent $this->getReferenceableContent();
  100.         if (null !== $referenceableContent) {
  101.             $translation $referenceableContent->getTranslation();
  102.             if (null === $referenceableContent->getMetadataTitle() && null !== $translation) {
  103.                 $locale $translation->getLocale();
  104.                 if (null !== $locale) {
  105.                     $this->setCurrentLocale($locale);
  106.                 }
  107.                 return $this->getName();
  108.             }
  109.         }
  110.         return $this->getBaseMetadataTitle();
  111.     }
  112.     public function getMetadataDescription(): ?string
  113.     {
  114.         /** @var Taxon|null $referenceableContent */
  115.         $referenceableContent $this->getReferenceableContent();
  116.         if (null !== $referenceableContent) {
  117.             $translation $referenceableContent->getTranslation();
  118.             if (null === $referenceableContent->getMetadataDescription() && null !== $translation) {
  119.                 $locale $translation->getLocale();
  120.                 if (null !== $locale) {
  121.                     $this->setCurrentLocale($locale);
  122.                 }
  123.                 return null;
  124.             }
  125.         }
  126.         return $this->getBaseMetadataDescription();
  127.     }
  128.     public function getOpenGraphMetadataImage(): ?string
  129.     {
  130.         if (
  131.             null === $this->getReferenceableContent()->getOpenGraphMetadataImage() &&
  132.             null != $this->getImages()->first()
  133.         ) {
  134.             return $this->getImages()->first()->getPath();
  135.         }
  136.         return $this->getBaseOpenGraphMetadataImage();
  137.     }
  138.     public function getKeywords(): ?string
  139.     {
  140.         /** @var SEOContentInterface $referenceableContent */
  141.         $referenceableContent $this->getReferenceableContent();
  142.         /** @var SEOContentTranslationInterface $translation */
  143.         $translation $referenceableContent->getTranslation();
  144.         return $translation->getKeywords();
  145.     }
  146.     public function getEnabledChildrenByTranslationName(?string $locale): array
  147.     {
  148.         $children $this->children->filter(
  149.             /** @var TaxonInterface $taxon */
  150.             function (BaseTaxonInterface $taxon) use ($locale) {
  151.                 return $taxon->isEnabled() && null !== $taxon->getTranslation($locale);
  152.             }
  153.         )->toArray();
  154.         usort($children, function (BaseTaxonInterface $aBaseTaxonInterface $b) use ($locale) {
  155.             return $a->getTranslation($locale)->getName() <=> $b->getTranslation($locale)->getName();
  156.         });
  157.         return $children;
  158.     }
  159.     protected function createReferenceableContent(): ReferenceableInterface
  160.     {
  161.         return new SEOContent();
  162.     }
  163. }