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\Attribute\Model\AttributeInterface; |
17
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
18
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
19
|
|
|
|
20
|
|
|
final class AttributeTaxonsBuilder extends AbstractBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ProductRepositoryInterface |
24
|
|
|
*/ |
25
|
|
|
private $productRepository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $attributeProperty; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $taxonsProperty; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ProductRepositoryInterface $productRepository |
39
|
|
|
* @param string $attributeProperty |
40
|
|
|
* @param string $taxonsProperty |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
ProductRepositoryInterface $productRepository, |
44
|
|
|
string $attributeProperty, |
45
|
|
|
string $taxonsProperty |
46
|
|
|
) |
47
|
|
|
{ |
48
|
|
|
$this->productRepository = $productRepository; |
49
|
|
|
$this->attributeProperty = $attributeProperty; |
50
|
|
|
$this->taxonsProperty = $taxonsProperty; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param TransformEvent $event |
55
|
|
|
*/ |
56
|
|
|
public function buildProperty(TransformEvent $event): void |
57
|
|
|
{ |
58
|
|
|
/** @var AttributeInterface $documentAttribute */ |
59
|
|
|
$documentAttribute = $event->getObject(); |
60
|
|
|
|
61
|
|
|
if (!$documentAttribute instanceof AttributeInterface) { |
|
|
|
|
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->getAttributes() as $attributeValue) { |
72
|
|
|
if ($documentAttribute === $attributeValue->getAttribute()) { |
73
|
|
|
foreach ($product->getTaxons() as $taxon) { |
74
|
|
|
$code = $taxon->getCode(); |
75
|
|
|
if (!in_array($code, $taxons)) { |
76
|
|
|
$taxons[] = $code; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$document->set($this->taxonsProperty, $taxons); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|