vendor/bitbag/elasticsearch-plugin/src/PropertyBuilder/OptionTaxonsBuilder.php line 49

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.  * another great project.
  6.  * You can find more information about us on https://bitbag.io and write us
  7.  * an email on hello@bitbag.io.
  8.  */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
  11. use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface;
  12. use BitBag\SyliusElasticsearchPlugin\Repository\ProductVariantRepositoryInterface;
  13. use FOS\ElasticaBundle\Event\PostTransformEvent;
  14. use Sylius\Component\Core\Model\ProductInterface;
  15. use Sylius\Component\Product\Model\ProductOptionInterface;
  16. use Sylius\Component\Product\Model\ProductOptionValueInterface;
  17. use Sylius\Component\Resource\Repository\RepositoryInterface;
  18. final class OptionTaxonsBuilder extends AbstractBuilder
  19. {
  20.     private RepositoryInterface $productOptionValueRepository;
  21.     private ProductVariantRepositoryInterface $productVariantRepository;
  22.     private ProductTaxonsMapperInterface $productTaxonsMapper;
  23.     private string $taxonsProperty;
  24.     private array $excludedOptions;
  25.     public function __construct(
  26.         RepositoryInterface $productOptionValueRepository,
  27.         ProductVariantRepositoryInterface $productVariantRepository,
  28.         ProductTaxonsMapperInterface $productTaxonsMapper,
  29.         string $taxonsProperty,
  30.         array $excludedOptions = []
  31.     ) {
  32.         $this->productOptionValueRepository $productOptionValueRepository;
  33.         $this->productVariantRepository $productVariantRepository;
  34.         $this->productTaxonsMapper $productTaxonsMapper;
  35.         $this->taxonsProperty $taxonsProperty;
  36.         $this->excludedOptions $excludedOptions;
  37.     }
  38.     public function consumeEvent(PostTransformEvent $event): void
  39.     {
  40.         $documentProductOption $event->getObject();
  41.         if (!$documentProductOption instanceof ProductOptionInterface
  42.             || in_array($documentProductOption->getCode(), $this->excludedOptionstrue)
  43.         ) {
  44.             return;
  45.         }
  46.         $document $event->getDocument();
  47.         $optionValues $this->productOptionValueRepository->findAll();
  48.         $taxons = [];
  49.         /** @var ProductOptionValueInterface $optionValue */
  50.         foreach ($optionValues as $optionValue) {
  51.             $option $optionValue->getOption();
  52.             $productVariant $this->productVariantRepository->findOneByOptionValue($optionValue);
  53.             if (null === $productVariant) {
  54.                 continue;
  55.             }
  56.             /** @var ProductInterface $product */
  57.             $product $productVariant->getProduct();
  58.             if ($documentProductOption === $option && $product->isEnabled()) {
  59.                 $taxons array_merge($taxons$this->productTaxonsMapper->mapToUniqueCodes($product));
  60.             }
  61.         }
  62.         $document->set($this->taxonsPropertyarray_values(array_unique($taxons)));
  63.     }
  64. }