Completed
Push — master ( b40331...4f0c8d )
by Damian
14:06 queued 09:46
created

Form/Type/ChoiceMapper/ProductAttributesMapper.php (1 issue)

Labels
Severity
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\Form\Type\ChoiceMapper;
14
15
use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface;
16
use BitBag\SyliusElasticsearchPlugin\Repository\ProductAttributeValueRepositoryInterface;
17
use Sylius\Component\Locale\Context\LocaleContextInterface;
18
use Sylius\Component\Product\Model\ProductAttributeInterface;
19
use Sylius\Component\Product\Model\ProductAttributeValueInterface;
20
21
final class ProductAttributesMapper implements ProductAttributesMapperInterface
22
{
23
    /** @var ProductAttributeValueRepositoryInterface */
24
    private $productAttributeValueRepository;
25
26
    /** @var LocaleContextInterface */
27
    private $localeContext;
28
29
    /** @var StringFormatterInterface */
30
    private $stringFormatter;
31
32
    public function __construct(
33
        ProductAttributeValueRepositoryInterface $productAttributeValueRepository,
34
        LocaleContextInterface $localeContext,
35
        StringFormatterInterface $stringFormatter
36
    ) {
37
        $this->productAttributeValueRepository = $productAttributeValueRepository;
38
        $this->localeContext = $localeContext;
39
        $this->stringFormatter = $stringFormatter;
40
    }
41
42
    public function mapToChoices(ProductAttributeInterface $productAttribute): array
43
    {
44
        $configuration = $productAttribute->getConfiguration();
45
46
        if (isset($configuration['choices']) && is_array($configuration['choices'])) {
47
            $choices = [];
48
            foreach ($configuration['choices'] as $singleValue => $val) {
49
                $label = $configuration['choices'][$singleValue][$this->localeContext->getLocaleCode()];
50
                $singleValue = SelectAttributeType::TYPE === $productAttribute->getType() ? $label : $singleValue;
0 ignored issues
show
The type BitBag\SyliusElasticsear...per\SelectAttributeType 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...
51
                $choice = $this->stringFormatter->formatToLowercaseWithoutSpaces($singleValue);
52
                $choices[$label] = $choice;
53
            }
54
55
            return $choices;
56
        }
57
58
        $attributeValues = $this->productAttributeValueRepository->getUniqueAttributeValues($productAttribute);
59
60
        $choices = [];
61
        array_walk($attributeValues, function (ProductAttributeValueInterface $productAttributeValue) use (&$choices): void {
62
            $product = $productAttributeValue->getProduct();
63
64
            if (!$product->isEnabled()) {
65
                unset($product);
66
67
                return;
68
            }
69
70
            $value = $productAttributeValue->getValue();
71
            $configuration = $productAttributeValue->getAttribute()->getConfiguration();
72
73
            if (is_array($value)
74
                && isset($configuration['choices'])
75
                && is_array($configuration['choices'])
76
            ) {
77
                foreach ($value as $singleValue) {
78
                    $choice = $this->stringFormatter->formatToLowercaseWithoutSpaces($singleValue);
79
                    $label = $configuration['choices'][$singleValue][$this->localeContext->getLocaleCode()];
80
                    $choices[$label] = $choice;
81
                }
82
            } else {
83
                $choice = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value;
84
                $choices[$value] = $choice;
85
            }
86
        });
87
        unset($attributeValues);
88
89
        return $choices;
90
    }
91
}
92