src/Component/Elasticsearch/PropertyBuilder/ShortDescriptionBuilder.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\Elasticsearch\PropertyBuilder;
  4. use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\AbstractBuilder;
  5. use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
  6. use Elastica\Document;
  7. use FOS\ElasticaBundle\Event\PostTransformEvent;
  8. use Sylius\Component\Core\Model\ProductInterface;
  9. use Sylius\Component\Core\Model\ProductTranslationInterface;
  10. final class ShortDescriptionBuilder extends AbstractBuilder
  11. {
  12.     public function __construct(
  13.         private ConcatedNameResolverInterface $productShortDescriptionNameResolver
  14.     ) {
  15.     }
  16.     public function consumeEvent(PostTransformEvent $event): void
  17.     {
  18.         $this->buildProperty(
  19.             $event,
  20.             ProductInterface::class,
  21.             function (ProductInterface $productDocument $document): void {
  22.                 /** @var ProductTranslationInterface $productTranslation */
  23.                 foreach ($product->getTranslations() as $productTranslation) {
  24.                     $propertyName $this->productShortDescriptionNameResolver->resolvePropertyName(
  25.                         $productTranslation->getLocale() ?? 'en'
  26.                     );
  27.                     $document->set($propertyName$this->stripHtmlTags($productTranslation->getShortDescription()));
  28.                 }
  29.             }
  30.         );
  31.     }
  32.     private function stripHtmlTags(?string $description): ?string
  33.     {
  34.         if (null === $description) {
  35.             return null;
  36.         }
  37.         return html_entity_decode(
  38.             strip_tags($description),
  39.             \ENT_QUOTES,
  40.             'UTF-8'
  41.         );
  42.     }
  43. }