Passed
Push — master ( 154e98...8e1d78 )
by Mikołaj
03:58
created

consumeEvent()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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