vendor/bitbag/elasticsearch-plugin/src/PropertyBuilder/OptionBuilder.php line 35

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\Formatter\StringFormatterInterface;
  12. use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
  13. use Elastica\Document;
  14. use FOS\ElasticaBundle\Event\PostTransformEvent;
  15. use Sylius\Component\Core\Model\ProductInterface;
  16. final class OptionBuilder extends AbstractBuilder
  17. {
  18.     private ConcatedNameResolverInterface $optionNameResolver;
  19.     private StringFormatterInterface $stringFormatter;
  20.     public function __construct(
  21.         ConcatedNameResolverInterface $optionNameResolver,
  22.         StringFormatterInterface $stringFormatter
  23.     ) {
  24.         $this->optionNameResolver $optionNameResolver;
  25.         $this->stringFormatter $stringFormatter;
  26.     }
  27.     public function consumeEvent(PostTransformEvent $event): void
  28.     {
  29.         $this->buildProperty(
  30.             $event,
  31.             ProductInterface::class,
  32.             function (ProductInterface $productDocument $document): void {
  33.                 $this->resolveProductOptions($product$document);
  34.             }
  35.         );
  36.     }
  37.     private function resolveProductOptions(ProductInterface $productDocument $document): void
  38.     {
  39.         foreach ($product->getVariants() as $productVariant) {
  40.             foreach ($productVariant->getOptionValues() as $productOptionValue) {
  41.                 $optionCode $productOptionValue->getOption()->getCode();
  42.                 $index $this->optionNameResolver->resolvePropertyName($optionCode);
  43.                 $options $document->has($index) ? $document->get($index) : [];
  44.                 $value $this->stringFormatter->formatToLowercaseWithoutSpaces($productOptionValue->getValue());
  45.                 $options[] = $value;
  46.                 $document->set($indexarray_values(array_unique($options)));
  47.             }
  48.         }
  49.     }
  50. }