PriceSelector   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 31
c 2
b 0
f 1
dl 0
loc 98
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 33 6
A getLocaleToPriceMap() 0 3 1
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 PriceSelector extends AbstractTranslatorFunction implements TranslatorFunctionInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected const KEY_LOCALE = 'locale';
20
21
    /**
22
     * @var string
23
     */
24
    protected const KEY_DATA = 'data';
25
26
    /**
27
     * @var string
28
     */
29
    protected const KEY_AMOUNT = 'amount';
30
31
    /**
32
     * @var string
33
     */
34
    protected const KEY_PRICE = 'price';
35
36
    /**
37
     * @var string
38
     */
39
    protected const KEY_CURRENCY = 'currency';
40
41
    /**
42
     * @var string
43
     */
44
    protected const KEY_PRICE_TYPE = 'type';
45
46
    /**
47
     * @var string
48
     */
49
    protected const KEY_STORE = 'store';
50
51
    /**
52
     * @var string
53
     */
54
    public const OPTION_LOCALE_TO_PRICE_MAP = 'LOCALE_TO_PRICE_MAP_OPTION';
55
56
    /**
57
     * @var array
58
     */
59
    protected $requiredOptions = [
60
        self::OPTION_LOCALE_TO_PRICE_MAP,
61
    ];
62
63
    /**
64
     * @param mixed $value
65
     * @param array $payload
66
     *
67
     * @throws \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Business\Exception\MissingPriceException
68
     *
69
     * @return array
70
     */
71
    public function translate($value, array $payload): array
72
    {
73
        if ($value === null) {
74
            throw new MissingPriceException();
75
        }
76
77
        $result = [];
78
79
        $localeToPriceTypeMap = $this->getLocaleToPriceMap();
80
81
        foreach ($value as $priceInfo) {
82
            $locale = $priceInfo[static::KEY_LOCALE];
83
            if (!array_key_exists($locale, $localeToPriceTypeMap)) {
84
                continue;
85
            }
86
87
            $currency = $localeToPriceTypeMap[$locale][static::KEY_CURRENCY] ?? null;
88
            $type = $localeToPriceTypeMap[$locale][static::KEY_PRICE_TYPE] ?? null;
89
            $store = $localeToPriceTypeMap[$locale][static::KEY_STORE] ?? null;
90
91
            foreach ($priceInfo[static::KEY_DATA] as $price) {
92
                if ($price[static::KEY_CURRENCY] === $currency) {
93
                    $result[] = [
94
                        static::KEY_PRICE => (int)((float)$price[static::KEY_AMOUNT]) * 100,
95
                        static::KEY_PRICE_TYPE => $type,
96
                        static::KEY_CURRENCY => $price[static::KEY_CURRENCY],
97
                        static::KEY_STORE => $store,
98
                    ];
99
                }
100
            }
101
        }
102
103
        return $result;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    protected function getLocaleToPriceMap(): array
110
    {
111
        return $this->options[static::OPTION_LOCALE_TO_PRICE_MAP];
112
    }
113
}
114