ProductSetWriterStep   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 354
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 34
eloc 139
dl 0
loc 354
rs 9.68
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findOrCreateProductAbstractSet() 0 23 4
B findOrCreateProductSetData() 0 35 7
A createOrUpdateProductImageAltTexts() 0 20 4
A saveProductImageAltTextGlossaryKeys() 0 19 1
A findOrCreateProductSet() 0 16 3
B findOrCreateProductImageSet() 0 42 9
A execute() 0 7 1
A createOrUpdateGlossaryKey() 0 24 4
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
                $productAbstractSetEntity->save();
198
            }
199
200
            $this->addPublishEvents(ProductEvents::PRODUCT_ABSTRACT_PUBLISH, $idProductAbstract);
201
        }
202
    }
203
204
    /**
205
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
206
     * @param \Orm\Zed\ProductSet\Persistence\SpyProductSet $productSetEntity
207
     *
208
     * @return void
209
     */
210
    protected function findOrCreateProductSetData(DataSetInterface $dataSet, SpyProductSet $productSetEntity): void
211
    {
212
        foreach ($dataSet[LocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) {
213
            if ($localizedAttributes === []) {
214
                continue;
215
            }
216
            $productSetDataEntity = SpyProductSetDataQuery::create()
217
                ->filterByFkProductSet($productSetEntity->getIdProductSet())
218
                ->filterByFkLocale($idLocale)
219
                ->findOneOrCreate();
220
221
            $productSetDataEntity
222
                ->setName($localizedAttributes[static::KEY_NAME])
223
                ->setDescription($localizedAttributes[static::KEY_DESCRIPTION])
224
                ->setMetaTitle($localizedAttributes[static::KEY_META_TITLE])
225
                ->setMetaDescription($localizedAttributes[static::KEY_META_DESCRIPTION])
226
                ->setMetaKeywords($localizedAttributes[static::KEY_META_KEYWORDS]);
227
228
            if ($productSetDataEntity->isNew() || $productSetDataEntity->isModified()) {
229
                $productSetDataEntity->save();
230
            }
231
232
            $productSetUrlEntity = SpyUrlQuery::create()
233
                ->filterByFkResourceProductSet($productSetEntity->getIdProductSet())
234
                ->filterByFkLocale($idLocale)
235
                ->findOneOrCreate();
236
237
            $productSetUrlEntity->setUrl($localizedAttributes[static::KEY_URL]);
238
239
            if (!$productSetUrlEntity->isNew() && !$productSetUrlEntity->isModified()) {
240
                continue;
241
            }
242
243
            $productSetUrlEntity->save();
244
            $this->addPublishEvents(UrlEvents::URL_PUBLISH, $productSetUrlEntity->getIdUrl());
245
        }
246
    }
247
248
    /**
249
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
250
     * @param \Orm\Zed\ProductSet\Persistence\SpyProductSet $productSetEntity
251
     *
252
     * @return void
253
     */
254
    protected function findOrCreateProductImageSet(DataSetInterface $dataSet, SpyProductSet $productSetEntity): void
255
    {
256
        foreach ($dataSet[ProductSetImageExtractorStep::KEY_TARGET] as $imageSetIndex => $imageSet) {
257
            $productImageSetEntity = SpyProductImageSetQuery::create()
258
                ->filterByFkResourceProductSet($productSetEntity->getIdProductSet())
259
                ->filterByName($imageSet[static::KEY_IMAGE_SET])
260
                ->findOneOrCreate();
261
262
            if ($productImageSetEntity->isNew() || $productImageSetEntity->isModified()) {
263
                $productImageSetEntity->save();
264
            }
265
266
            foreach ($imageSet[static::KEY_IMAGES] as $imageIndex => $image) {
267
                $productImageEntity = SpyProductImageQuery::create()
268
                    ->filterByExternalUrlLarge($image[static::KEY_IMAGE_LARGE])
269
                    ->findOneOrCreate();
270
271
                $productImageEntity->setExternalUrlSmall($image[static::KEY_IMAGE_LARGE]);
272
273
                if ($productImageEntity->isNew() || $productImageEntity->isModified()) {
274
                    $productImageEntity->save();
275
                }
276
277
                $productImageEntity = $this->saveProductImageAltTextGlossaryKeys($productImageEntity);
278
                $this->createOrUpdateProductImageAltTexts(
279
                    $dataSet,
280
                    $productImageEntity,
281
                    ProductSetImageExtractorStep::IMAGE_ALT_TEXT_KEY_PREFIX . ProductSetImageExtractorStep::IMAGE_SMALL_KEY_PREFIX . $imageSetIndex . '.' . $imageIndex,
282
                    ProductSetImageExtractorStep::IMAGE_ALT_TEXT_KEY_PREFIX . ProductSetImageExtractorStep::IMAGE_LARGE_KEY_PREFIX . $imageSetIndex . '.' . $imageIndex,
283
                );
284
                $productImageSetToProductImageEntity = SpyProductImageSetToProductImageQuery::create()
285
                    ->filterByFkProductImage($productImageEntity->getIdProductImage())
286
                    ->filterByFkProductImageSet($productImageSetEntity->getIdProductImageSet())
287
                    ->findOneOrCreate();
288
289
                $productImageSetToProductImageEntity->setSortOrder(0);
290
291
                if (!$productImageSetToProductImageEntity->isNew() && !$productImageSetToProductImageEntity->isModified()) {
292
                    continue;
293
                }
294
295
                $productImageSetToProductImageEntity->save();
296
            }
297
        }
298
    }
299
300
    /**
301
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
302
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImage $productImageEntity
303
     * @param string $keyAltTextSmall
304
     * @param string $keyAltTextLarge
305
     *
306
     * @return void
307
     */
308
    protected function createOrUpdateProductImageAltTexts(
309
        DataSetInterface $dataSet,
310
        SpyProductImage $productImageEntity,
311
        string $keyAltTextSmall,
312
        string $keyAltTextLarge,
313
    ): void {
314
        if (!$productImageEntity->getAltTextLarge() || !$productImageEntity->getAltTextSmall()) {
315
            $productImageEntity = $this->saveProductImageAltTextGlossaryKeys($productImageEntity);
316
        }
317
318
        foreach ($dataSet[ProductSetImageLocalizedAttributesExtractorStep::KEY_IMAGE_ALT_TEXT_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) {
319
            $this->createOrUpdateGlossaryKey(
320
                $productImageEntity->getAltTextSmall(),
321
                $idLocale,
322
                $localizedAttributes[$keyAltTextSmall],
323
            );
324
            $this->createOrUpdateGlossaryKey(
325
                $productImageEntity->getAltTextLarge(),
326
                $idLocale,
327
                $localizedAttributes[$keyAltTextLarge],
328
            );
329
        }
330
    }
331
332
    /**
333
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImage $productImageEntity
334
     *
335
     * @return \Orm\Zed\ProductImage\Persistence\SpyProductImage
336
     */
337
    protected function saveProductImageAltTextGlossaryKeys(SpyProductImage $productImageEntity): SpyProductImage
338
    {
339
        $altTextLargeGlossaryKey = sprintf(
340
            '%s.%s.%s',
341
            static::GLOSSARY_KEY_PREFIX,
342
            $productImageEntity->getIdProductImage(),
343
            static::KEY_ALT_TEXT_LARGE,
344
        );
345
        $altTextSmallGlossaryKey = sprintf(
346
            '%s.%s.%s',
347
            static::GLOSSARY_KEY_PREFIX,
348
            $productImageEntity->getIdProductImage(),
349
            static::KEY_ALT_TEXT_SMALL,
350
        );
351
        $productImageEntity->setAltTextLarge($altTextLargeGlossaryKey)
352
            ->setAltTextSmall($altTextSmallGlossaryKey)
353
            ->save();
354
355
        return $productImageEntity;
356
    }
357
358
    /**
359
     * @param string $glossaryKey
360
     * @param int $idLocale
361
     * @param string $value
362
     *
363
     * @return void
364
     */
365
    protected function createOrUpdateGlossaryKey(
366
        string $glossaryKey,
367
        int $idLocale,
368
        string $value,
369
    ): void {
370
        if (!$value) {
371
            return;
372
        }
373
374
        $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
375
            ->filterByKey($glossaryKey)
376
            ->findOneOrCreate();
377
        $glossaryKeyEntity->save();
378
        $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
379
            ->filterByGlossaryKey($glossaryKeyEntity)
380
            ->filterByFkLocale($idLocale)
381
            ->findOneOrCreate();
382
        $glossaryTranslationEntity->setValue($value);
383
384
        if (!$glossaryTranslationEntity->isNew() && !$glossaryTranslationEntity->isModified()) {
385
            return;
386
        }
387
388
        $glossaryTranslationEntity->save();
389
    }
390
}
391