Passed
Pull Request — master (#69)
by Manuele
04:01
created

ProductDescriptionBuilder::consumeEvent()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
6
7
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
8
use Elastica\Document;
9
use FOS\ElasticaBundle\Event\TransformEvent;
10
use Sylius\Component\Core\Model\ProductInterface;
11
use Sylius\Component\Core\Model\ProductTranslationInterface;
12
13
final class ProductDescriptionBuilder extends AbstractBuilder
14
{
15
    /**
16
     * @var ConcatedNameResolverInterface
17
     */
18
    private $productDescriptionNameResolver;
19
20
    public function __construct(ConcatedNameResolverInterface $productDescriptionNameResolver)
21
    {
22
        $this->productDescriptionNameResolver = $productDescriptionNameResolver;
23
    }
24
25
    public function consumeEvent(TransformEvent $event): void
26
    {
27
        $this->buildProperty($event, ProductInterface::class,
28
            function (ProductInterface $product, Document $document): void {
29
                /** @var ProductTranslationInterface $productTranslation */
30
                foreach ($product->getTranslations() as $productTranslation) {
31
                    $propertyName = $this->productDescriptionNameResolver->resolvePropertyName(
32
                        $productTranslation->getLocale()
33
                    );
34
                    $document->set($propertyName, $productTranslation->getDescription());
35
                }
36
            }
37
        );
38
    }
39
}
40