Passed
Push — master ( 846ac7...e3f277 )
by Mikołaj
04:36
created

ProductTaxonsPropertyBuilder::buildProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 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 FOS\ElasticaBundle\Event\TransformEvent;
16
use Sylius\Component\Core\Model\ProductInterface;
17
18
final class ProductTaxonsPropertyBuilder implements PropertyBuilderInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private $taxonsProperty;
24
25
    /**
26
     * @param string $taxonsProperty
27
     */
28
    public function __construct(string $taxonsProperty)
29
    {
30
        $this->taxonsProperty = $taxonsProperty;
31
    }
32
33
    /**
34
     * @param TransformEvent $event
35
     */
36
    public function buildProperty(TransformEvent $event): void
37
    {
38
        $product = $event->getObject();
39
40
        if (!$product instanceof ProductInterface) {
41
            return;
42
        }
43
44
        $taxons = [];
45
46
        /** @var ProductInterface $product */
47
        foreach ($product->getTaxons() as $taxon) {
48
            $taxons[] = $taxon->getCode();
49
        }
50
51
        $event->getDocument()->set($this->taxonsProperty, $taxons);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public static function getSubscribedEvents(): array
58
    {
59
        return [
60
            TransformEvent::POST_TRANSFORM => 'buildProperty',
61
        ];
62
    }
63
}
64