Passed
Push — master ( 991ca2...0b0913 )
by Mikołaj
03:26
created

OptionTaxonsPropertyBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
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 OptionTaxonsPropertyBuilder implements PropertyBuilderInterface
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 $documentOptionValue */
59
        $documentProductOption = $event->getObject();
60
        $document = $event->getDocument();
61
        $products = $this->productRepository->findAll();
62
        $taxons = [];
63
64
        /** @var ProductInterface $product */
65
        foreach ($products as $product) {
66
            foreach ($product->getVariants() as $productVariant) {
67
                foreach ($productVariant->getOptionValues() as $productOptionValue) {
68
                    if ($documentProductOption === $productOptionValue->getOption()) {
69
                        foreach ($product->getTaxons() as $taxon) {
70
                            $slug = $taxon->getCode();
71
                            if (!in_array($slug, $taxons)) {
72
                                $taxons[] = $taxon->getSlug();
73
                            }
74
                        }
75
                    }
76
                }
77
            }
78
        }
79
80
        $document->set($this->taxonsProperty, $taxons);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public static function getSubscribedEvents(): array
87
    {
88
        return [
89
            TransformEvent::POST_TRANSFORM => 'buildProperty',
90
        ];
91
    }
92
}
93