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

OptionTaxonsBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C buildProperty() 0 30 8
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\Core\Model\ProductInterface;
17
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
18
use Sylius\Component\Product\Model\ProductOptionInterface;
19
20
final class OptionTaxonsBuilder extends AbstractBuilder
21
{
22
    /**
23
     * @var ProductRepositoryInterface
24
     */
25
    private $productRepository;
26
27
    /**
28
     * @var string
29
     */
30
    private $optionProperty;
31
32
    /**
33
     * @var string
34
     */
35
    private $taxonsProperty;
36
37
    /**
38
     * @param ProductRepositoryInterface $productRepository
39
     * @param string $optionProperty
40
     * @param string $taxonsProperty
41
     */
42
    public function __construct(
43
        ProductRepositoryInterface $productRepository,
44
        string $optionProperty,
45
        string $taxonsProperty
46
    )
47
    {
48
        $this->productRepository = $productRepository;
49
        $this->optionProperty = $optionProperty;
50
        $this->taxonsProperty = $taxonsProperty;
51
    }
52
53
    /**
54
     * @param TransformEvent $event
55
     */
56
    public function buildProperty(TransformEvent $event): void
57
    {
58
        /** @var ProductOptionInterface $documentProductOption */
59
        $documentProductOption = $event->getObject();
60
61
        if (!$documentProductOption instanceof ProductOptionInterface) {
0 ignored issues
show
introduced by
$documentProductOption is always a sub-type of Sylius\Component\Product...\ProductOptionInterface.
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->getVariants() as $productVariant) {
72
                foreach ($productVariant->getOptionValues() as $productOptionValue) {
73
                    if ($documentProductOption === $productOptionValue->getOption()) {
74
                        foreach ($product->getTaxons() as $taxon) {
75
                            $code = $taxon->getCode();
76
                            if (!in_array($code, $taxons)) {
77
                                $taxons[] = $code;
78
                            }
79
                        }
80
                    }
81
                }
82
            }
83
        }
84
85
        $document->set($this->taxonsProperty, $taxons);
86
    }
87
}
88