|
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\PropertyBuilder\Mapper\ProductTaxonsMapperInterface; |
|
16
|
|
|
use FOS\ElasticaBundle\Event\TransformEvent; |
|
17
|
|
|
use Sylius\Component\Attribute\Model\AttributeInterface; |
|
18
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
19
|
|
|
use Sylius\Component\Product\Model\ProductAttributeValueInterface; |
|
20
|
|
|
use Sylius\Component\Product\Repository\ProductAttributeValueRepositoryInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class AttributeTaxonsBuilder extends AbstractBuilder |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ProductAttributeValueRepositoryInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $productAttributeValueRepository; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ProductTaxonsMapperInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $productTaxonsMapper; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $attributeProperty; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $taxonsProperty; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private $excludedAttributes; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param ProductAttributeValueRepositoryInterface $productAttributeValueRepository |
|
51
|
|
|
* @param ProductTaxonsMapperInterface $productTaxonsMapper |
|
52
|
|
|
* @param string $attributeProperty |
|
53
|
|
|
* @param string $taxonsProperty |
|
54
|
|
|
* @param array $excludedAttributes |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct( |
|
57
|
|
|
ProductAttributeValueRepositoryInterface $productAttributeValueRepository, |
|
58
|
|
|
ProductTaxonsMapperInterface $productTaxonsMapper, |
|
59
|
|
|
string $attributeProperty, |
|
60
|
|
|
string $taxonsProperty, |
|
61
|
|
|
array $excludedAttributes = [] |
|
62
|
|
|
) { |
|
63
|
|
|
$this->productAttributeValueRepository = $productAttributeValueRepository; |
|
64
|
|
|
$this->productTaxonsMapper = $productTaxonsMapper; |
|
65
|
|
|
$this->attributeProperty = $attributeProperty; |
|
66
|
|
|
$this->taxonsProperty = $taxonsProperty; |
|
67
|
|
|
$this->excludedAttributes = $excludedAttributes; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param TransformEvent $event |
|
72
|
|
|
*/ |
|
73
|
|
|
public function buildProperty(TransformEvent $event): void |
|
74
|
|
|
{ |
|
75
|
|
|
$documentAttribute = $event->getObject(); |
|
76
|
|
|
|
|
77
|
|
|
if (!$documentAttribute instanceof AttributeInterface |
|
78
|
|
|
|| in_array($documentAttribute->getCode(), $this->excludedAttributes) |
|
79
|
|
|
) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$document = $event->getDocument(); |
|
84
|
|
|
$productAttributes = $this->productAttributeValueRepository->findAll(); |
|
85
|
|
|
$taxons = []; |
|
86
|
|
|
|
|
87
|
|
|
/** @var ProductAttributeValueInterface $attributeValue */ |
|
88
|
|
|
foreach ($productAttributes as $attributeValue) { |
|
89
|
|
|
if ($documentAttribute === $attributeValue->getAttribute()) { |
|
90
|
|
|
/** @var ProductInterface $product */ |
|
91
|
|
|
$product = $attributeValue->getProduct(); |
|
92
|
|
|
$taxons = $this->productTaxonsMapper->mapToUniqueCodes($product); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$document->set($this->taxonsProperty, $taxons); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|