Passed
Push — master ( 59f96f...9edaa9 )
by Mikołaj
05:36
created

AttributeTaxonsBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C buildProperty() 0 28 7
A __construct() 0 9 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\Attribute\Model\AttributeInterface;
17
use Sylius\Component\Core\Model\ProductInterface;
18
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
19
20
final class AttributeTaxonsBuilder extends AbstractBuilder
21
{
22
    /**
23
     * @var ProductRepositoryInterface
24
     */
25
    private $productRepository;
26
27
    /**
28
     * @var string
29
     */
30
    private $attributeProperty;
31
32
    /**
33
     * @var string
34
     */
35
    private $taxonsProperty;
36
37
    /**
38
     * @param ProductRepositoryInterface $productRepository
39
     * @param string $attributeProperty
40
     * @param string $taxonsProperty
41
     */
42
    public function __construct(
43
        ProductRepositoryInterface $productRepository,
44
        string $attributeProperty,
45
        string $taxonsProperty
46
    )
47
    {
48
        $this->productRepository = $productRepository;
49
        $this->attributeProperty = $attributeProperty;
50
        $this->taxonsProperty = $taxonsProperty;
51
    }
52
53
    /**
54
     * @param TransformEvent $event
55
     */
56
    public function buildProperty(TransformEvent $event): void
57
    {
58
        /** @var AttributeInterface $documentAttribute */
59
        $documentAttribute = $event->getObject();
60
61
        if (!$documentAttribute instanceof AttributeInterface) {
0 ignored issues
show
introduced by
$documentAttribute is always a sub-type of Sylius\Component\Attribu...odel\AttributeInterface.
Loading history...
62
            return;
63
        }
64
65
        $document = $event->getDocument();
66
        $products = $this->productRepository->findAll();
67
        $taxons = [];
68
69
        /** @var ProductInterface $product */
70
        foreach ($products as $product) {
71
            foreach ($product->getAttributes() as $attributeValue) {
72
                if ($documentAttribute === $attributeValue->getAttribute()) {
73
                    foreach ($product->getTaxons() as $taxon) {
74
                        $code = $taxon->getCode();
75
                        if (!in_array($code, $taxons)) {
76
                            $taxons[] = $code;
77
                        }
78
                    }
79
                }
80
            }
81
        }
82
83
        $document->set($this->taxonsProperty, $taxons);
84
    }
85
}
86