Passed
Pull Request — master (#164)
by
unknown
07:07
created

AttributeBuilder::consumeEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Attribute\Model\AttributeTranslation;
20
use Sylius\Component\Core\Model\ProductInterface;
21
use Sylius\Component\Locale\Context\LocaleContextInterface;
22
23
final class AttributeBuilder extends AbstractBuilder
24
{
25
    /** @var ConcatedNameResolverInterface */
26
    private $attributeNameResolver;
27
28
    /** @var StringFormatterInterface */
29
    private $stringFormatter;
30
31
    /** @var LocaleContextInterface */
32
    private $localeContext;
33
34
    public function __construct(
35
        ConcatedNameResolverInterface $attributeNameResolver,
36
        StringFormatterInterface $stringFormatter,
37
        LocaleContextInterface $localeContext
38
    ) {
39
        $this->attributeNameResolver = $attributeNameResolver;
40
        $this->stringFormatter = $stringFormatter;
41
        $this->localeContext = $localeContext;
42
    }
43
44
    public function consumeEvent(TransformEvent $event): void
45
    {
46
        $this->buildProperty($event, ProductInterface::class,
47
            function (ProductInterface $product, Document $document): void {
48
                $this->resolveProductAttributes($product, $document);
49
            });
50
    }
51
52
    private function resolveProductAttributes(ProductInterface $product, Document $document): void
53
    {
54
        foreach ($product->getAttributes() as $attributeValue) {
55
            $attribute = $attributeValue->getAttribute();
56
57
            if (!$attribute) {
58
                continue;
59
            }
60
61
            $attributeValueCode = $attributeValue->getLocaleCode();
62
            $attributeCode = $attribute->getCode();
63
64
            $attributeConfiguration = $attribute->getConfiguration();
65
            foreach ($attribute->getTranslations() as $attributeTranslation) {
66
                if ($attributeValueCode !== $attributeTranslation->getLocale()) {
67
                    continue;
68
                }
69
                $attributeValue = $attributeValue->getValue();
70
71
                $propertyName = $this->attributeNameResolver->resolvePropertyName(
72
                    \sprintf('%s_%s', $attributeCode, $attributeTranslation->getLocale()
73
                    )
74
                );
75
                $values = $this->resolveProductAttribute(
76
                    $attributeConfiguration,
77
                    $attributeValue,
78
                    $attributeTranslation
79
                );
80
81
                $document->set($propertyName, $values);
82
            }
83
        }
84
    }
85
86
    private function resolveProductAttribute(array $attributeConfiguration, $attributeValue, AttributeTranslation $attribute): array
87
    {
88
        if ($attribute->getTranslatable()->getType() === 'select') {
0 ignored issues
show
Bug introduced by
The method getType() does not exist on Sylius\Component\Resourc...l\TranslatableInterface. It seems like you code against a sub-type of Sylius\Component\Resourc...l\TranslatableInterface such as Sylius\Component\Attribu...odel\AttributeInterface or Sylius\Bundle\ApiBundle\Application\Entity\Taxon or Sylius\Bundle\ApiBundle\Application\Entity\Taxon. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        if ($attribute->getTranslatable()->/** @scrutinizer ignore-call */ getType() === 'select') {
Loading history...
89
            $choices = $attributeConfiguration['choices'];
90
            if (is_array($attributeValue)) {
91
                foreach ($attributeValue as $i => $item) {
92
                    $attributeValue[$i] = $choices[$item][$attribute->getLocale()] ?? $item;
93
                }
94
            } else {
95
                $attributeValue = $choices[$attributeValue][$attribute->getLocale()] ?? $attributeValue;
96
            }
97
        }
98
99
        $attributes = [];
100
        if (is_array($attributeValue)) {
101
            foreach ($attributeValue as $singleElement) {
102
                $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement);
103
            }
104
        } else {
105
            $attributeValue = is_string($attributeValue) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($attributeValue) : $attributeValue;
106
            $attributes[] = $attributeValue;
107
        }
108
109
        return $attributes;
110
    }
111
}
112