1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface; |
14
|
|
|
use BitBag\SyliusElasticsearchPlugin\Repository\ProductVariantRepositoryInterface; |
15
|
|
|
use FOS\ElasticaBundle\Event\PostTransformEvent; |
16
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
17
|
|
|
use Sylius\Component\Product\Model\ProductOptionInterface; |
18
|
|
|
use Sylius\Component\Product\Model\ProductOptionValueInterface; |
19
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
20
|
|
|
|
21
|
|
|
final class OptionTaxonsBuilder extends AbstractBuilder |
22
|
|
|
{ |
23
|
|
|
/** @var RepositoryInterface */ |
24
|
|
|
private $productOptionValueRepository; |
25
|
|
|
|
26
|
|
|
/** @var ProductVariantRepositoryInterface */ |
27
|
|
|
private $productVariantRepository; |
28
|
|
|
|
29
|
|
|
/** @var ProductTaxonsMapperInterface */ |
30
|
|
|
private $productTaxonsMapper; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $taxonsProperty; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
private $excludedOptions; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
RepositoryInterface $productOptionValueRepository, |
40
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
41
|
|
|
ProductTaxonsMapperInterface $productTaxonsMapper, |
42
|
|
|
string $taxonsProperty, |
43
|
|
|
array $excludedOptions = [] |
44
|
|
|
) { |
45
|
|
|
$this->productOptionValueRepository = $productOptionValueRepository; |
46
|
|
|
$this->productVariantRepository = $productVariantRepository; |
47
|
|
|
$this->productTaxonsMapper = $productTaxonsMapper; |
48
|
|
|
$this->taxonsProperty = $taxonsProperty; |
49
|
|
|
$this->excludedOptions = $excludedOptions; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function consumeEvent(PostTransformEvent $event): void |
53
|
|
|
{ |
54
|
|
|
$documentProductOption = $event->getObject(); |
55
|
|
|
|
56
|
|
|
if (!$documentProductOption instanceof ProductOptionInterface |
57
|
|
|
|| in_array($documentProductOption->getCode(), $this->excludedOptions, true) |
58
|
|
|
) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$document = $event->getDocument(); |
63
|
|
|
$optionValues = $this->productOptionValueRepository->findAll(); |
64
|
|
|
$taxons = []; |
65
|
|
|
|
66
|
|
|
/** @var ProductOptionValueInterface $optionValue */ |
67
|
|
|
foreach ($optionValues as $optionValue) { |
68
|
|
|
$option = $optionValue->getOption(); |
69
|
|
|
$productVariant = $this->productVariantRepository->findOneByOptionValue($optionValue); |
70
|
|
|
|
71
|
|
|
if (null === $productVariant) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @var ProductInterface $product */ |
76
|
|
|
$product = $productVariant->getProduct(); |
77
|
|
|
|
78
|
|
|
if ($documentProductOption === $option && $product->isEnabled()) { |
79
|
|
|
$taxons = $this->productTaxonsMapper->mapToUniqueCodes($product); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$document->set($this->taxonsProperty, array_unique($taxons)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|