DefaultPriceSelector::translate()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 9
eloc 28
c 1
b 0
f 1
nc 9
nop 2
dl 0
loc 48
rs 8.0555
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\AkeneoPimMiddlewareConnector\Business\Translator\TranslatorFunction;
9
10
use SprykerEco\Zed\AkeneoPimMiddlewareConnector\Business\Exception\MissingPriceException;
11
use SprykerMiddleware\Zed\Process\Business\Translator\TranslatorFunction\AbstractTranslatorFunction;
0 ignored issues
show
Bug introduced by
The type SprykerMiddleware\Zed\Pr...tractTranslatorFunction 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...
12
use SprykerMiddleware\Zed\Process\Business\Translator\TranslatorFunction\TranslatorFunctionInterface;
0 ignored issues
show
Bug introduced by
The type SprykerMiddleware\Zed\Pr...slatorFunctionInterface 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...
13
14
class DefaultPriceSelector extends AbstractTranslatorFunction implements TranslatorFunctionInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected const DEFAULT_PRICE_TYPE = 'DEFAULT';
20
21
    /**
22
     * @var string
23
     */
24
    protected const KEY_AMOUNT = 'amount';
25
26
    /**
27
     * @var string
28
     */
29
    protected const KEY_DATA = 'data';
30
31
    /**
32
     * @var string
33
     */
34
    protected const KEY_LOCALE = 'locale';
35
36
    /**
37
     * @var string
38
     */
39
    protected const KEY_CURRENCY = 'currency';
40
41
    /**
42
     * @var string
43
     */
44
    protected const KEY_PRICE = 'price';
45
46
    /**
47
     * @var string
48
     */
49
    protected const KEY_PRICE_TYPE = 'type';
50
51
    /**
52
     * @var string
53
     */
54
    protected const KEY_STORE = 'store';
55
56
    /**
57
     * @var string
58
     */
59
    public const OPTION_STORES = 'OPTION_STORES';
60
61
    /**
62
     * @var string
63
     */
64
    public const OPTION_LOCALE_TO_PRICE_MAP = 'LOCALE_TO_PRICE_MAP_OPTION';
65
66
    /**
67
     * @var array
68
     */
69
    protected $requiredOptions = [
70
        self::OPTION_STORES,
71
        self::OPTION_LOCALE_TO_PRICE_MAP,
72
    ];
73
74
    /**
75
     * @param mixed $value
76
     * @param array $payload
77
     *
78
     * @throws \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Business\Exception\MissingPriceException
79
     *
80
     * @return array
81
     */
82
    public function translate($value, array $payload): array
83
    {
84
        if ($value === null) {
85
            throw new MissingPriceException();
86
        }
87
88
        $result = [];
89
90
        foreach ($value as $priceInfo) {
91
            $locale = $priceInfo[static::KEY_LOCALE];
92
            if ($locale == null) {
93
                foreach ($priceInfo[static::KEY_DATA] as $price) {
94
                    foreach ($this->getStores() as $store) {
95
                        $result[] = [
96
                            static::KEY_PRICE => (int)((float)$price[static::KEY_AMOUNT]) * 100,
97
                            static::KEY_PRICE_TYPE => static::DEFAULT_PRICE_TYPE,
98
                            static::KEY_CURRENCY => $price[static::KEY_CURRENCY],
99
                            static::KEY_STORE => $store,
100
                        ];
101
                    }
102
                }
103
104
                return $result;
105
            }
106
107
            $localeToPriceTypeMap = $this->getLocaleToPriceMap();
108
109
            if (!array_key_exists($locale, $localeToPriceTypeMap)) {
110
                continue;
111
            }
112
113
            $currency = $localeToPriceTypeMap[$locale][static::KEY_CURRENCY] ?? null;
114
            $type = $localeToPriceTypeMap[$locale][static::KEY_PRICE_TYPE] ?? null;
115
            $store = $localeToPriceTypeMap[$locale][static::KEY_STORE] ?? null;
116
117
            foreach ($priceInfo[static::KEY_DATA] as $price) {
118
                if ($price[static::KEY_CURRENCY] === $currency) {
119
                    $result[] = [
120
                        static::KEY_PRICE => (int)((float)$price[static::KEY_AMOUNT]) * 100,
121
                        static::KEY_PRICE_TYPE => $type,
122
                        static::KEY_CURRENCY => $price[static::KEY_CURRENCY],
123
                        static::KEY_STORE => $store,
124
                    ];
125
                }
126
            }
127
        }
128
129
        return $result;
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    protected function getStores(): array
136
    {
137
        return $this->options[static::OPTION_STORES];
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    protected function getLocaleToPriceMap(): array
144
    {
145
        return $this->options[static::OPTION_LOCALE_TO_PRICE_MAP];
146
    }
147
}
148