Passed
Push — master ( b1f912...39c5fb )
by Damian
08:50
created

AttributeBuilder::processAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 14
rs 9.9
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\AttributeInterface;
20
use Sylius\Component\Attribute\Model\AttributeTranslation;
21
use Sylius\Component\Core\Model\ProductInterface;
22
23
final class AttributeBuilder extends AbstractBuilder
24
{
25
    /** @var ConcatedNameResolverInterface */
26
    private $attributeNameResolver;
27
28
    /** @var StringFormatterInterface */
29
    private $stringFormatter;
30
31
    public function __construct(
32
        ConcatedNameResolverInterface $attributeNameResolver,
33
        StringFormatterInterface $stringFormatter
34
    ) {
35
        $this->attributeNameResolver = $attributeNameResolver;
36
        $this->stringFormatter = $stringFormatter;
37
    }
38
39
    public function consumeEvent(TransformEvent $event): void
40
    {
41
        $this->buildProperty($event, ProductInterface::class,
42
            function (ProductInterface $product, Document $document): void {
43
                $this->resolveProductAttributes($product, $document);
44
            });
45
    }
46
47
    private function resolveProductAttributes(ProductInterface $product, Document $document): void
48
    {
49
        foreach ($product->getAttributes() as $productAttribute) {
50
            $attribute = $productAttribute->getAttribute();
51
52
            if (!$attribute) {
53
                continue;
54
            }
55
56
            $this->processAttribute($attribute, $productAttribute, $document);
57
        }
58
    }
59
60
    private function resolveProductAttribute(array $attributeConfiguration, $attributeValue, AttributeTranslation $attribute): array
61
    {
62
        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

62
        if ($attribute->getTranslatable()->/** @scrutinizer ignore-call */ getType() === 'select') {
Loading history...
63
            $choices = $attributeConfiguration['choices'];
64
            if (is_array($attributeValue)) {
65
                foreach ($attributeValue as $i => $item) {
66
                    $attributeValue[$i] = $choices[$item][$attribute->getLocale()] ?? $item;
67
                }
68
            } else {
69
                $attributeValue = $choices[$attributeValue][$attribute->getLocale()] ?? $attributeValue;
70
            }
71
        }
72
73
        $attributes = [];
74
        if (is_array($attributeValue)) {
75
            foreach ($attributeValue as $singleElement) {
76
                $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement);
77
            }
78
        } else {
79
            $attributeValue = is_string($attributeValue) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($attributeValue) : $attributeValue;
80
            $attributes[] = $attributeValue;
81
        }
82
83
        return $attributes;
84
    }
85
86
87
    private function processAttribute(AttributeInterface $attribute, $productAttribute, Document $document): void
88
    {
89
        $attributeCode = $attribute->getCode();
90
        $attributeConfiguration = $attribute->getConfiguration();
91
        foreach ($attribute->getTranslations() as $attributeTranslation) {
92
            $value = $productAttribute->getValue();
93
            $documentKey = $this->attributeNameResolver->resolvePropertyName($attributeCode);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

93
            $documentKey = $this->attributeNameResolver->resolvePropertyName(/** @scrutinizer ignore-type */ $attributeCode);
Loading history...
94
            $code = \sprintf('%s_%s', $documentKey, $attributeTranslation->getLocale());
95
            $values = $this->resolveProductAttribute(
96
                $attributeConfiguration,
97
                $value,
98
                $attributeTranslation
99
            );
100
            $document->set($code, $values);
101
        }
102
    }
103
104
}
105