DefaultValuesToLocalizedAttributes::translate()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 18
c 1
b 0
f 1
nc 8
nop 2
dl 0
loc 31
rs 9.0444
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 DefaultValuesToLocalizedAttributes extends AbstractTranslatorFunction implements TranslatorFunctionInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected const KEY_LOCALIZED_ATTRIBUTES = 'localizedAttributes';
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 string
32
     */
33
    protected const KEY_ERP_NAME = 'erp_name';
34
35
    /**
36
     * @var string
37
     */
38
    protected const KEY_NAME = 'name';
39
40
    /**
41
     * @var string
42
     */
43
    protected const KEY_DESCRIPTION = 'description';
44
45
    /**
46
     * @var array
47
     */
48
    protected $requiredOptions = ['locales'];
49
50
    /**
51
     * @param mixed $value
52
     * @param array $payload
53
     *
54
     * @return array
55
     */
56
    public function translate($value, array $payload): array
57
    {
58
        $localizedAttributes = [];
59
60
        foreach ($value as $key => $scopedAttributes) {
61
            foreach ($scopedAttributes as $attribute) {
62
                if ($this->isLocalizableByAkeneo($attribute)) {
63
                    $localizedAttributes = $this->addToLocalizedAttributes(
64
                        $key,
65
                        $attribute[static::KEY_DATA],
66
                        $attribute[static::KEY_LOCALE],
67
                        $localizedAttributes,
68
                    );
69
                }
70
71
                if ($this->hasLocaleKeysInData($attribute)) {
72
                    foreach ($attribute[static::KEY_DATA] as $locale => $localizedValue) {
73
                        $localizedAttributes = $this->addToLocalizedAttributes(
74
                            $key,
75
                            $localizedValue,
76
                            $locale,
77
                            $localizedAttributes,
78
                        );
79
                    }
80
                }
81
            }
82
        }
83
84
        $value[static::KEY_LOCALIZED_ATTRIBUTES] = $localizedAttributes;
85
86
        return $value;
87
    }
88
89
    /**
90
     * @param mixed $attribute
91
     *
92
     * @return bool
93
     */
94
    protected function isLocalizableByAkeneo($attribute): bool
95
    {
96
        return is_array($attribute) && array_key_exists(static::KEY_LOCALE, $attribute) && $attribute[static::KEY_LOCALE] !== null;
97
    }
98
99
    /**
100
     * @param mixed $attribute
101
     *
102
     * @return bool
103
     */
104
    protected function hasLocaleKeysInData($attribute): bool
105
    {
106
        return is_array($attribute) && is_array($attribute[static::KEY_DATA] ?? null) && $this->hasLocaleKey(array_keys($attribute[static::KEY_DATA]));
107
    }
108
109
    /**
110
     * @param mixed $keys
111
     *
112
     * @return bool
113
     */
114
    protected function hasLocaleKey($keys): bool
115
    {
116
        foreach ($this->getLocales() as $locale) {
117
            if (in_array($locale, $keys, true)) {
118
                return true;
119
            }
120
        }
121
122
        return false;
123
    }
124
125
    /**
126
     * @param string $key
127
     * @param mixed $value
128
     * @param string $locale
129
     * @param array $localizedAttributes
130
     *
131
     * @return array
132
     */
133
    protected function addToLocalizedAttributes(string $key, $value, string $locale, array $localizedAttributes): array
134
    {
135
        if (!array_key_exists($locale, $localizedAttributes)) {
136
            $localizedAttributes[$locale] = [];
137
        }
138
        if ($key === static::KEY_ERP_NAME) {
139
            $localizedAttributes[$locale][static::KEY_NAME] = $value;
140
141
            return $localizedAttributes;
142
        }
143
        $localizedAttributes[$locale][$key] = $value;
144
145
        return $localizedAttributes;
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    protected function getLocales(): array
152
    {
153
        return $this->options['locales'];
154
    }
155
}
156