src/Component/Elasticsearch/PropertyBuilder/VendorPropertyBuilder.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\Elasticsearch\PropertyBuilder;
  4. use BitBag\OpenMarketplace\Component\Product\Entity\ProductInterface;
  5. use BitBag\OpenMarketplace\Component\Vendor\Entity\VendorInterface;
  6. use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\AbstractBuilder;
  7. use Elastica\Document;
  8. use FOS\ElasticaBundle\Event\PostTransformEvent;
  9. use Sylius\Component\Core\Formatter\StringInflector;
  10. final class VendorPropertyBuilder extends AbstractBuilder
  11. {
  12.     public function __construct(
  13.         private string $vendorProperty,
  14.         private string $vendorNameProperty,
  15.     ) {
  16.     }
  17.     public function consumeEvent(PostTransformEvent $event): void
  18.     {
  19.         $this->buildProperty(
  20.             $event,
  21.             ProductInterface::class,
  22.             function (ProductInterface $productDocument $document): void {
  23.                 $vendor $product->getVendor();
  24.                 if (!$vendor instanceof VendorInterface) {
  25.                     return;
  26.                 }
  27.                 $vendorName $vendor->getCompanyName() ?? '';
  28.                 if ('' === $vendorName) {
  29.                     return;
  30.                 }
  31.                 $document->set(
  32.                     $this->vendorProperty,
  33.                     StringInflector::nameToLowercaseCode($vendorName),
  34.                 );
  35.                 $document->set(
  36.                     $this->vendorNameProperty,
  37.                     $vendorName,
  38.                 );
  39.             }
  40.         );
  41.     }
  42. }