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\Formatter\StringFormatterInterface; |
||||
14 | use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
||||
15 | use Elastica\Document; |
||||
16 | use FOS\ElasticaBundle\Event\PostTransformEvent; |
||||
17 | use Sylius\Component\Attribute\Model\AttributeInterface; |
||||
18 | use Sylius\Component\Attribute\Model\AttributeTranslation; |
||||
19 | use Sylius\Component\Core\Model\ProductInterface; |
||||
20 | |||||
21 | final class AttributeBuilder extends AbstractBuilder |
||||
22 | { |
||||
23 | /** @var ConcatedNameResolverInterface */ |
||||
24 | private $attributeNameResolver; |
||||
25 | |||||
26 | /** @var StringFormatterInterface */ |
||||
27 | private $stringFormatter; |
||||
28 | |||||
29 | public function __construct( |
||||
30 | ConcatedNameResolverInterface $attributeNameResolver, |
||||
31 | StringFormatterInterface $stringFormatter |
||||
32 | ) { |
||||
33 | $this->attributeNameResolver = $attributeNameResolver; |
||||
34 | $this->stringFormatter = $stringFormatter; |
||||
35 | } |
||||
36 | |||||
37 | public function consumeEvent(PostTransformEvent $event): void |
||||
38 | { |
||||
39 | $this->buildProperty( |
||||
40 | $event, |
||||
41 | ProductInterface::class, |
||||
42 | function (ProductInterface $product, Document $document): void { |
||||
43 | $this->resolveProductAttributes($product, $document); |
||||
44 | } |
||||
45 | ); |
||||
46 | } |
||||
47 | |||||
48 | private function resolveProductAttributes(ProductInterface $product, Document $document): void |
||||
49 | { |
||||
50 | foreach ($product->getAttributes() as $productAttribute) { |
||||
51 | $attribute = $productAttribute->getAttribute(); |
||||
52 | |||||
53 | if (!$attribute) { |
||||
54 | continue; |
||||
55 | } |
||||
56 | |||||
57 | $this->processAttribute($attribute, $productAttribute, $document); |
||||
58 | } |
||||
59 | } |
||||
60 | |||||
61 | private function resolveProductAttribute( |
||||
62 | array $attributeConfiguration, |
||||
63 | $attributeValue, |
||||
64 | AttributeTranslation $attribute |
||||
65 | ): array |
||||
66 | { |
||||
67 | if ('select' === $attribute->getTranslatable()->getType()) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
68 | $choices = $attributeConfiguration['choices']; |
||||
69 | if (is_array($attributeValue)) { |
||||
70 | foreach ($attributeValue as $i => $item) { |
||||
71 | $attributeValue[$i] = $choices[$item][$attribute->getLocale()] ?? $item; |
||||
72 | } |
||||
73 | } else { |
||||
74 | $attributeValue = $choices[$attributeValue][$attribute->getLocale()] ?? $attributeValue; |
||||
75 | } |
||||
76 | } |
||||
77 | |||||
78 | $attributes = []; |
||||
79 | if (is_array($attributeValue)) { |
||||
80 | foreach ($attributeValue as $singleElement) { |
||||
81 | $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement); |
||||
82 | } |
||||
83 | } else { |
||||
84 | $attributeValue = is_string($attributeValue) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($attributeValue) : $attributeValue; |
||||
85 | $attributes[] = $attributeValue; |
||||
86 | } |
||||
87 | |||||
88 | return $attributes; |
||||
89 | } |
||||
90 | |||||
91 | private function processAttribute( |
||||
92 | AttributeInterface $attribute, |
||||
93 | $productAttribute, |
||||
94 | Document $document |
||||
95 | ): void |
||||
96 | { |
||||
97 | $attributeCode = $attribute->getCode(); |
||||
98 | $attributeConfiguration = $attribute->getConfiguration(); |
||||
99 | foreach ($attribute->getTranslations() as $attributeTranslation) { |
||||
100 | $value = $productAttribute->getValue(); |
||||
101 | $documentKey = $this->attributeNameResolver->resolvePropertyName($attributeCode); |
||||
0 ignored issues
–
show
It seems like
$attributeCode can also be of type null ; however, parameter $suffix of BitBag\SyliusElasticsear...::resolvePropertyName() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
102 | $code = \sprintf('%s_%s', $documentKey, $attributeTranslation->getLocale()); |
||||
103 | $values = $this->resolveProductAttribute( |
||||
104 | $attributeConfiguration, |
||||
105 | $value, |
||||
106 | $attributeTranslation |
||||
107 | ); |
||||
108 | $document->set($code, $values); |
||||
109 | } |
||||
110 | } |
||||
111 | } |
||||
112 |