Passed
Push — master ( cc4e78...1262d4 )
by Damian
04:08
created

ProductShortDescriptionBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 20
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A consumeEvent() 0 10 2
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 ProductShortDescriptionBuilder extends AbstractBuilder
14
{
15
    /** @var ConcatedNameResolverInterface */
16
    private $productShortDescriptionNameResolver;
17
18
    public function __construct(ConcatedNameResolverInterface $productShortDescriptionNameResolver)
19
    {
20
        $this->productShortDescriptionNameResolver = $productShortDescriptionNameResolver;
21
    }
22
23
    public function consumeEvent(TransformEvent $event): void
24
    {
25
        $this->buildProperty($event, ProductInterface::class,
26
            function (ProductInterface $product, Document $document): void {
27
                /** @var ProductTranslationInterface $productTranslation */
28
                foreach ($product->getTranslations() as $productTranslation) {
29
                    $propertyName = $this->productShortDescriptionNameResolver->resolvePropertyName(
30
                        $productTranslation->getLocale()
31
                    );
32
                    $document->set($propertyName, $productTranslation->getShortDescription());
33
                }
34
            }
35
        );
36
    }
37
}
38