Passed
Pull Request — master (#167)
by
unknown
05:24
created

handleAttributesPrefixedProperty()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 17
rs 9.6111
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\Controller\RequestDataHandler;
14
15
use BitBag\SyliusElasticsearchPlugin\Exception\TaxonNotFoundException;
16
use BitBag\SyliusElasticsearchPlugin\Finder\ProductAttributesFinderInterface;
17
use Sylius\Component\Attribute\AttributeType\CheckboxAttributeType;
18
use Sylius\Component\Attribute\AttributeType\IntegerAttributeType;
19
use Sylius\Component\Locale\Context\LocaleContextInterface;
20
use Sylius\Component\Product\Model\ProductAttribute;
21
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
22
23
final class ShopProductListDataHandler implements DataHandlerInterface
24
{
25
    /** @var TaxonRepositoryInterface */
26
    private $taxonRepository;
27
28
    /** @var LocaleContextInterface */
29
    private $localeContext;
30
31
    /** @var ProductAttributesFinderInterface */
32
    private $attributesFinder;
33
34
    /** @var string */
35
    private $namePropertyPrefix;
36
37
    /** @var string */
38
    private $taxonsProperty;
39
40
    /** @var string */
41
    private $optionPropertyPrefix;
42
43
    /** @var string */
44
    private $attributePropertyPrefix;
45
46
    public function __construct(
47
        TaxonRepositoryInterface $taxonRepository,
48
        LocaleContextInterface $localeContext,
49
        ProductAttributesFinderInterface $attributesFinder,
50
        string $namePropertyPrefix,
51
        string $taxonsProperty,
52
        string $optionPropertyPrefix,
53
        string $attributePropertyPrefix
54
    )
55
    {
56
        $this->taxonRepository = $taxonRepository;
57
        $this->localeContext = $localeContext;
58
        $this->attributesFinder = $attributesFinder;
59
        $this->namePropertyPrefix = $namePropertyPrefix;
60
        $this->taxonsProperty = $taxonsProperty;
61
        $this->optionPropertyPrefix = $optionPropertyPrefix;
62
        $this->attributePropertyPrefix = $attributePropertyPrefix;
63
    }
64
65
    public function retrieveData(array $requestData): array
66
    {
67
        $slug = $requestData['slug'];
68
        $taxon = $this->taxonRepository->findOneBySlug($slug, $this->localeContext->getLocaleCode());
69
70
        if (null === $taxon) {
71
            throw new TaxonNotFoundException();
72
        }
73
74
        $data[$this->namePropertyPrefix] = (string) $requestData[$this->namePropertyPrefix];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
75
        $data[$this->taxonsProperty] = strtolower($taxon->getCode());
0 ignored issues
show
Bug introduced by
It seems like $taxon->getCode() can also be of type null; however, parameter $string of strtolower() 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

75
        $data[$this->taxonsProperty] = strtolower(/** @scrutinizer ignore-type */ $taxon->getCode());
Loading history...
76
        $data['taxon'] = $taxon;
77
        $data = array_merge($data, $requestData['price']);
78
79
        $attributesDefinitions = $this->attributesFinder->findByTaxon($taxon);
80
81
        $this->handleOptionsPrefixedProperty($requestData, $data);
82
        $this->handleAttributesPrefixedProperty($requestData, $data, $attributesDefinitions);
0 ignored issues
show
Bug introduced by
It seems like $attributesDefinitions can also be of type null; however, parameter $attributesDefinitions of BitBag\SyliusElasticsear...butesPrefixedProperty() does only seem to accept array, 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

82
        $this->handleAttributesPrefixedProperty($requestData, $data, /** @scrutinizer ignore-type */ $attributesDefinitions);
Loading history...
83
84
        return $data;
85
    }
86
87
    private function handleOptionsPrefixedProperty(
88
        array $requestData,
89
        array &$data
90
    ): void
91
    {
92
        if (!isset($requestData['options'])) {
93
            return;
94
        }
95
96
        foreach ($requestData['options'] as $key => $value) {
97
            if (is_array($value) && 0 === strpos($key, $this->optionPropertyPrefix)) {
98
                $data[$key] = array_map(function (string $property): string {
99
                    return strtolower($property);
100
                }, $value);
101
            }
102
        }
103
    }
104
105
    private function handleAttributesPrefixedProperty(
106
        array $requestData,
107
        array &$data,
108
        array $attributesDefinitions
109
    ): void
110
    {
111
        if (!isset($requestData['attributes'])) {
112
            return;
113
        }
114
115
        $attributeTypes = $this->getAttributeTypes($attributesDefinitions);
116
117
        foreach ($requestData['attributes'] as $key => $value) {
118
            if (!is_array($value) || 0 !== strpos($key, $this->attributePropertyPrefix)) {
119
                continue;
120
            }
121
            $data[$key] = $this->reformatAttributeArrayValues($value, $key, $attributeTypes);
122
        }
123
    }
124
125
    private function getAttributeTypes(array $attributesDefinitions): array
126
    {
127
        $data = [];
128
        /** @var ProductAttribute $attributesDefinition */
129
        foreach ($attributesDefinitions as $attributesDefinition) {
130
            $data["attribute_".$attributesDefinition->getCode()] = $attributesDefinition->getType();
131
        }
132
        return $data;
133
    }
134
135
    private function reformatAttributeArrayValues(array $attributeValues, string $property, array $attributesDefinitions): array
136
    {
137
        $reformattedValues = [];
138
        foreach ($attributeValues as $attributeValue) {
139
            switch ($attributesDefinitions[$property]){
140
                case CheckboxAttributeType::TYPE:
141
                    $value = (bool)($attributeValue);
142
                    break;
143
                case IntegerAttributeType::TYPE:
144
                    $value = (float)($attributeValue);
145
                    break;
146
                default:
147
                    $value = strtolower($attributeValue);
148
            }
149
            $reformattedValues[] = $value;
150
        }
151
        return $reformattedValues;
152
    }
153
}
154