ArticleMapper::mapArticles()   C
last analyzed

Complexity

Conditions 11
Paths 26

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
rs 6.5481
cc 11
eloc 31
nc 26
nop 1

How to fix   Long Method    Complexity   

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
namespace TEiling\Scd16\Mapper;
4
5
use TEiling\Scd16\Config\ConfigInterface;
6
use TEiling\Scd16\Utils\ArticleUtil;
7
8
class ArticleMapper implements ArticleMapperInterface
9
{
10
    private $groupName;
11
12
    /** @var ConfigInterface */
13
    private $config;
14
15
16
    public function __construct(ConfigInterface $config)
17
    {
18
        $this->config = $config;
19
        $this->groupName = $this->config->getGroupName();
20
21
    }
22
23
    /**
24
     * @param array $csv
25
     *
26
     * @return array
27
     */
28
    public function mapArticles($csv)
29
    {
30
        $articles = [];
31
        foreach ($csv as $key => $entry) {
32
            if (strpos($entry['Artikelnummer'], '_1') !== false) {
33
                continue;
34
            }
35
            $article = $this->getArticleArray($entry);
36
            if ($csv[$key + 1]['Artikelnummer'] === $entry['Artikelnummer'] . '_1') {
37
                $secondLine = $csv[$key + 1];
38
                $secondLine['Artikelnummer'] = str_replace('_1', '', $secondLine['Artikelnummer']);
39
                $articleExtended = $this->getArticleArray($secondLine);
40
41
                $article['variants'] = array_merge($article['variants'], $articleExtended['variants']);
42
43
                $groupKeys = array_keys($article['configuratorSet']['groups']);
44
                if (is_array($groupKeys)) {
45
                    foreach ($groupKeys as $groupKey) {
46
                        $article['configuratorSet']['groups'][$groupKey]['options'] = array_merge(
47
                            $article['configuratorSet']['groups'][$groupKey]['options'],
48
                            $articleExtended['configuratorSet']['groups'][$groupKey]['options']
49
                        );
50
                    }
51
                }
52
            }
53
            // if in the first row no variant has a stock but the second row
54
            if ($article['mainDetail']['number'] === false && !empty($articleExtended['mainDetail']['number'])) {
55
                $article['mainDetail']['number'] = $articleExtended['mainDetail']['number'];
56
            }
57
58
            // we need at least one default order number ;-)
59
            if ($article['mainDetail']['number'] === false) {
60
                $stockArray = ArticleUtil::getKeyValueArray($entry['Bestand']);
61
                $article['mainDetail']['number'] = ArticleUtil::convertOrderNumber(
62
                    $entry['Artikelnummer'] . '-' . key($stockArray)
63
                );
64
                foreach ($article['variants'] as &$value) {
65
                    if ($value['number'] === $article['mainDetail']['number']) {
66
                        $value['isMain'] = true;
67
                        break;
68
                    }
69
                }
70
            }
71
72
            // testing category
73
            $article['categories'][] = [
74
                'id' => 4
75
            ];
76
77
78
            $articles[] = $article;
79
        }
80
81
        return $articles;
82
83
    }
84
85
    /**
86
     * @todo fixing magic number
87
     *
88
     * @param $article
89
     *
90
     * @return array
91
     */
92
    private function getArticleArray($article)
93
    {
94
        $articleArray = [
95
            'name' => $article['Hersteller'] . ' ' . $article['Modell'],
96
            'taxId' => 1,
97
            'active' => 1,
98
            'description' => $article['Notiz'],
99
            'lastStock' => $article['Abverkauf'],
100
            'supplier' => $article['Hersteller'],
101
            'mainDetail' => [
102
                'active' => 1,
103
                'inStock' => 0,
104
                'number' => ArticleUtil::getMainOrderNumber($article['Artikelnummer'], $article['Bestand']),
105
                'prices' => [
106
                    [
107
                        'customerGroupKey' => 'EK',
108
                        'price' => ArticleUtil::convertPrice($article['Preis']),
109
                        'pseudoPrice' => ArticleUtil::convertPrice($article['VKS'])
110
                    ],
111
                ]
112
            ]
113
        ];
114
115
        $this->attachArticleVariants($articleArray, $article);
116
117
        return $articleArray;
118
    }
119
120
    /**
121
     * @param $articleArray
122
     * @param $article
123
     */
124
    private function attachArticleVariants(&$articleArray, $article)
125
    {
126
        $stock = ArticleUtil::getKeyValueArray($article['Bestand']);
127
128
        if (key($stock) === '-') {
129
            return;
130
        }
131
132
        $ean = ArticleUtil::getKeyValueArray($article['EAN']);
133
134
        $groupOptions = [];
135
        $variants = [];
136
137
        foreach ($stock as $size => $value) {
138
            $variantNumber = ArticleUtil::convertOrderNumber($article['Artikelnummer'] . '-' . $size);
139
            $variants[] =
140
                [
141
                    'isMain' => $variantNumber === $articleArray['mainDetail']['number'],
142
                    'number' => $variantNumber,
143
                    'ean' => $ean[$size],
144
                    'active' => $stock[$size] > 0,
145
                    'inStock' => $stock[$size],
146
                    'additionalText' => $size,
147
                    'configuratorOptions' => [
148
                        [
149
                            'group' => $this->groupName,
150
                            'option' => $size
151
                        ]
152
                    ],
153
                    'prices' => [
154
                        [
155
                            'price' => ArticleUtil::convertPrice($article['Preis']),
156
                            'pseudoPrice' => ArticleUtil::convertPrice($article['VKS'])
157
                        ],
158
                    ],
159
                ];
160
161
            // add configurator set
162
            $groupOptions[] = ['name' => $size];
163
164
        }
165
        // add configurator set
166
        $articleArray['configuratorSet'] = [
167
            'type' => 1,
168
            'groups' => [
169
                [
170
                    'name' => $this->groupName,
171
                    'options' => $groupOptions
172
                ]
173
            ]
174
        ];
175
176
        $articleArray['variants'] = $variants;
177
    }
178
}
179