<?php
declare(strict_types=1);
namespace App\ElasticSearch\PropertyBuilder;
use App\Entity\Product\ProductInterface;
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\AbstractBuilder;
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use Elastica\Document;
use FOS\ElasticaBundle\Event\PostTransformEvent;
use Sylius\Component\Core\Model\ProductTranslationInterface;
final class MetaKeywordsNameBuilder extends AbstractBuilder
{
public function __construct(private ConcatedNameResolverInterface $productMetaKeywordNameResolver)
{
}
public function consumeEvent(PostTransformEvent $event): void
{
$this->buildProperty(
$event,
\Sylius\Component\Core\Model\ProductInterface::class,
function (ProductInterface $product, Document $document): void {
/** @var ProductTranslationInterface $productTranslation */
foreach ($product->getTranslations() as $productTranslation) {
$locale = $productTranslation->getLocale();
if ($locale === null) {
continue;
}
$propertyName = $this->productMetaKeywordNameResolver
->resolvePropertyName($locale);
$document->set($propertyName, $productTranslation->getMetaKeywords());
}
},
);
}
}