ProductAttributesMapper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 33
c 5
b 0
f 0
dl 0
loc 62
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B mapToChoices() 0 41 10
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\Form\Type\ChoiceMapper;
12
13
use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface;
14
use BitBag\SyliusElasticsearchPlugin\Repository\ProductAttributeValueRepositoryInterface;
15
use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
16
use Sylius\Component\Locale\Context\LocaleContextInterface;
17
use Sylius\Component\Product\Model\ProductAttributeInterface;
18
19
final class ProductAttributesMapper implements ProductAttributesMapperInterface
20
{
21
    /** @var ProductAttributeValueRepositoryInterface */
22
    private $productAttributeValueRepository;
23
24
    /** @var LocaleContextInterface */
25
    private $localeContext;
26
27
    /** @var StringFormatterInterface */
28
    private $stringFormatter;
29
30
    public function __construct(
31
        ProductAttributeValueRepositoryInterface $productAttributeValueRepository,
32
        LocaleContextInterface $localeContext,
33
        StringFormatterInterface $stringFormatter
34
    ) {
35
        $this->productAttributeValueRepository = $productAttributeValueRepository;
36
        $this->localeContext = $localeContext;
37
        $this->stringFormatter = $stringFormatter;
38
    }
39
40
    public function mapToChoices(ProductAttributeInterface $productAttribute): array
41
    {
42
        $configuration = $productAttribute->getConfiguration();
43
44
        if (isset($configuration['choices']) && is_array($configuration['choices'])) {
45
            $choices = [];
46
            foreach ($configuration['choices'] as $singleValue => $val) {
47
                $label = $configuration['choices'][$singleValue][$this->localeContext->getLocaleCode()];
48
                $singleValue = SelectAttributeType::TYPE === $productAttribute->getType() ? $label : $singleValue;
49
                $choice = $this->stringFormatter->formatToLowercaseWithoutSpaces($singleValue);
50
                $choices[$label] = $choice;
51
            }
52
53
            return $choices;
54
        }
55
56
        $attributeValues = $this->productAttributeValueRepository->getUniqueAttributeValues($productAttribute);
57
58
        $choices = [];
59
        array_walk($attributeValues, function ($productAttributeValue) use (&$choices, $productAttribute): void {
60
            $value = $productAttributeValue['value'];
61
62
            $configuration = $productAttribute->getConfiguration();
63
64
            if (is_array($value)
65
                && isset($configuration['choices'])
66
                && is_array($configuration['choices'])
67
            ) {
68
                foreach ($value as $singleValue) {
69
                    $choice = $this->stringFormatter->formatToLowercaseWithoutSpaces($singleValue);
70
                    $label = $configuration['choices'][$singleValue][$this->localeContext->getLocaleCode()];
71
                    $choices[$label] = $choice;
72
                }
73
            } else {
74
                $choice = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value;
75
                $choices[$value] = $choice;
76
            }
77
        });
78
        unset($attributeValues);
79
80
        return $choices;
81
    }
82
}
83