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
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $excludedOptions; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ProductRepositoryInterface $productRepository |
44
|
|
|
* @param string $optionProperty |
45
|
|
|
* @param string $taxonsProperty |
46
|
|
|
* @param array $excludedOptions |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
ProductRepositoryInterface $productRepository, |
50
|
|
|
string $optionProperty, |
51
|
|
|
string $taxonsProperty, |
52
|
|
|
array $excludedOptions = [] |
53
|
|
|
) { |
54
|
|
|
$this->productRepository = $productRepository; |
55
|
|
|
$this->optionProperty = $optionProperty; |
56
|
|
|
$this->taxonsProperty = $taxonsProperty; |
57
|
|
|
$this->excludedOptions = $excludedOptions; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param TransformEvent $event |
62
|
|
|
*/ |
63
|
|
|
public function buildProperty(TransformEvent $event): void |
64
|
|
|
{ |
65
|
|
|
$documentProductOption = $event->getObject(); |
66
|
|
|
|
67
|
|
|
if ( |
68
|
|
|
!$documentProductOption instanceof ProductOptionInterface || |
69
|
|
|
in_array($documentProductOption->getCode(), $this->excludedOptions) |
70
|
|
|
) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$document = $event->getDocument(); |
75
|
|
|
$products = $this->productRepository->findAll(); |
76
|
|
|
$taxons = []; |
77
|
|
|
|
78
|
|
|
/** @var ProductInterface $product */ |
79
|
|
|
foreach ($products as $product) { |
80
|
|
|
foreach ($product->getVariants() as $productVariant) { |
81
|
|
|
foreach ($productVariant->getOptionValues() as $productOptionValue) { |
82
|
|
|
if ($documentProductOption === $productOptionValue->getOption()) { |
83
|
|
|
foreach ($product->getTaxons() as $taxon) { |
84
|
|
|
$code = $taxon->getCode(); |
85
|
|
|
if (!in_array($code, $taxons)) { |
86
|
|
|
$taxons[] = $code; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$document->set($this->taxonsProperty, $taxons); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|