ProductTaxonsBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
12
13
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface;
14
use Elastica\Document;
15
use FOS\ElasticaBundle\Event\PostTransformEvent;
16
use Sylius\Component\Core\Model\ProductInterface;
17
18
final class ProductTaxonsBuilder extends AbstractBuilder
19
{
20
    /** @var ProductTaxonsMapperInterface */
21
    private $productTaxonsMapper;
22
23
    /** @var string */
24
    private $taxonsProperty;
25
26
    public function __construct(ProductTaxonsMapperInterface $productTaxonsMapper, string $taxonsProperty)
27
    {
28
        $this->productTaxonsMapper = $productTaxonsMapper;
29
        $this->taxonsProperty = $taxonsProperty;
30
    }
31
32
    public function consumeEvent(PostTransformEvent $event): void
33
    {
34
        $this->buildProperty(
35
            $event,
36
            ProductInterface::class,
37
            function (ProductInterface $product, Document $document): void {
38
                $taxons = $this->productTaxonsMapper->mapToUniqueCodes($product);
39
40
                $document->set($this->taxonsProperty, $taxons);
41
            }
42
        );
43
    }
44
}
45