ValuesToAttributes::getLocales()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 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...
11
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...
12
13
class ValuesToAttributes extends AbstractTranslatorFunction implements TranslatorFunctionInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected const KEY_ATTRIBUTES = 'attributes';
19
20
    /**
21
     * @var string
22
     */
23
    protected const KEY_LOCALE = 'locale';
24
25
    /**
26
     * @var string
27
     */
28
    protected const KEY_DATA = 'data';
29
30
    /**
31
     * @var array
32
     */
33
    protected $requiredOptions = ['locales'];
34
35
    /**
36
     * @param mixed $value
37
     * @param array $payload
38
     *
39
     * @return array
40
     */
41
    public function translate($value, array $payload): array
42
    {
43
        $value[static::KEY_ATTRIBUTES] = [];
44
45
        foreach ($value as $key => $scopedAttributes) {
46
            foreach ($scopedAttributes as $attribute) {
47
                if (!$this->isLocalizedAttribute($attribute)) {
48
                    $value[static::KEY_ATTRIBUTES][$key] = $attribute[static::KEY_DATA] ?? null;
49
                }
50
            }
51
        }
52
53
        return $value;
54
    }
55
56
    /**
57
     * @param mixed $attribute
58
     *
59
     * @return bool
60
     */
61
    protected function isLocalizedAttribute($attribute): bool
62
    {
63
        return $this->isLocalizableByAkeneo($attribute) || $this->hasLocaleKeysInData($attribute);
64
    }
65
66
    /**
67
     * @param mixed $attribute
68
     *
69
     * @return bool
70
     */
71
    protected function isLocalizableByAkeneo($attribute): bool
72
    {
73
        return is_array($attribute) && array_key_exists(static::KEY_LOCALE, $attribute) && $attribute[static::KEY_LOCALE] !== null;
74
    }
75
76
    /**
77
     * @param mixed $attribute
78
     *
79
     * @return bool
80
     */
81
    protected function hasLocaleKeysInData($attribute): bool
82
    {
83
        return is_array($attribute) && is_array($attribute[static::KEY_DATA] ?? null) && $this->hasLocaleKey(array_keys($attribute[static::KEY_DATA]));
84
    }
85
86
    /**
87
     * @param mixed $keys
88
     *
89
     * @return bool
90
     */
91
    protected function hasLocaleKey($keys): bool
92
    {
93
        foreach ($this->getLocales() as $locale) {
94
            if (in_array($locale, $keys, true)) {
95
                return true;
96
            }
97
        }
98
99
        return false;
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    protected function getLocales(): array
106
    {
107
        return $this->options['locales'];
108
    }
109
}
110