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

ProductTaxonsBuilder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A consumeEvent() 0 7 1
A __construct() 0 4 1
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\PropertyBuilder\Mapper\ProductTaxonsMapperInterface;
16
use Elastica\Document;
17
use FOS\ElasticaBundle\Event\TransformEvent;
18
use Sylius\Component\Core\Model\ProductInterface;
19
20
final class ProductTaxonsBuilder extends AbstractBuilder
21
{
22
    /**
23
     * @var ProductTaxonsMapperInterface
24
     */
25
    private $productTaxonsMapper;
26
27
    /**
28
     * @var string
29
     */
30
    private $taxonsProperty;
31
32
    /**
33
     * @param ProductTaxonsMapperInterface $productTaxonsMapper
34
     * @param string $taxonsProperty
35
     */
36
    public function __construct(ProductTaxonsMapperInterface $productTaxonsMapper, string $taxonsProperty)
37
    {
38
        $this->productTaxonsMapper = $productTaxonsMapper;
39
        $this->taxonsProperty = $taxonsProperty;
40
    }
41
42
    /**
43
     * @param TransformEvent $event
44
     */
45
    public function consumeEvent(TransformEvent $event): void
46
    {
47
        $this->buildProperty($event, ProductInterface::class,
48
            function (ProductInterface $product, Document $document): void {
49
                $taxons = $this->productTaxonsMapper->mapToUniqueCodes($product);
50
51
                $document->set($this->taxonsProperty, $taxons);
52
            }
53
        );
54
    }
55
}
56