Passed
Push — master ( 474ad3...1d93cd )
by Mikołaj
04:15
created

AttributeBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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 BitBag\SyliusElasticsearchPlugin\PropertyValueResolver\AttributeValueResolverInterface;
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusElasticsear...eValueResolverInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Elastica\Document;
19
use FOS\ElasticaBundle\Event\TransformEvent;
20
use Sylius\Component\Core\Model\ProductInterface;
21
22
final class AttributeBuilder extends AbstractBuilder
23
{
24
    /**
25
     * @var ConcatedNameResolverInterface
26
     */
27
    private $attributeNameResolver;
28
29
    /**
30
     * @var AttributeValueResolverInterface
31
     */
32
    private $attributeValueResolver;
0 ignored issues
show
introduced by
The private property $attributeValueResolver is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var StringFormatterInterface
36
     */
37
    private $stringFormatter;
38
39
    /**
40
     * @param ConcatedNameResolverInterface $attributeNameResolver
41
     * @param StringFormatterInterface $stringFormatter
42
     */
43
    public function __construct(
44
        ConcatedNameResolverInterface $attributeNameResolver,
45
        StringFormatterInterface $stringFormatter
46
    )
47
    {
48
        $this->attributeNameResolver = $attributeNameResolver;
49
        $this->stringFormatter = $stringFormatter;
50
    }
51
52
    /**
53
     * @param TransformEvent $event
54
     */
55
    public function buildProperty(TransformEvent $event): void
56
    {
57
        /** @var ProductInterface $product */
58
        $product = $event->getObject();
59
60
        if (!$product instanceof ProductInterface) {
0 ignored issues
show
introduced by
$product is always a sub-type of Sylius\Component\Core\Model\ProductInterface.
Loading history...
61
            return;
62
        }
63
64
        $document = $event->getDocument();
65
66
        $this->resolveProductAttributes($product, $document);
67
    }
68
69
    /**
70
     * @param ProductInterface $product
71
     * @param Document $document
72
     */
73
    private function resolveProductAttributes(ProductInterface $product, Document $document): void
74
    {
75
        foreach ($product->getAttributes() as $attributeValue) {
76
            $attributeCode = $attributeValue->getAttribute()->getCode();
77
            $index = $this->attributeNameResolver->resolvePropertyName($attributeCode);
78
            $value = $attributeValue->getValue();
79
            $attributes = [];
80
81
            if (is_array($value)) {
82
                foreach ($value as $singleElement) {
83
                    $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement);
84
                }
85
            } else {
86
                $value = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value;
87
                $attributes[] = $value;
88
            }
89
90
            $document->set($index, $attributes);
91
        }
92
    }
93
}
94