DefaultProductImportDictionary::getDictionary()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 127
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 75
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 127
rs 8.5454

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Dictionary;
9
10
use SprykerEco\Zed\AkeneoPimMiddlewareConnector\AkeneoPimMiddlewareConnectorConfig;
11
use SprykerEco\Zed\AkeneoPimMiddlewareConnector\Business\Translator\TranslatorFunction\DefaultPriceSelector;
12
use SprykerMiddleware\Zed\Process\Business\Translator\Dictionary\AbstractDictionary;
0 ignored issues
show
Bug introduced by
The type SprykerMiddleware\Zed\Pr...nary\AbstractDictionary 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 DefaultProductImportDictionary extends AbstractDictionary
15
{
16
    /**
17
     * @var array
18
     */
19
    protected static $localeMap;
20
21
    /**
22
     * @var array
23
     */
24
    protected static $attributeMap;
25
26
    /**
27
     * @var \SprykerEco\Zed\AkeneoPimMiddlewareConnector\AkeneoPimMiddlewareConnectorConfig
28
     */
29
    private $config;
30
31
    /**
32
     * @param \SprykerEco\Zed\AkeneoPimMiddlewareConnector\AkeneoPimMiddlewareConnectorConfig $config
33
     */
34
    public function __construct(AkeneoPimMiddlewareConnectorConfig $config)
35
    {
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public function getDictionary(): array
43
    {
44
        return [
45
            'values.*' => 'MeasureUnitToInt',
46
            'values.collection.data' => function ($value) {
47
                if (is_array($value)) {
48
                    return reset($value);
49
                }
50
51
                return $value;
52
            },
53
            'parent' => [
54
                'AddAbstractSkuIfNotExist',
55
            ],
56
            'categories' => [
57
                [
58
                    'ArrayToString',
59
                    'options' => [
60
                        'glue' => ',',
61
                    ],
62
                ],
63
            ],
64
            'values' => [
65
                [
66
                    'EnrichAttributes',
67
                    'options' => [
68
                        'map' => $this->getAttributeMap(),
69
                        'excludeKeys' => [
70
                            'country_availability',
71
                        ],
72
                    ],
73
                ],
74
                [
75
                    'ValuesToAttributes',
76
                    'options' => [
77
                        'locales' => $this->config->getLocalesForImport(),
78
                    ],
79
                ],
80
                [
81
                    'DefaultValuesToLocalizedAttributes',
82
                    'options' => [
83
                        'locales' => $this->config->getLocalesForImport(),
84
                    ],
85
                ],
86
            ],
87
            'values.localizedAttributes' => [
88
                [
89
                    'AddMissingLocales',
90
                    'options' => [
91
                        'locales' => $this->config->getLocalesForImport(),
92
                    ],
93
                ],
94
                [
95
                    'LocaleKeysToIds',
96
                    'options' => [
97
                        'map' => $this->getLocaleMap(),
98
                    ],
99
                ],
100
                [
101
                    'MoveLocalizedAttributesToAttributes',
102
                    'options' => [
103
                        'blacklist' => [
104
                            'name',
105
                            'title',
106
                            'product_description',
107
                            'tax_set',
108
                            'is_active_per_locale',
109
                            'price',
110
                            'bild_information',
111
                            'picto_informationen',
112
                            'meta_title',
113
                            'meta_description',
114
                            'meta_keywords',
115
                        ],
116
                    ],
117
                ],
118
            ],
119
            'values.price' => [
120
                [
121
                    'DefaultPriceSelector',
122
                    'options' => [
123
                        DefaultPriceSelector::OPTION_STORES => $this->config->getActiveStoresForProducts(),
124
                        DefaultPriceSelector::OPTION_LOCALE_TO_PRICE_MAP => $this->config->getLocaleToPriceMap(),
125
                    ],
126
                ],
127
            ],
128
            'values.localizedAttributes.*' => [
129
                [
130
                    'ExcludeKeysAssociativeFilter',
131
                    'options' => [
132
                        'excludeKeys' => [
133
                            'price',
134
                            'bild_information',
135
                            'picto_information',
136
                            'tax_set',
137
                        ],
138
                    ],
139
                ],
140
                [
141
                    'AddMissingAttributes',
142
                    'options' => [
143
                        'attributes' => [
144
                            'name' => '',
145
                            'description' => '',
146
                            'meta_title' => '',
147
                            'meta_description' => '',
148
                            'meta_keywords' => '',
149
                            'is_searchable' => true,
150
                        ],
151
                    ],
152
                ],
153
                [
154
                    'AddUrlToLocalizedAttributes',
155
                ],
156
            ],
157
            'values.attributes' => [
158
                [
159
                    'ExcludeKeysAssociativeFilter',
160
                    'options' => [
161
                        'excludeKeys' => [
162
                            'price',
163
                            'country_availability',
164
                        ],
165
                    ],
166
                ],
167
                [
168
                    'AddFamilyAttribute',
169
                ],
170
            ],
171
        ];
172
    }
173
174
    /**
175
     * @return array
176
     */
177
    protected function getLocaleMap(): array
178
    {
179
        if (static::$localeMap === null) {
0 ignored issues
show
introduced by
The condition static::localeMap === null is always false.
Loading history...
180
            $content = file_get_contents($this->config->getLocaleMapFilePath());
181
            static::$localeMap = json_decode($content, true);
182
        }
183
184
        return static::$localeMap;
185
    }
186
187
    /**
188
     * @return array
189
     */
190
    protected function getAttributeMap(): array
191
    {
192
        if (static::$attributeMap === null) {
0 ignored issues
show
introduced by
The condition static::attributeMap === null is always false.
Loading history...
193
            $content = file_get_contents($this->config->getAttributeMapFilePath());
194
            static::$attributeMap = json_decode($content, true);
195
        }
196
197
        $formattedResult = [];
198
199
        foreach (static::$attributeMap as $key => $value) {
200
            $formattedResult[$value['attribute_key']] = $value;
201
        }
202
203
        return $formattedResult;
204
    }
205
}
206