|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Spryker Commerce OS. |
|
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types = 1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Pyz\Zed\DataImport\Business\Model\ProductSet; |
|
11
|
|
|
|
|
12
|
|
|
use Orm\Zed\Glossary\Persistence\SpyGlossaryKeyQuery; |
|
13
|
|
|
use Orm\Zed\Glossary\Persistence\SpyGlossaryTranslationQuery; |
|
14
|
|
|
use Orm\Zed\ProductImage\Persistence\SpyProductImage; |
|
15
|
|
|
use Orm\Zed\ProductImage\Persistence\SpyProductImageQuery; |
|
16
|
|
|
use Orm\Zed\ProductImage\Persistence\SpyProductImageSetQuery; |
|
17
|
|
|
use Orm\Zed\ProductImage\Persistence\SpyProductImageSetToProductImageQuery; |
|
18
|
|
|
use Orm\Zed\ProductSet\Persistence\SpyProductAbstractSetQuery; |
|
19
|
|
|
use Orm\Zed\ProductSet\Persistence\SpyProductSet; |
|
20
|
|
|
use Orm\Zed\ProductSet\Persistence\SpyProductSetDataQuery; |
|
21
|
|
|
use Orm\Zed\ProductSet\Persistence\SpyProductSetQuery; |
|
22
|
|
|
use Orm\Zed\Url\Persistence\SpyUrlQuery; |
|
23
|
|
|
use Pyz\Zed\DataImport\Business\Model\Product\Repository\ProductRepositoryInterface; |
|
24
|
|
|
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface; |
|
25
|
|
|
use Spryker\Zed\DataImport\Business\Model\DataImportStep\LocalizedAttributesExtractorStep; |
|
26
|
|
|
use Spryker\Zed\DataImport\Business\Model\DataImportStep\PublishAwareStep; |
|
27
|
|
|
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface; |
|
28
|
|
|
use Spryker\Zed\Product\Dependency\ProductEvents; |
|
29
|
|
|
use Spryker\Zed\ProductSet\Dependency\ProductSetEvents; |
|
30
|
|
|
use Spryker\Zed\Url\Dependency\UrlEvents; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
34
|
|
|
*/ |
|
35
|
|
|
class ProductSetWriterStep extends PublishAwareStep implements DataImportStepInterface |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var int |
|
39
|
|
|
*/ |
|
40
|
|
|
public const BULK_SIZE = 100; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
public const KEY_PRODUCT_SET_KEY = 'product_set_key'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
public const KEY_NAME = 'name'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
public const KEY_DESCRIPTION = 'description'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
public const KEY_META_TITLE = 'meta_title'; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
public const KEY_META_DESCRIPTION = 'meta_description'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
public const KEY_META_KEYWORDS = 'meta_keywords'; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
public const KEY_URL = 'url'; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
public const KEY_IS_ACTIVE = 'is_active'; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
public const KEY_WEIGHT = 'weight'; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var string |
|
89
|
|
|
*/ |
|
90
|
|
|
public const KEY_ABSTRACT_SKUS = 'abstract_skus'; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @var string |
|
94
|
|
|
*/ |
|
95
|
|
|
public const KEY_IMAGE_SET = 'image_set'; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @var string |
|
99
|
|
|
*/ |
|
100
|
|
|
public const KEY_IMAGES = 'images'; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @var string |
|
104
|
|
|
*/ |
|
105
|
|
|
public const KEY_IMAGE_LARGE = 'image_large'; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected const KEY_ALT_TEXT_SMALL = 'alt_text_small'; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var string |
|
114
|
|
|
*/ |
|
115
|
|
|
protected const KEY_ALT_TEXT_LARGE = 'alt_text_large'; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @var string |
|
119
|
|
|
*/ |
|
120
|
|
|
protected const GLOSSARY_KEY_PREFIX = 'product_image'; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @var \Pyz\Zed\DataImport\Business\Model\Product\Repository\ProductRepositoryInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
protected $productRepository; |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param \Pyz\Zed\DataImport\Business\Model\Product\Repository\ProductRepositoryInterface $productRepository |
|
129
|
|
|
*/ |
|
130
|
|
|
public function __construct(ProductRepositoryInterface $productRepository) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->productRepository = $productRepository; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet |
|
137
|
|
|
* |
|
138
|
|
|
* @return void |
|
139
|
|
|
*/ |
|
140
|
|
|
public function execute(DataSetInterface $dataSet): void |
|
141
|
|
|
{ |
|
142
|
|
|
$productSetEntity = $this->findOrCreateProductSet($dataSet); |
|
143
|
|
|
|
|
144
|
|
|
$this->findOrCreateProductAbstractSet($dataSet, $productSetEntity); |
|
145
|
|
|
$this->findOrCreateProductSetData($dataSet, $productSetEntity); |
|
146
|
|
|
$this->findOrCreateProductImageSet($dataSet, $productSetEntity); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet |
|
151
|
|
|
* |
|
152
|
|
|
* @return \Orm\Zed\ProductSet\Persistence\SpyProductSet |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function findOrCreateProductSet(DataSetInterface $dataSet): SpyProductSet |
|
155
|
|
|
{ |
|
156
|
|
|
$productSetEntity = SpyProductSetQuery::create() |
|
157
|
|
|
->filterByProductSetKey($dataSet[static::KEY_PRODUCT_SET_KEY]) |
|
158
|
|
|
->findOneOrCreate(); |
|
159
|
|
|
|
|
160
|
|
|
$productSetEntity |
|
161
|
|
|
->setIsActive($dataSet[static::KEY_IS_ACTIVE]) |
|
162
|
|
|
->setWeight($dataSet[static::KEY_WEIGHT]); |
|
163
|
|
|
|
|
164
|
|
|
if ($productSetEntity->isNew() || $productSetEntity->isModified()) { |
|
165
|
|
|
$productSetEntity->save(); |
|
166
|
|
|
$this->addPublishEvents(ProductSetEvents::PRODUCT_SET_PUBLISH, $productSetEntity->getIdProductSet()); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $productSetEntity; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet |
|
174
|
|
|
* @param \Orm\Zed\ProductSet\Persistence\SpyProductSet $productSetEntity |
|
175
|
|
|
* |
|
176
|
|
|
* @return void |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function findOrCreateProductAbstractSet(DataSetInterface $dataSet, SpyProductSet $productSetEntity): void |
|
179
|
|
|
{ |
|
180
|
|
|
$productAbstractSkus = explode(',', $dataSet[static::KEY_ABSTRACT_SKUS]); |
|
181
|
|
|
$productAbstractSkus = array_map('trim', $productAbstractSkus); |
|
182
|
|
|
|
|
183
|
|
|
$position = 0; |
|
184
|
|
|
|
|
185
|
|
|
foreach ($productAbstractSkus as $productAbstractSku) { |
|
186
|
|
|
$idProductAbstract = $this->productRepository->getIdProductAbstractByAbstractSku($productAbstractSku); |
|
187
|
|
|
|
|
188
|
|
|
$productAbstractSetEntity = SpyProductAbstractSetQuery::create() |
|
189
|
|
|
->filterByFkProductSet($productSetEntity->getIdProductSet()) |
|
190
|
|
|
->filterByFkProductAbstract($idProductAbstract) |
|
191
|
|
|
->findOneOrCreate(); |
|
192
|
|
|
|
|
193
|
|
|
$position++; |
|
194
|
|
|
$productAbstractSetEntity->setPosition($position); |
|
195
|
|
|
|
|
196
|
|
|
if (!$productAbstractSetEntity->isNew() && !$productAbstractSetEntity->isModified()) { |
|
197
|
|
|
continue; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$productAbstractSetEntity->save(); |
|
201
|
|
|
|
|
202
|
|
|
$this->addPublishEvents(ProductEvents::PRODUCT_ABSTRACT_PUBLISH, $idProductAbstract); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet |
|
208
|
|
|
* @param \Orm\Zed\ProductSet\Persistence\SpyProductSet $productSetEntity |
|
209
|
|
|
* |
|
210
|
|
|
* @return void |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function findOrCreateProductSetData(DataSetInterface $dataSet, SpyProductSet $productSetEntity): void |
|
213
|
|
|
{ |
|
214
|
|
|
foreach ($dataSet[LocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) { |
|
215
|
|
|
if ($localizedAttributes === []) { |
|
216
|
|
|
continue; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$productSetDataEntity = SpyProductSetDataQuery::create() |
|
220
|
|
|
->filterByFkProductSet($productSetEntity->getIdProductSet()) |
|
221
|
|
|
->filterByFkLocale($idLocale) |
|
222
|
|
|
->findOneOrCreate(); |
|
223
|
|
|
|
|
224
|
|
|
$productSetDataEntity |
|
225
|
|
|
->setName($localizedAttributes[static::KEY_NAME]) |
|
226
|
|
|
->setDescription($localizedAttributes[static::KEY_DESCRIPTION]) |
|
227
|
|
|
->setMetaTitle($localizedAttributes[static::KEY_META_TITLE]) |
|
228
|
|
|
->setMetaDescription($localizedAttributes[static::KEY_META_DESCRIPTION]) |
|
229
|
|
|
->setMetaKeywords($localizedAttributes[static::KEY_META_KEYWORDS]); |
|
230
|
|
|
|
|
231
|
|
|
if ($productSetDataEntity->isNew() || $productSetDataEntity->isModified()) { |
|
232
|
|
|
$productSetDataEntity->save(); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$productSetUrlEntity = SpyUrlQuery::create() |
|
236
|
|
|
->filterByFkResourceProductSet($productSetEntity->getIdProductSet()) |
|
237
|
|
|
->filterByFkLocale($idLocale) |
|
238
|
|
|
->findOneOrCreate(); |
|
239
|
|
|
|
|
240
|
|
|
$productSetUrlEntity->setUrl($localizedAttributes[static::KEY_URL]); |
|
241
|
|
|
|
|
242
|
|
|
if (!$productSetUrlEntity->isNew() && !$productSetUrlEntity->isModified()) { |
|
243
|
|
|
continue; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$productSetUrlEntity->save(); |
|
247
|
|
|
$this->addPublishEvents(UrlEvents::URL_PUBLISH, $productSetUrlEntity->getIdUrl()); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet |
|
253
|
|
|
* @param \Orm\Zed\ProductSet\Persistence\SpyProductSet $productSetEntity |
|
254
|
|
|
* |
|
255
|
|
|
* @return void |
|
256
|
|
|
*/ |
|
257
|
|
|
protected function findOrCreateProductImageSet(DataSetInterface $dataSet, SpyProductSet $productSetEntity): void |
|
258
|
|
|
{ |
|
259
|
|
|
foreach ($dataSet[ProductSetImageExtractorStep::KEY_TARGET] as $imageSetIndex => $imageSet) { |
|
260
|
|
|
$productImageSetEntity = SpyProductImageSetQuery::create() |
|
261
|
|
|
->filterByFkResourceProductSet($productSetEntity->getIdProductSet()) |
|
262
|
|
|
->filterByName($imageSet[static::KEY_IMAGE_SET]) |
|
263
|
|
|
->findOneOrCreate(); |
|
264
|
|
|
|
|
265
|
|
|
if ($productImageSetEntity->isNew() || $productImageSetEntity->isModified()) { |
|
266
|
|
|
$productImageSetEntity->save(); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
foreach ($imageSet[static::KEY_IMAGES] as $imageIndex => $image) { |
|
270
|
|
|
$productImageEntity = SpyProductImageQuery::create() |
|
271
|
|
|
->filterByExternalUrlLarge($image[static::KEY_IMAGE_LARGE]) |
|
272
|
|
|
->findOneOrCreate(); |
|
273
|
|
|
|
|
274
|
|
|
$productImageEntity->setExternalUrlSmall($image[static::KEY_IMAGE_LARGE]); |
|
275
|
|
|
|
|
276
|
|
|
if ($productImageEntity->isNew() || $productImageEntity->isModified()) { |
|
277
|
|
|
$productImageEntity->save(); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
$productImageEntity = $this->saveProductImageAltTextGlossaryKeys($productImageEntity); |
|
281
|
|
|
$this->createOrUpdateProductImageAltTexts( |
|
282
|
|
|
$dataSet, |
|
283
|
|
|
$productImageEntity, |
|
284
|
|
|
ProductSetImageExtractorStep::IMAGE_ALT_TEXT_KEY_PREFIX . ProductSetImageExtractorStep::IMAGE_SMALL_KEY_PREFIX . $imageSetIndex . '.' . $imageIndex, |
|
285
|
|
|
ProductSetImageExtractorStep::IMAGE_ALT_TEXT_KEY_PREFIX . ProductSetImageExtractorStep::IMAGE_LARGE_KEY_PREFIX . $imageSetIndex . '.' . $imageIndex, |
|
286
|
|
|
); |
|
287
|
|
|
|
|
288
|
|
|
$productImageSetToProductImageEntity = SpyProductImageSetToProductImageQuery::create() |
|
289
|
|
|
->filterByFkProductImage($productImageEntity->getIdProductImage()) |
|
290
|
|
|
->filterByFkProductImageSet($productImageSetEntity->getIdProductImageSet()) |
|
291
|
|
|
->findOneOrCreate(); |
|
292
|
|
|
|
|
293
|
|
|
$productImageSetToProductImageEntity->setSortOrder(0); |
|
294
|
|
|
|
|
295
|
|
|
if (!$productImageSetToProductImageEntity->isNew() && !$productImageSetToProductImageEntity->isModified()) { |
|
296
|
|
|
continue; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
$productImageSetToProductImageEntity->save(); |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet |
|
306
|
|
|
* @param \Orm\Zed\ProductImage\Persistence\SpyProductImage $productImageEntity |
|
307
|
|
|
* @param string $keyAltTextSmall |
|
308
|
|
|
* @param string $keyAltTextLarge |
|
309
|
|
|
* |
|
310
|
|
|
* @return void |
|
311
|
|
|
*/ |
|
312
|
|
|
protected function createOrUpdateProductImageAltTexts( |
|
313
|
|
|
DataSetInterface $dataSet, |
|
314
|
|
|
SpyProductImage $productImageEntity, |
|
315
|
|
|
string $keyAltTextSmall, |
|
316
|
|
|
string $keyAltTextLarge, |
|
317
|
|
|
): void { |
|
318
|
|
|
if (!$productImageEntity->getAltTextLarge() || !$productImageEntity->getAltTextSmall()) { |
|
319
|
|
|
$productImageEntity = $this->saveProductImageAltTextGlossaryKeys($productImageEntity); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
foreach ($dataSet[ProductSetImageLocalizedAttributesExtractorStep::KEY_IMAGE_ALT_TEXT_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) { |
|
323
|
|
|
$this->createOrUpdateGlossaryKey( |
|
324
|
|
|
$productImageEntity->getAltTextSmall(), |
|
325
|
|
|
$idLocale, |
|
326
|
|
|
$localizedAttributes[$keyAltTextSmall], |
|
327
|
|
|
); |
|
328
|
|
|
$this->createOrUpdateGlossaryKey( |
|
329
|
|
|
$productImageEntity->getAltTextLarge(), |
|
330
|
|
|
$idLocale, |
|
331
|
|
|
$localizedAttributes[$keyAltTextLarge], |
|
332
|
|
|
); |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* @param \Orm\Zed\ProductImage\Persistence\SpyProductImage $productImageEntity |
|
338
|
|
|
* |
|
339
|
|
|
* @return \Orm\Zed\ProductImage\Persistence\SpyProductImage |
|
340
|
|
|
*/ |
|
341
|
|
|
protected function saveProductImageAltTextGlossaryKeys(SpyProductImage $productImageEntity): SpyProductImage |
|
342
|
|
|
{ |
|
343
|
|
|
$altTextLargeGlossaryKey = sprintf( |
|
344
|
|
|
'%s.%s.%s', |
|
345
|
|
|
static::GLOSSARY_KEY_PREFIX, |
|
346
|
|
|
$productImageEntity->getIdProductImage(), |
|
347
|
|
|
static::KEY_ALT_TEXT_LARGE, |
|
348
|
|
|
); |
|
349
|
|
|
$altTextSmallGlossaryKey = sprintf( |
|
350
|
|
|
'%s.%s.%s', |
|
351
|
|
|
static::GLOSSARY_KEY_PREFIX, |
|
352
|
|
|
$productImageEntity->getIdProductImage(), |
|
353
|
|
|
static::KEY_ALT_TEXT_SMALL, |
|
354
|
|
|
); |
|
355
|
|
|
$productImageEntity->setAltTextLarge($altTextLargeGlossaryKey) |
|
356
|
|
|
->setAltTextSmall($altTextSmallGlossaryKey) |
|
357
|
|
|
->save(); |
|
358
|
|
|
|
|
359
|
|
|
return $productImageEntity; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* @param string $glossaryKey |
|
364
|
|
|
* @param int $idLocale |
|
365
|
|
|
* @param string $value |
|
366
|
|
|
* |
|
367
|
|
|
* @return void |
|
368
|
|
|
*/ |
|
369
|
|
|
protected function createOrUpdateGlossaryKey( |
|
370
|
|
|
string $glossaryKey, |
|
371
|
|
|
int $idLocale, |
|
372
|
|
|
string $value, |
|
373
|
|
|
): void { |
|
374
|
|
|
if (!$value) { |
|
375
|
|
|
return; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
$glossaryKeyEntity = SpyGlossaryKeyQuery::create() |
|
379
|
|
|
->filterByKey($glossaryKey) |
|
380
|
|
|
->findOneOrCreate(); |
|
381
|
|
|
$glossaryKeyEntity->save(); |
|
382
|
|
|
$glossaryTranslationEntity = SpyGlossaryTranslationQuery::create() |
|
383
|
|
|
->filterByGlossaryKey($glossaryKeyEntity) |
|
384
|
|
|
->filterByFkLocale($idLocale) |
|
385
|
|
|
->findOneOrCreate(); |
|
386
|
|
|
$glossaryTranslationEntity->setValue($value); |
|
387
|
|
|
|
|
388
|
|
|
if (!$glossaryTranslationEntity->isNew() && !$glossaryTranslationEntity->isModified()) { |
|
389
|
|
|
return; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
$glossaryTranslationEntity->save(); |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
|