Passed
Push — master ( 1a7a28...c3773f )
by Mikołaj
03:50
created

AttributeBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B resolveProductAttributes() 0 18 5
A consumeEvent() 0 5 1
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 AttributeBuilder extends AbstractBuilder
22
{
23
    /**
24
     * @var ConcatedNameResolverInterface
25
     */
26
    private $attributeNameResolver;
27
28
    /**
29
     * @var StringFormatterInterface
30
     */
31
    private $stringFormatter;
32
33
    /**
34
     * @param ConcatedNameResolverInterface $attributeNameResolver
35
     * @param StringFormatterInterface $stringFormatter
36
     */
37
    public function __construct(
38
        ConcatedNameResolverInterface $attributeNameResolver,
39
        StringFormatterInterface $stringFormatter
40
    )
41
    {
42
        $this->attributeNameResolver = $attributeNameResolver;
43
        $this->stringFormatter = $stringFormatter;
44
    }
45
46
    /**
47
     * @param TransformEvent $event
48
     */
49
    public function consumeEvent(TransformEvent $event): void
50
    {
51
        $this->buildProperty($event, ProductInterface::class,
52
            function (ProductInterface $product, Document $document): void {
53
                $this->resolveProductAttributes($product, $document);
54
            });
55
    }
56
57
    /**
58
     * @param ProductInterface $product
59
     * @param Document $document
60
     */
61
    private function resolveProductAttributes(ProductInterface $product, Document $document): void
62
    {
63
        foreach ($product->getAttributes() as $attributeValue) {
64
            $attributeCode = $attributeValue->getAttribute()->getCode();
65
            $index = $this->attributeNameResolver->resolvePropertyName($attributeCode);
66
            $value = $attributeValue->getValue();
67
            $attributes = [];
68
69
            if (is_array($value)) {
70
                foreach ($value as $singleElement) {
71
                    $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string)$singleElement);
72
                }
73
            } else {
74
                $value = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value;
75
                $attributes[] = $value;
76
            }
77
78
            $document->set($index, $attributes);
79
        }
80
    }
81
}
82