vendor/bitbag/elasticsearch-plugin/src/PropertyBuilder/ChannelPricingBuilder.php line 30

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\PropertyNameResolver\ConcatedNameResolverInterface;
  12. use Elastica\Document;
  13. use FOS\ElasticaBundle\Event\PostTransformEvent;
  14. use Sylius\Component\Core\Model\ProductInterface;
  15. use Sylius\Component\Core\Model\ProductVariantInterface;
  16. final class ChannelPricingBuilder extends AbstractBuilder
  17. {
  18.     private ConcatedNameResolverInterface $channelPricingNameResolver;
  19.     public function __construct(ConcatedNameResolverInterface $channelPricingNameResolver)
  20.     {
  21.         $this->channelPricingNameResolver $channelPricingNameResolver;
  22.     }
  23.     public function consumeEvent(PostTransformEvent $event): void
  24.     {
  25.         $this->buildProperty(
  26.             $event,
  27.             ProductInterface::class,
  28.             function (ProductInterface $productDocument $document): void {
  29.                 if (=== $product->getVariants()->count()) {
  30.                     return;
  31.                 }
  32.                 /** @var ProductVariantInterface $productVariant */
  33.                 $productVariant $product->getVariants()->first();
  34.                 foreach ($productVariant->getChannelPricings() as $channelPricing) {
  35.                     $propertyName $this->channelPricingNameResolver
  36.                         ->resolvePropertyName($channelPricing->getChannelCode());
  37.                     $document->set($propertyName$channelPricing->getPrice());
  38.                 }
  39.             }
  40.         );
  41.     }
  42. }