|
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\Formatter\StringFormatterInterface; |
|
16
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
|
17
|
|
|
use Elastica\Document; |
|
18
|
|
|
use FOS\ElasticaBundle\Event\TransformEvent; |
|
19
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class OptionBuilder extends AbstractBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ConcatedNameResolverInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $optionNameResolver; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var StringFormatterInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $stringFormatter; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ConcatedNameResolverInterface $optionNameResolver |
|
35
|
|
|
* @param StringFormatterInterface $stringFormatter |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(ConcatedNameResolverInterface $optionNameResolver, StringFormatterInterface $stringFormatter) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->optionNameResolver = $optionNameResolver; |
|
40
|
|
|
$this->stringFormatter = $stringFormatter; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param TransformEvent $event |
|
45
|
|
|
*/ |
|
46
|
|
|
public function consumeEvent(TransformEvent $event): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->buildProperty($event, ProductInterface::class, |
|
49
|
|
|
function (ProductInterface $product, Document $document): void { |
|
50
|
|
|
$this->resolveProductOptions($product, $document); |
|
51
|
|
|
}); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param ProductInterface $product |
|
56
|
|
|
* @param Document $document |
|
57
|
|
|
*/ |
|
58
|
|
|
private function resolveProductOptions(ProductInterface $product, Document $document): void |
|
59
|
|
|
{ |
|
60
|
|
|
foreach ($product->getVariants() as $productVariant) { |
|
61
|
|
|
foreach ($productVariant->getOptionValues() as $productOptionValue) { |
|
62
|
|
|
$optionCode = $productOptionValue->getOption()->getCode(); |
|
63
|
|
|
$index = $this->optionNameResolver->resolvePropertyName($optionCode); |
|
64
|
|
|
$options = $document->has($index) ? $document->get($index) : []; |
|
65
|
|
|
$value = $this->stringFormatter->formatToLowercaseWithoutSpaces($productOptionValue->getValue()); |
|
66
|
|
|
$options[] = $value; |
|
67
|
|
|
|
|
68
|
|
|
$document->set($index, array_unique($options)); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|