|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\FactFinderSdk\Business\Exporter; |
|
9
|
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\CurrencyTransfer; |
|
|
|
|
|
|
11
|
|
|
use Generated\Shared\Transfer\LocaleTransfer; |
|
|
|
|
|
|
12
|
|
|
use Generated\Shared\Transfer\StoreTransfer; |
|
|
|
|
|
|
13
|
|
|
use Orm\Zed\Product\Persistence\Base\SpyProductAbstractQuery; |
|
|
|
|
|
|
14
|
|
|
use SprykerEco\Shared\FactFinderSdk\FactFinderSdkConstants; |
|
15
|
|
|
use SprykerEco\Zed\FactFinderSdk\Business\Writer\FileWriterInterface; |
|
16
|
|
|
use SprykerEco\Zed\FactFinderSdk\Dependency\Facade\FactFinderSdkToCurrencyInterface; |
|
17
|
|
|
use SprykerEco\Zed\FactFinderSdk\Dependency\Facade\FactFinderSdkToStoreInterface; |
|
18
|
|
|
use SprykerEco\Zed\FactFinderSdk\FactFinderSdkConfig; |
|
19
|
|
|
use SprykerEco\Zed\FactFinderSdk\Persistence\FactFinderSdkQueryContainerInterface; |
|
20
|
|
|
|
|
21
|
|
|
class FactFinderSdkProductExporter implements FactFinderSdkProductExporterInterface |
|
22
|
|
|
{ |
|
23
|
|
|
public const VIRTUAL_COLUMN_NAME = 'name'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \SprykerEco\Zed\FactFinderSdk\Business\Writer\FileWriterInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $fileWriter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Generated\Shared\Transfer\LocaleTransfer |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $localeTransfer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $filePath; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $queryLimit; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $fileNamePrefix; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $fileNameDelimiter; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $fileExtension; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var \SprykerEco\Zed\FactFinderSdk\Persistence\FactFinderSdkQueryContainerInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $factFinderQueryContainer; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var \SprykerEco\Zed\FactFinderSdk\FactFinderSdkConfig |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $factFinderConfig; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var \SprykerEco\Zed\FactFinderSdk\Dependency\Facade\FactFinderSdkToCurrencyInterface |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $currencyFacade; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var \SprykerEco\Zed\FactFinderSdk\Dependency\Facade\FactFinderSdkToStoreInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $storeFacade; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var array |
|
82
|
|
|
*/ |
|
83
|
|
|
private $expanders; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param \SprykerEco\Zed\FactFinderSdk\Business\Writer\FileWriterInterface $fileWriter |
|
87
|
|
|
* @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer |
|
88
|
|
|
* @param \SprykerEco\Zed\FactFinderSdk\FactFinderSdkConfig $factFinderConfig |
|
89
|
|
|
* @param \SprykerEco\Zed\FactFinderSdk\Persistence\FactFinderSdkQueryContainerInterface $factFinderQueryContainer |
|
90
|
|
|
* @param \SprykerEco\Zed\FactFinderSdk\Dependency\Facade\FactFinderSdkToCurrencyInterface $currencyFacade |
|
91
|
|
|
* @param \SprykerEco\Zed\FactFinderSdk\Dependency\Facade\FactFinderSdkToStoreInterface $storeFacade |
|
92
|
|
|
* @param \SprykerEco\Zed\FactFinderSdk\Business\Expander\FactFinderSdkExpanderInterface[] $expanders |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __construct( |
|
95
|
|
|
FileWriterInterface $fileWriter, |
|
96
|
|
|
LocaleTransfer $localeTransfer, |
|
97
|
|
|
FactFinderSdkConfig $factFinderConfig, |
|
98
|
|
|
FactFinderSdkQueryContainerInterface $factFinderQueryContainer, |
|
99
|
|
|
FactFinderSdkToCurrencyInterface $currencyFacade, |
|
100
|
|
|
FactFinderSdkToStoreInterface $storeFacade, |
|
101
|
|
|
$expanders |
|
102
|
|
|
) { |
|
103
|
|
|
$this->fileWriter = $fileWriter; |
|
104
|
|
|
$this->localeTransfer = $localeTransfer; |
|
105
|
|
|
$this->queryLimit = $factFinderConfig->getExportQueryLimit(); |
|
106
|
|
|
$this->fileNamePrefix = $factFinderConfig->getExportFileNamePrefix(); |
|
107
|
|
|
$this->fileNameDelimiter = $factFinderConfig->getExportFileNameDelimiter(); |
|
108
|
|
|
$this->fileExtension = $factFinderConfig->getExportFileExtension(); |
|
109
|
|
|
$this->factFinderQueryContainer = $factFinderQueryContainer; |
|
110
|
|
|
$this->factFinderConfig = $factFinderConfig; |
|
111
|
|
|
$this->currencyFacade = $currencyFacade; |
|
112
|
|
|
$this->storeFacade = $storeFacade; |
|
113
|
|
|
$this->expanders = $expanders; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function export() |
|
120
|
|
|
{ |
|
121
|
|
|
$currency = $this->currencyFacade->getDefaultCurrencyForCurrentStore(); |
|
122
|
|
|
$store = $this->storeFacade->getCurrentStore(); |
|
123
|
|
|
|
|
124
|
|
|
$query = $this->factFinderQueryContainer |
|
125
|
|
|
->getExportDataQuery($this->localeTransfer, $store, $currency); |
|
126
|
|
|
|
|
127
|
|
|
if (!$this->productsExists($query)) { |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
$filePath = $this->getFilePath($this->localeTransfer, $store, $currency); |
|
131
|
|
|
|
|
132
|
|
|
$this->exportToCsv($filePath, $query, $currency, $store); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $filePath |
|
137
|
|
|
* @param \Orm\Zed\Product\Persistence\Base\SpyProductAbstractQuery $query |
|
138
|
|
|
* @param \Generated\Shared\Transfer\CurrencyTransfer $currencyTransfer |
|
139
|
|
|
* @param \Generated\Shared\Transfer\StoreTransfer $storeTransfer |
|
140
|
|
|
* |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function exportToCsv($filePath, SpyProductAbstractQuery $query, CurrencyTransfer $currencyTransfer, StoreTransfer $storeTransfer) |
|
144
|
|
|
{ |
|
145
|
|
|
$offset = 0; |
|
146
|
|
|
$this->saveFileHeader($filePath); |
|
147
|
|
|
|
|
148
|
|
|
do { |
|
149
|
|
|
$result = $query->limit($this->queryLimit) |
|
150
|
|
|
->offset($offset) |
|
151
|
|
|
->find()->toArray(FactFinderSdkConstants::ITEM_PRODUCT_NUMBER); |
|
152
|
|
|
$offset += $this->queryLimit; |
|
153
|
|
|
|
|
154
|
|
|
foreach ($result as &$product) { |
|
155
|
|
|
$product = $this->expandData($currencyTransfer, $storeTransfer, $product); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$this->fileWriter |
|
159
|
|
|
->write($filePath, $result, true); |
|
160
|
|
|
} while (!empty($result)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param \Generated\Shared\Transfer\CurrencyTransfer $currencyTransfer |
|
165
|
|
|
* @param \Generated\Shared\Transfer\StoreTransfer $storeTransfer |
|
166
|
|
|
* @param array $product |
|
167
|
|
|
* |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function expandData(CurrencyTransfer $currencyTransfer, StoreTransfer $storeTransfer, $product) |
|
171
|
|
|
{ |
|
172
|
|
|
foreach ($this->expanders as $expander) { |
|
173
|
|
|
$product = $expander->expand($this->localeTransfer, $currencyTransfer, $storeTransfer, $product); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $this->prepareData($product); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param \Orm\Zed\Product\Persistence\Base\SpyProductAbstractQuery $query |
|
181
|
|
|
* |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function productsExists(SpyProductAbstractQuery $query) |
|
185
|
|
|
{ |
|
186
|
|
|
$productsCount = $query->limit($this->queryLimit) |
|
187
|
|
|
->count(); |
|
188
|
|
|
|
|
189
|
|
|
return $productsCount > 0; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function getFileHeader() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->factFinderConfig->getItemFields(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer |
|
202
|
|
|
* @param \Generated\Shared\Transfer\StoreTransfer $storeTransfer |
|
203
|
|
|
* @param \Generated\Shared\Transfer\CurrencyTransfer $currencyTransfer |
|
204
|
|
|
* |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function getFilePath(LocaleTransfer $localeTransfer, StoreTransfer $storeTransfer, CurrencyTransfer $currencyTransfer) |
|
208
|
|
|
{ |
|
209
|
|
|
$directory = $this->factFinderConfig |
|
210
|
|
|
->getCsvDirectory(); |
|
211
|
|
|
$fileNameParts = [ |
|
212
|
|
|
$directory, |
|
213
|
|
|
$this->fileNamePrefix, |
|
214
|
|
|
$this->fileNameDelimiter, |
|
215
|
|
|
$localeTransfer->getLocaleName(), |
|
216
|
|
|
$this->fileNameDelimiter, |
|
217
|
|
|
$storeTransfer->getName(), |
|
218
|
|
|
$this->fileNameDelimiter, |
|
219
|
|
|
$currencyTransfer->getName(), |
|
220
|
|
|
$this->fileExtension, |
|
221
|
|
|
]; |
|
222
|
|
|
|
|
223
|
|
|
return implode('', $fileNameParts); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param string $filePath |
|
228
|
|
|
* |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function saveFileHeader($filePath) |
|
232
|
|
|
{ |
|
233
|
|
|
$header = $this->getFileHeader(); |
|
234
|
|
|
$this->fileWriter->write($filePath, [$header]); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param array $product |
|
239
|
|
|
* |
|
240
|
|
|
* @return array |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function prepareData($product) |
|
243
|
|
|
{ |
|
244
|
|
|
$headers = $this->getFileHeader(); |
|
245
|
|
|
|
|
246
|
|
|
$product = array_filter($product, function ($key) use ($headers) { |
|
247
|
|
|
return in_array($key, $headers); |
|
248
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
249
|
|
|
|
|
250
|
|
|
$product = array_merge(array_flip($headers), $product); |
|
251
|
|
|
|
|
252
|
|
|
return $product; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths