src/Component/Product/Entity/Product.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * You can find more information about us on https://bitbag.io and write us
  6.  * an email on hello@bitbag.io.
  7.  */
  8. declare(strict_types=1);
  9. namespace BitBag\OpenMarketplace\Component\Product\Entity;
  10. use BitBag\OpenMarketplace\Component\Core\Common\Model\Datasheet\DatasheetAwareTrait;
  11. use BitBag\OpenMarketplace\Component\Product\Model\ProductTrait;
  12. use BitBag\OpenMarketplace\Component\SellerPlan\Entity\FeaturedInterface;
  13. use BitBag\OpenMarketplace\Component\SellerPlan\Entity\SellerPlanInterface;
  14. use BitBag\OpenMarketplace\Component\SEO\Entity\SEOContent;
  15. use BitBag\OpenMarketplace\Component\SEO\Entity\SEOContentTranslationInterface;
  16. use BitBag\OpenMarketplace\Component\Tag\Entity\ProductTagInterface;
  17. use BitBag\OpenMarketplace\Component\Tag\Entity\SearchTermInterface;
  18. use BitBag\OpenMarketplace\Component\Taxonomy\Entity\TaxonInterface;
  19. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableInterface;
  20. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableTrait;
  21. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetProductSubjectTrait;
  22. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetSubjectInterface;
  23. use Dedi\SyliusSEOPlugin\Entity\SEOContentInterface;
  24. use Doctrine\Common\Collections\ArrayCollection;
  25. use Doctrine\Common\Collections\Collection;
  26. use Sylius\Component\Addressing\Model\CountryInterface;
  27. use Sylius\Component\Core\Model\Product as BaseProduct;
  28. class Product extends BaseProduct implements ProductInterface
  29. {
  30.     use ProductTrait,
  31.         ReferenceableTrait,
  32.         RichSnippetProductSubjectTrait,
  33.         DatasheetAwareTrait
  34.     ;
  35.     private Collection $productTags;
  36.     private Collection $searchTerms;
  37.     private bool $featured false;
  38.     private ?SellerPlanInterface $sellerPlan null;
  39.     private Collection $countries;
  40.     protected Collection $datasheets;
  41.     private Collection $countryAdjustments;
  42.     private bool $rfqRestricted false;
  43.     private bool $inquiryRestricted false;
  44.     private ?FeaturedInterface $featuredProduct null;
  45.     public function __construct(
  46.     ) {
  47.         parent::__construct();
  48.         $this->productTags = new ArrayCollection();
  49.         $this->countries = new ArrayCollection();
  50.         $this->searchTerms = new ArrayCollection();
  51.         $this->datasheets = new ArrayCollection();
  52.         $this->countryAdjustments = new ArrayCollection();
  53.     }
  54.     public function getProductTags(): Collection
  55.     {
  56.         return $this->productTags;
  57.     }
  58.     public function addProductTag(ProductTagInterface $productTag): void
  59.     {
  60.         if (!$this->hasProductTag($productTag)) {
  61.             $this->productTags->add($productTag);
  62.             $productTag->setProduct($this);
  63.         }
  64.     }
  65.     public function removeProductTag(ProductTagInterface $productTag): void
  66.     {
  67.         if ($this->hasProductTag($productTag)) {
  68.             $this->productTags->removeElement($productTag);
  69.             $productTag->setProduct(null);
  70.         }
  71.     }
  72.     public function hasProductTag(ProductTagInterface $productTag): bool
  73.     {
  74.         return $this->productTags->contains($productTag);
  75.     }
  76.     public function isFeatured(): bool
  77.     {
  78.         return $this->featured || $this->getFeaturedProduct()?->isActive();
  79.     }
  80.     public function setFeatured(bool $featured): void
  81.     {
  82.         $this->featured $featured;
  83.     }
  84.     public function getSellerPlan(): ?SellerPlanInterface
  85.     {
  86.         return $this->sellerPlan;
  87.     }
  88.     public function setSellerPlan(?SellerPlanInterface $sellerPlan): void
  89.     {
  90.         $this->sellerPlan $sellerPlan;
  91.     }
  92.     public function getCountries(): Collection
  93.     {
  94.         return $this->countries;
  95.     }
  96.     public function addCountry(CountryInterface $country): void
  97.     {
  98.         if (!$this->hasCountry($country)) {
  99.             $this->countries->add($country);
  100.         }
  101.     }
  102.     public function removeCountry(CountryInterface $country): void
  103.     {
  104.         if ($this->hasCountry($country)) {
  105.             $this->countries->removeElement($country);
  106.         }
  107.     }
  108.     public function hasCountry(CountryInterface $country): bool
  109.     {
  110.         return $this->countries->contains($country);
  111.     }
  112.     public function setCountries(Collection $countries): void
  113.     {
  114.         $this->countries $countries;
  115.     }
  116.     public function getSearchTerms(): Collection
  117.     {
  118.         return $this->searchTerms;
  119.     }
  120.     public function addSearchTerm(SearchTermInterface $searchTerm): void
  121.     {
  122.         if (!$this->hasSearchTerm($searchTerm)) {
  123.             $this->searchTerms->add($searchTerm);
  124.             $searchTerm->setProduct($this);
  125.         }
  126.     }
  127.     public function removeSearchTerm(SearchTermInterface $searchTerm): void
  128.     {
  129.         if ($this->hasSearchTerm($searchTerm)) {
  130.             $this->searchTerms->removeElement($searchTerm);
  131.             $searchTerm->setProduct(null);
  132.         }
  133.     }
  134.     public function hasSearchTerm(SearchTermInterface $searchTerm): bool
  135.     {
  136.         return $this->searchTerms->contains($searchTerm);
  137.     }
  138.     public function isRfqRestricted(): bool
  139.     {
  140.         return $this->rfqRestricted;
  141.     }
  142.     public function setRfqRestricted(bool $rfqRestricted): void
  143.     {
  144.         $this->rfqRestricted $rfqRestricted;
  145.     }
  146.     public function isInquiryRestricted(): bool
  147.     {
  148.         return $this->inquiryRestricted;
  149.     }
  150.     public function setInquiryRestricted(bool $inquiryRestricted): void
  151.     {
  152.         $this->inquiryRestricted $inquiryRestricted;
  153.     }
  154.     protected function createReferenceableContent(): ReferenceableInterface
  155.     {
  156.         return new SEOContent();
  157.     }
  158.     public function getRichSnippetSubjectParent(): ?RichSnippetSubjectInterface
  159.     {
  160.         /** @var ?TaxonInterface $mainTaxon */
  161.         $mainTaxon $this->getMainTaxon();
  162.         return $mainTaxon;
  163.     }
  164.     public function getRichSnippetSubjectType(): string
  165.     {
  166.         return 'product';
  167.     }
  168.     public function getMetadataTitle(): ?string
  169.     {
  170.         /** @var SEOContent|null $content */
  171.         $content $this->getReferenceableContent();
  172.         if (null === $content) {
  173.             return $this->getName();
  174.         }
  175.         $locale $content->getCurrentLocale();
  176.         if (null !== $locale) {
  177.             $exactTranslation $content->getTranslations()->get($locale);
  178.             if (null !== $exactTranslation && !empty($exactTranslation->getMetadataTitle())) {
  179.                 return $exactTranslation->getMetadataTitle();
  180.             }
  181.             // No locale-specific SEO title: fall back to the product's own translated name
  182.             $this->setCurrentLocale($locale);
  183.         }
  184.         return $this->getName();
  185.     }
  186.     public function getCountryAdjustments(): Collection
  187.     {
  188.         return $this->countryAdjustments;
  189.     }
  190.     public function addCountryAdjustment(ProductCountryAdjustmentInterface $countryAdjustment): void
  191.     {
  192.         if (!$this->hasCountryAdjustment($countryAdjustment)) {
  193.             $this->countryAdjustments->add($countryAdjustment);
  194.             $countryAdjustment->setProduct($this);
  195.         }
  196.     }
  197.     public function removeCountryAdjustment(ProductCountryAdjustmentInterface $countryAdjustment): void
  198.     {
  199.         if ($this->hasCountryAdjustment($countryAdjustment)) {
  200.             $this->countryAdjustments->removeElement($countryAdjustment);
  201.             $countryAdjustment->setProduct(null);
  202.         }
  203.     }
  204.     public function hasCountryAdjustment(ProductCountryAdjustmentInterface $countryAdjustment): bool
  205.     {
  206.         return $this->countryAdjustments->contains($countryAdjustment);
  207.     }
  208.     public function getKeywords(): ?string
  209.     {
  210.         /** @var SEOContentInterface $referenceableContent */
  211.         $referenceableContent $this->getReferenceableContent();
  212.         /** @var SEOContentTranslationInterface $translation */
  213.         $translation $referenceableContent->getTranslation();
  214.         return $translation->getKeywords();
  215.     }
  216.     public function getFeaturedProduct(): ?FeaturedInterface
  217.     {
  218.         return $this->featuredProduct;
  219.     }
  220.     public function setFeaturedProduct(?FeaturedInterface $featuredProduct): void
  221.     {
  222.         $this->featuredProduct $featuredProduct;
  223.     }
  224. }