ProductSetWriterStep   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 34
eloc 139
dl 0
loc 249
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
    public const BULK_SIZE = 100;
38
39
    public const KEY_PRODUCT_SET_KEY = 'product_set_key';
40
41
    public const KEY_NAME = 'name';
42
43
    public const KEY_DESCRIPTION = 'description';
44
45
    public const KEY_META_TITLE = 'meta_title';
46
47
    public const KEY_META_DESCRIPTION = 'meta_description';
48
49
    public const KEY_META_KEYWORDS = 'meta_keywords';
50
51
    public const KEY_URL = 'url';
52
53
    public const KEY_IS_ACTIVE = 'is_active';
54
55
    public const KEY_WEIGHT = 'weight';
56
57
    public const KEY_ABSTRACT_SKUS = 'abstract_skus';
58
59
    public const KEY_IMAGE_SET = 'image_set';
60
61
    public const KEY_IMAGES = 'images';
62
63
    public const KEY_IMAGE_LARGE = 'image_large';
64
65
    protected const KEY_ALT_TEXT_SMALL = 'alt_text_small';
66
67
    protected const KEY_ALT_TEXT_LARGE = 'alt_text_large';
68
69
    protected const GLOSSARY_KEY_PREFIX = 'product_image';
70
71
    protected ProductRepositoryInterface $productRepository;
72
73
    public function __construct(ProductRepositoryInterface $productRepository)
74
    {
75
        $this->productRepository = $productRepository;
76
    }
77
78
    public function execute(DataSetInterface $dataSet): void
79
    {
80
        $productSetEntity = $this->findOrCreateProductSet($dataSet);
81
82
        $this->findOrCreateProductAbstractSet($dataSet, $productSetEntity);
83
        $this->findOrCreateProductSetData($dataSet, $productSetEntity);
84
        $this->findOrCreateProductImageSet($dataSet, $productSetEntity);
85
    }
86
87
    protected function findOrCreateProductSet(DataSetInterface $dataSet): SpyProductSet
88
    {
89
        $productSetEntity = SpyProductSetQuery::create()
90
            ->filterByProductSetKey($dataSet[static::KEY_PRODUCT_SET_KEY])
91
            ->findOneOrCreate();
92
93
        $productSetEntity
94
            ->setIsActive($dataSet[static::KEY_IS_ACTIVE])
95
            ->setWeight($dataSet[static::KEY_WEIGHT]);
96
97
        if ($productSetEntity->isNew() || $productSetEntity->isModified()) {
98
            $productSetEntity->save();
99
            $this->addPublishEvents(ProductSetEvents::PRODUCT_SET_PUBLISH, $productSetEntity->getIdProductSet());
100
        }
101
102
        return $productSetEntity;
103
    }
104
105
    protected function findOrCreateProductAbstractSet(DataSetInterface $dataSet, SpyProductSet $productSetEntity): void
106
    {
107
        $productAbstractSkus = explode(',', $dataSet[static::KEY_ABSTRACT_SKUS]);
108
        $productAbstractSkus = array_map('trim', $productAbstractSkus);
109
110
        $position = 0;
111
112
        foreach ($productAbstractSkus as $productAbstractSku) {
113
            $idProductAbstract = $this->productRepository->getIdProductAbstractByAbstractSku($productAbstractSku);
114
115
            $productAbstractSetEntity = SpyProductAbstractSetQuery::create()
116
                ->filterByFkProductSet($productSetEntity->getIdProductSet())
117
                ->filterByFkProductAbstract($idProductAbstract)
118
                ->findOneOrCreate();
119
120
            $position++;
121
            $productAbstractSetEntity->setPosition($position);
122
123
            if ($productAbstractSetEntity->isNew() || $productAbstractSetEntity->isModified()) {
124
                $productAbstractSetEntity->save();
125
            }
126
127
            $this->addPublishEvents(ProductEvents::PRODUCT_ABSTRACT_PUBLISH, $idProductAbstract);
128
        }
129
    }
130
131
    protected function findOrCreateProductSetData(DataSetInterface $dataSet, SpyProductSet $productSetEntity): void
132
    {
133
        foreach ($dataSet[LocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) {
134
            if ($localizedAttributes === []) {
135
                continue;
136
            }
137
            $productSetDataEntity = SpyProductSetDataQuery::create()
138
                ->filterByFkProductSet($productSetEntity->getIdProductSet())
139
                ->filterByFkLocale($idLocale)
140
                ->findOneOrCreate();
141
142
            $productSetDataEntity
143
                ->setName($localizedAttributes[static::KEY_NAME])
144
                ->setDescription($localizedAttributes[static::KEY_DESCRIPTION])
145
                ->setMetaTitle($localizedAttributes[static::KEY_META_TITLE])
146
                ->setMetaDescription($localizedAttributes[static::KEY_META_DESCRIPTION])
147
                ->setMetaKeywords($localizedAttributes[static::KEY_META_KEYWORDS]);
148
149
            if ($productSetDataEntity->isNew() || $productSetDataEntity->isModified()) {
150
                $productSetDataEntity->save();
151
            }
152
153
            $productSetUrlEntity = SpyUrlQuery::create()
154
                ->filterByFkResourceProductSet($productSetEntity->getIdProductSet())
155
                ->filterByFkLocale($idLocale)
156
                ->findOneOrCreate();
157
158
            $productSetUrlEntity->setUrl($localizedAttributes[static::KEY_URL]);
159
160
            if (!$productSetUrlEntity->isNew() && !$productSetUrlEntity->isModified()) {
161
                continue;
162
            }
163
164
            $productSetUrlEntity->save();
165
            $this->addPublishEvents(UrlEvents::URL_PUBLISH, $productSetUrlEntity->getIdUrl());
166
        }
167
    }
168
169
    protected function findOrCreateProductImageSet(DataSetInterface $dataSet, SpyProductSet $productSetEntity): void
170
    {
171
        foreach ($dataSet[ProductSetImageExtractorStep::KEY_TARGET] as $imageSetIndex => $imageSet) {
172
            $productImageSetEntity = SpyProductImageSetQuery::create()
173
                ->filterByFkResourceProductSet($productSetEntity->getIdProductSet())
174
                ->filterByName($imageSet[static::KEY_IMAGE_SET])
175
                ->findOneOrCreate();
176
177
            if ($productImageSetEntity->isNew() || $productImageSetEntity->isModified()) {
178
                $productImageSetEntity->save();
179
            }
180
181
            foreach ($imageSet[static::KEY_IMAGES] as $imageIndex => $image) {
182
                $productImageEntity = SpyProductImageQuery::create()
183
                    ->filterByExternalUrlLarge($image[static::KEY_IMAGE_LARGE])
184
                    ->findOneOrCreate();
185
186
                $productImageEntity->setExternalUrlSmall($image[static::KEY_IMAGE_LARGE]);
187
188
                if ($productImageEntity->isNew() || $productImageEntity->isModified()) {
189
                    $productImageEntity->save();
190
                }
191
192
                $productImageEntity = $this->saveProductImageAltTextGlossaryKeys($productImageEntity);
193
                $this->createOrUpdateProductImageAltTexts(
194
                    $dataSet,
195
                    $productImageEntity,
196
                    ProductSetImageExtractorStep::IMAGE_ALT_TEXT_KEY_PREFIX . ProductSetImageExtractorStep::IMAGE_SMALL_KEY_PREFIX . $imageSetIndex . '.' . $imageIndex,
197
                    ProductSetImageExtractorStep::IMAGE_ALT_TEXT_KEY_PREFIX . ProductSetImageExtractorStep::IMAGE_LARGE_KEY_PREFIX . $imageSetIndex . '.' . $imageIndex,
198
                );
199
                $productImageSetToProductImageEntity = SpyProductImageSetToProductImageQuery::create()
200
                    ->filterByFkProductImage($productImageEntity->getIdProductImage())
201
                    ->filterByFkProductImageSet($productImageSetEntity->getIdProductImageSet())
202
                    ->findOneOrCreate();
203
204
                $productImageSetToProductImageEntity->setSortOrder(0);
205
206
                if (!$productImageSetToProductImageEntity->isNew() && !$productImageSetToProductImageEntity->isModified()) {
207
                    continue;
208
                }
209
210
                $productImageSetToProductImageEntity->save();
211
            }
212
        }
213
    }
214
215
    protected function createOrUpdateProductImageAltTexts(
216
        DataSetInterface $dataSet,
217
        SpyProductImage $productImageEntity,
218
        string $keyAltTextSmall,
219
        string $keyAltTextLarge,
220
    ): void {
221
        if (!$productImageEntity->getAltTextLarge() || !$productImageEntity->getAltTextSmall()) {
222
            $productImageEntity = $this->saveProductImageAltTextGlossaryKeys($productImageEntity);
223
        }
224
225
        foreach ($dataSet[ProductSetImageLocalizedAttributesExtractorStep::KEY_IMAGE_ALT_TEXT_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) {
226
            $this->createOrUpdateGlossaryKey(
227
                $productImageEntity->getAltTextSmall(),
228
                $idLocale,
229
                $localizedAttributes[$keyAltTextSmall],
230
            );
231
            $this->createOrUpdateGlossaryKey(
232
                $productImageEntity->getAltTextLarge(),
233
                $idLocale,
234
                $localizedAttributes[$keyAltTextLarge],
235
            );
236
        }
237
    }
238
239
    protected function saveProductImageAltTextGlossaryKeys(SpyProductImage $productImageEntity): SpyProductImage
240
    {
241
        $altTextLargeGlossaryKey = sprintf(
242
            '%s.%s.%s',
243
            static::GLOSSARY_KEY_PREFIX,
244
            $productImageEntity->getIdProductImage(),
245
            static::KEY_ALT_TEXT_LARGE,
246
        );
247
        $altTextSmallGlossaryKey = sprintf(
248
            '%s.%s.%s',
249
            static::GLOSSARY_KEY_PREFIX,
250
            $productImageEntity->getIdProductImage(),
251
            static::KEY_ALT_TEXT_SMALL,
252
        );
253
        $productImageEntity->setAltTextLarge($altTextLargeGlossaryKey)
254
            ->setAltTextSmall($altTextSmallGlossaryKey)
255
            ->save();
256
257
        return $productImageEntity;
258
    }
259
260
    protected function createOrUpdateGlossaryKey(
261
        string $glossaryKey,
262
        int $idLocale,
263
        string $value,
264
    ): void {
265
        if (!$value) {
266
            return;
267
        }
268
269
        $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
270
            ->filterByKey($glossaryKey)
271
            ->findOneOrCreate();
272
        $glossaryKeyEntity->save();
273
        $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
274
            ->filterByGlossaryKey($glossaryKeyEntity)
275
            ->filterByFkLocale($idLocale)
276
            ->findOneOrCreate();
277
        $glossaryTranslationEntity->setValue($value);
278
279
        if (!$glossaryTranslationEntity->isNew() && !$glossaryTranslationEntity->isModified()) {
280
            return;
281
        }
282
283
        $glossaryTranslationEntity->save();
284
    }
285
}
286