DataImportProductConcreteWriteStream::write()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 29
rs 8.8333
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\Stream\DataImport;
9
10
use SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface;
11
use SprykerMiddleware\Shared\Process\Stream\WriteStreamInterface;
0 ignored issues
show
Bug introduced by
The type SprykerMiddleware\Shared...am\WriteStreamInterface 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\Exception\MethodNotSupportedException;
0 ignored issues
show
Bug introduced by
The type SprykerMiddleware\Zed\Pr...odNotSupportedException 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 DataImportProductConcreteWriteStream implements WriteStreamInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected const KEY_ABSTRACT_SKU = 'abstract_sku';
20
21
    /**
22
     * @var string
23
     */
24
    protected const KEY_CONCRETE_SKU = 'concrete_sku';
25
26
    /**
27
     * @var string
28
     */
29
    protected const KEY_PRICES = 'prices';
30
31
    /**
32
     * @var string
33
     */
34
    protected const KEY_STORES = 'stores';
35
36
    /**
37
     * @var string
38
     */
39
    protected const ABSTRACT_PRODUCT_CREATION_IDENTIFIER = 'abstract_product_creation';
40
41
    /**
42
     * @var \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface
43
     */
44
    protected $dataImporterConcretePlugin;
45
46
    /**
47
     * @var \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface
48
     */
49
    protected $dataImporterAbstractPlugin;
50
51
    /**
52
     * @var \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface
53
     */
54
    protected $dataImporterPricePlugin;
55
56
    /**
57
     * @var \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface
58
     */
59
    protected $dataImportAbstractStoresPlugin;
60
61
    /**
62
     * @var array
63
     */
64
    protected $concreteData;
65
66
    /**
67
     * @var array
68
     */
69
    protected $abstractData;
70
71
    /**
72
     * @var array
73
     */
74
    protected $pricesData;
75
76
    /**
77
     * @var array
78
     */
79
    protected $storesData;
80
81
    /**
82
     * @var int
83
     */
84
    protected $bufferSize;
85
86
    /**
87
     * @param \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface $dataImporterConcretePlugin
88
     * @param \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface $dataImporterAbstractPlugin
89
     * @param \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface $dataImporterPricePlugin
90
     * @param \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Plugin\DataImporterPluginInterface $dataImportAbstractStoresPlugin
91
     * @param int $bufferSize
92
     */
93
    public function __construct(
94
        DataImporterPluginInterface $dataImporterConcretePlugin,
95
        DataImporterPluginInterface $dataImporterAbstractPlugin,
96
        DataImporterPluginInterface $dataImporterPricePlugin,
97
        DataImporterPluginInterface $dataImportAbstractStoresPlugin,
98
        int $bufferSize = 300
99
    ) {
100
        $this->dataImporterConcretePlugin = $dataImporterConcretePlugin;
101
        $this->dataImporterAbstractPlugin = $dataImporterAbstractPlugin;
102
        $this->dataImporterPricePlugin = $dataImporterPricePlugin;
103
        $this->dataImportAbstractStoresPlugin = $dataImportAbstractStoresPlugin;
104
        $this->bufferSize = $bufferSize;
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    public function open(): bool
111
    {
112
        $this->concreteData = [];
113
        $this->abstractData = [];
114
        $this->pricesData = [];
115
        $this->storesData = [];
116
117
        return true;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function close(): bool
124
    {
125
        $this->flush();
126
127
        return true;
128
    }
129
130
    /**
131
     * @param int $offset
132
     * @param int $whence
133
     *
134
     * @throws \SprykerMiddleware\Zed\Process\Business\Exception\MethodNotSupportedException
135
     *
136
     * @return int
137
     */
138
    public function seek(int $offset, int $whence): int
139
    {
140
        throw new MethodNotSupportedException();
141
    }
142
143
    /**
144
     * @throws \SprykerMiddleware\Zed\Process\Business\Exception\MethodNotSupportedException
145
     *
146
     * @return bool
147
     */
148
    public function eof(): bool
149
    {
150
        throw new MethodNotSupportedException();
151
    }
152
153
    /**
154
     * @param array $data
155
     *
156
     * @return int
157
     */
158
    public function write(array $data): int
159
    {
160
        if ($data[static::ABSTRACT_PRODUCT_CREATION_IDENTIFIER]) {
161
            $this->abstractData[] = $data;
162
        }
163
164
        if (is_array($data[static::KEY_PRICES])) {
165
            foreach ($data[static::KEY_PRICES] as $price) {
166
                $price['abstract_sku'] = $data[static::KEY_ABSTRACT_SKU];
167
                $this->pricesData[] = $price;
168
            }
169
        }
170
171
        if (is_array($data[static::KEY_STORES])) {
172
            foreach ($data[static::KEY_STORES] as $store) {
173
                $this->storesData[] = [
174
                    'product_abstract_sku' => $data[static::KEY_ABSTRACT_SKU],
175
                    'store_name' => $store,
176
                ];
177
            }
178
        }
179
180
        $this->concreteData[] = $data;
181
182
        if (count($this->concreteData) > $this->bufferSize) {
183
            $this->flush();
184
        }
185
186
        return 1;
187
    }
188
189
    /**
190
     * @return bool
191
     */
192
    public function flush(): bool
193
    {
194
        $this->dataImporterAbstractPlugin->import($this->abstractData);
195
        $this->dataImporterConcretePlugin->import($this->concreteData);
196
        $this->dataImportAbstractStoresPlugin->import($this->storesData);
197
        $this->dataImporterPricePlugin->import($this->pricesData);
198
199
        $this->concreteData = [];
200
        $this->abstractData = [];
201
        $this->storesData = [];
202
        $this->pricesData = [];
203
204
        return true;
205
    }
206
}
207