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 BitBag\SyliusElasticsearchPlugin\EntityRepository\ProductVariantRepositoryInterface; |
16
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface; |
17
|
|
|
use FOS\ElasticaBundle\Event\TransformEvent; |
18
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
19
|
|
|
use Sylius\Component\Product\Model\ProductOptionInterface; |
20
|
|
|
use Sylius\Component\Product\Model\ProductOptionValueInterface; |
21
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
22
|
|
|
|
23
|
|
|
final class OptionTaxonsBuilder extends AbstractBuilder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var RepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $productOptionValueRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ProductVariantRepositoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $productVariantRepository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ProductTaxonsMapperInterface |
37
|
|
|
*/ |
38
|
|
|
private $productTaxonsMapper; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $optionProperty; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $taxonsProperty; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $excludedOptions; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param RepositoryInterface $productOptionValueRepository |
57
|
|
|
* @param ProductVariantRepositoryInterface $productVariantRepository |
58
|
|
|
* @param ProductTaxonsMapperInterface $productTaxonsMapper |
59
|
|
|
* @param string $optionProperty |
60
|
|
|
* @param string $taxonsProperty |
61
|
|
|
* @param array $excludedOptions |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
RepositoryInterface $productOptionValueRepository, |
65
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
66
|
|
|
ProductTaxonsMapperInterface $productTaxonsMapper, |
67
|
|
|
string $optionProperty, |
68
|
|
|
string $taxonsProperty, |
69
|
|
|
array $excludedOptions = [] |
70
|
|
|
) { |
71
|
|
|
$this->productOptionValueRepository = $productOptionValueRepository; |
72
|
|
|
$this->productVariantRepository = $productVariantRepository; |
73
|
|
|
$this->productTaxonsMapper = $productTaxonsMapper; |
74
|
|
|
$this->optionProperty = $optionProperty; |
75
|
|
|
$this->taxonsProperty = $taxonsProperty; |
76
|
|
|
$this->excludedOptions = $excludedOptions; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param TransformEvent $event |
81
|
|
|
*/ |
82
|
|
|
public function consumeEvent(TransformEvent $event): void |
83
|
|
|
{ |
84
|
|
|
$documentProductOption = $event->getObject(); |
85
|
|
|
|
86
|
|
|
if (!$documentProductOption instanceof ProductOptionInterface |
87
|
|
|
|| in_array($documentProductOption->getCode(), $this->excludedOptions) |
88
|
|
|
) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$document = $event->getDocument(); |
93
|
|
|
$optionValues = $this->productOptionValueRepository->findAll(); |
94
|
|
|
$taxons = []; |
95
|
|
|
|
96
|
|
|
/** @var ProductOptionValueInterface $optionValue */ |
97
|
|
|
foreach ($optionValues as $optionValue) { |
98
|
|
|
$option = $optionValue->getOption(); |
99
|
|
|
/** @var ProductInterface $product */ |
100
|
|
|
$productVariant = $this->productVariantRepository->findOneByOptionValue($optionValue); |
101
|
|
|
|
102
|
|
|
if (null === $productVariant) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$product = $productVariant->getProduct(); |
107
|
|
|
|
108
|
|
|
if ($documentProductOption === $option && $product->isEnabled()) { |
109
|
|
|
$taxons = $this->productTaxonsMapper->mapToUniqueCodes($product); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$document->set($this->taxonsProperty, $taxons); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|