Passed
Push — master ( 1a7a28...c3773f )
by Mikołaj
03:50
created

ProductNameBuilder::consumeEvent()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
14
15
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
16
use Elastica\Document;
17
use FOS\ElasticaBundle\Event\TransformEvent;
18
use Sylius\Component\Core\Model\ProductInterface;
19
use Sylius\Component\Core\Model\ProductTranslationInterface;
20
21
final class ProductNameBuilder extends AbstractBuilder
22
{
23
    /**
24
     * @var ConcatedNameResolverInterface
25
     */
26
    private $productNameNameResolver;
27
28
    /**
29
     * @param ConcatedNameResolverInterface $productNameNameResolver
30
     */
31
    public function __construct(ConcatedNameResolverInterface $productNameNameResolver)
32
    {
33
        $this->productNameNameResolver = $productNameNameResolver;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function consumeEvent(TransformEvent $event): void
40
    {
41
        $this->buildProperty($event, ProductInterface::class,
42
            function (ProductInterface $product, Document $document): void {
43
                /** @var ProductTranslationInterface $productTranslation */
44
                foreach ($product->getTranslations() as $productTranslation) {
45
                    $propertyName = $this->productNameNameResolver->resolvePropertyName($productTranslation->getLocale());
46
47
                    $document->set($propertyName, $productTranslation->getName());
48
                }
49
            }
50
        );
51
    }
52
}
53