Passed
Push — master ( c03d16...8498ca )
by Stanislav
45:46
created

createOrUpdateImageToImageSetRelation()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 20
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 3
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\ProductImage\Writer;
11
12
use Generated\Shared\Transfer\SpyProductImageEntityTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ductImageEntityTransfer 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
use Generated\Shared\Transfer\SpyProductImageSetEntityTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tImageSetEntityTransfer 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...
14
use Generated\Shared\Transfer\SpyProductImageSetToProductImageEntityTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ductImageEntityTransfer 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...
15
use Orm\Zed\Glossary\Persistence\SpyGlossaryKeyQuery;
16
use Orm\Zed\Glossary\Persistence\SpyGlossaryTranslationQuery;
17
use Orm\Zed\ProductImage\Persistence\SpyProductImage;
18
use Orm\Zed\ProductImage\Persistence\SpyProductImageSet;
19
use Pyz\Zed\DataImport\Business\Model\ProductImage\ProductImageHydratorStep;
20
use Pyz\Zed\DataImport\Business\Model\ProductImage\Repository\ProductImageRepositoryInterface;
21
use Spryker\Shared\GlossaryStorage\GlossaryStorageConfig;
22
use Spryker\Zed\DataImport\Business\Model\DataImportStep\LocalizedAttributesExtractorStep;
23
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
24
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetWriterInterface;
25
use Spryker\Zed\DataImport\Business\Model\Publisher\DataImporterPublisher;
26
use Spryker\Zed\Product\Dependency\ProductEvents;
27
use Spryker\Zed\ProductImage\Dependency\ProductImageEvents;
28
29
class ProductImagePropelDataSetWriter implements DataSetWriterInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    public const KEY_ALT_TEXT_SMALL = 'alt_text_small';
35
36
    /**
37
     * @var string
38
     */
39
    public const KEY_ALT_TEXT_LARGE = 'alt_text_large';
40
41
    /**
42
     * @var string
43
     */
44
    protected const GLOSSARY_KEY_PREFIX = 'product_image';
45
46
    /**
47
     * @var \Pyz\Zed\DataImport\Business\Model\ProductImage\Repository\ProductImageRepositoryInterface
48
     */
49
    protected $productImageRepository;
50
51
    /**
52
     * @param \Pyz\Zed\DataImport\Business\Model\ProductImage\Repository\ProductImageRepositoryInterface $productImageRepository
53
     */
54
    public function __construct(ProductImageRepositoryInterface $productImageRepository)
55
    {
56
        $this->productImageRepository = $productImageRepository;
57
    }
58
59
    /**
60
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
61
     *
62
     * @return void
63
     */
64
    public function write(DataSetInterface $dataSet): void
65
    {
66
        $productImageSetEntity = $this->createOrUpdateProductImageSet($dataSet);
67
        $productImageEntity = $this->createOrUpdateProductImage($dataSet, $productImageSetEntity);
68
        $this->createOrUpdateProductImageAltTexts($dataSet, $productImageEntity, $productImageSetEntity);
69
        $this->createOrUpdateImageToImageSetRelation($productImageSetEntity, $productImageEntity, $dataSet);
70
    }
71
72
    /**
73
     * @return void
74
     */
75
    public function flush(): void
76
    {
77
        DataImporterPublisher::triggerEvents();
78
    }
79
80
    /**
81
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
82
     *
83
     * @return \Orm\Zed\ProductImage\Persistence\SpyProductImageSet
84
     */
85
    protected function createOrUpdateProductImageSet(DataSetInterface $dataSet): SpyProductImageSet
86
    {
87
        $productImageSetEntityTransfer = $this->getProductImageSetTransfer($dataSet);
88
        $productImageSetEntity = $this->productImageRepository->getProductImageSetEntity(
89
            $productImageSetEntityTransfer->getName(),
90
            $productImageSetEntityTransfer->getFkLocale(),
91
            (int)$productImageSetEntityTransfer->getFkProductAbstract(),
92
            (int)$productImageSetEntityTransfer->getFkProduct(),
93
            $productImageSetEntityTransfer->getProductImageSetKey(),
94
        );
95
96
        if ($productImageSetEntity->isNew() || $productImageSetEntity->isModified()) {
97
            $productImageSetEntity->save();
98
99
            $this->addImagePublishEvents($productImageSetEntity);
100
        }
101
102
        return $productImageSetEntity;
103
    }
104
105
    /**
106
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
107
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImageSet $productImageSetEntity
108
     *
109
     * @return \Orm\Zed\ProductImage\Persistence\SpyProductImage
110
     */
111
    protected function createOrUpdateProductImage(
112
        DataSetInterface $dataSet,
113
        SpyProductImageSet $productImageSetEntity,
114
    ): SpyProductImage {
115
        $productImageEntityTransfer = $this->getProductImageTransfer($dataSet);
116
        $productImageEntity = $this->findOrCreateProductImageEntityByProductImageKey(
117
            $productImageEntityTransfer->getProductImageKey(),
118
        );
119
120
        $productImageEntity->setExternalUrlLarge($productImageEntityTransfer->getExternalUrlLarge());
121
        $productImageEntity->setExternalUrlSmall($productImageEntityTransfer->getExternalUrlSmall());
122
        $productImageEntity->setProductImageKey($productImageEntityTransfer->getProductImageKey());
123
124
        if ($productImageEntity->isNew() || $productImageEntity->isModified()) {
125
            $productImageEntity->save();
126
127
            $this->addImagePublishEvents($productImageSetEntity);
128
        }
129
130
        return $productImageEntity;
131
    }
132
133
    /**
134
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImageSet $productImageSetEntity
135
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImage $productImageEntity
136
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
137
     *
138
     * @return void
139
     */
140
    protected function createOrUpdateImageToImageSetRelation(
141
        SpyProductImageSet $productImageSetEntity,
142
        SpyProductImage $productImageEntity,
143
        DataSetInterface $dataSet,
144
    ): void {
145
        $productImageSetToProductImageEntity = $this->productImageRepository->getProductImageSetToProductImageRelationEntity(
146
            $productImageSetEntity->getIdProductImageSet(),
147
            $productImageEntity->getIdProductImage(),
148
        );
149
150
        $productImageToImageSetRelationTransfer = $this->getProductImageToImageSetRelationTransfer($dataSet);
151
        $productImageSetToProductImageEntity->setSortOrder($productImageToImageSetRelationTransfer->getSortOrder());
152
153
        if (!$productImageSetToProductImageEntity->isNew() && !$productImageSetToProductImageEntity->isModified()) {
154
            return;
155
        }
156
157
        $productImageSetToProductImageEntity->save();
158
159
        $this->addImagePublishEvents($productImageSetEntity);
160
    }
161
162
    /**
163
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImage $productImageEntity
164
     *
165
     * @return \Orm\Zed\ProductImage\Persistence\SpyProductImage
166
     */
167
    protected function saveProductImageAltTextGlossaryKeys(SpyProductImage $productImageEntity): SpyProductImage
168
    {
169
        $altTextLargeGlossaryKey = sprintf(
170
            '%s.%s.%s',
171
            static::GLOSSARY_KEY_PREFIX,
172
            $productImageEntity->getIdProductImage(),
173
            static::KEY_ALT_TEXT_LARGE,
174
        );
175
        $altTextSmallGlossaryKey = sprintf(
176
            '%s.%s.%s',
177
            static::GLOSSARY_KEY_PREFIX,
178
            $productImageEntity->getIdProductImage(),
179
            static::KEY_ALT_TEXT_SMALL,
180
        );
181
        $productImageEntity->setAltTextLarge($altTextLargeGlossaryKey)
182
            ->setAltTextSmall($altTextSmallGlossaryKey)
183
            ->save();
184
185
        return $productImageEntity;
186
    }
187
188
    /**
189
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
190
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImage $productImageEntity
191
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImageSet $productImageSetEntity
192
     *
193
     * @return void
194
     */
195
    protected function createOrUpdateProductImageAltTexts(
196
        DataSetInterface $dataSet,
197
        SpyProductImage $productImageEntity,
198
        SpyProductImageSet $productImageSetEntity,
199
    ): void {
200
        if (!$productImageEntity->getAltTextLarge() || !$productImageEntity->getAltTextSmall()) {
201
            $productImageEntity = $this->saveProductImageAltTextGlossaryKeys($productImageEntity);
202
            $this->addImagePublishEvents($productImageSetEntity);
203
        }
204
205
        foreach ($dataSet[LocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) {
206
            if ($productImageSetEntity->getFkLocale() && $productImageSetEntity->getFkLocale() !== $idLocale) {
207
                continue;
208
            }
209
210
            $this->createOrUpdateGlossaryKey(
211
                $productImageEntity->getAltTextSmall(),
212
                $idLocale,
213
                $localizedAttributes[static::KEY_ALT_TEXT_SMALL],
214
            );
215
            $this->createOrUpdateGlossaryKey(
216
                $productImageEntity->getAltTextLarge(),
217
                $idLocale,
218
                $localizedAttributes[static::KEY_ALT_TEXT_LARGE],
219
            );
220
        }
221
    }
222
223
    /**
224
     * @param string $glossaryKey
225
     * @param int $idLocale
226
     * @param string $value
227
     *
228
     * @return void
229
     */
230
    protected function createOrUpdateGlossaryKey(
231
        string $glossaryKey,
232
        int $idLocale,
233
        string $value,
234
    ): void {
235
        $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
236
            ->filterByKey($glossaryKey)
237
            ->findOneOrCreate();
238
        $glossaryKeyEntity->save();
239
        $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
240
            ->filterByGlossaryKey($glossaryKeyEntity)
241
            ->filterByFkLocale($idLocale)
242
            ->findOneOrCreate();
243
        $glossaryTranslationEntity->setValue($value);
244
245
        if (!$glossaryTranslationEntity->isNew() && !$glossaryTranslationEntity->isModified()) {
246
            return;
247
        }
248
249
        $glossaryTranslationEntity->save();
250
        DataImporterPublisher::addEvent(
251
            GlossaryStorageConfig::GLOSSARY_KEY_PUBLISH_WRITE,
252
            $glossaryTranslationEntity->getFkGlossaryKey(),
253
        );
254
        DataImporterPublisher::addEvent(
255
            GlossaryStorageConfig::PUBLISH_TRANSLATION,
256
            $glossaryTranslationEntity->getIdGlossaryTranslation(),
257
        );
258
    }
259
260
    /**
261
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
262
     *
263
     * @return \Generated\Shared\Transfer\SpyProductImageEntityTransfer
264
     */
265
    protected function getProductImageTransfer(DataSetInterface $dataSet): SpyProductImageEntityTransfer
266
    {
267
        return $dataSet[ProductImageHydratorStep::DATA_PRODUCT_IMAGE_TRANSFER];
268
    }
269
270
    /**
271
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
272
     *
273
     * @return \Generated\Shared\Transfer\SpyProductImageSetEntityTransfer
274
     */
275
    protected function getProductImageSetTransfer(DataSetInterface $dataSet): SpyProductImageSetEntityTransfer
276
    {
277
        return $dataSet[ProductImageHydratorStep::DATA_PRODUCT_IMAGE_SET_TRANSFER];
278
    }
279
280
    /**
281
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
282
     *
283
     * @return \Generated\Shared\Transfer\SpyProductImageSetToProductImageEntityTransfer
284
     */
285
    protected function getProductImageToImageSetRelationTransfer(
286
        DataSetInterface $dataSet,
287
    ): SpyProductImageSetToProductImageEntityTransfer {
288
        return $dataSet[ProductImageHydratorStep::DATA_PRODUCT_IMAGE_TO_IMAGE_SET_RELATION_TRANSFER];
289
    }
290
291
    /**
292
     * @param \Orm\Zed\ProductImage\Persistence\SpyProductImageSet $productImageSetEntity
293
     *
294
     * @return void
295
     */
296
    protected function addImagePublishEvents(SpyProductImageSet $productImageSetEntity): void
297
    {
298
        if ($productImageSetEntity->getFkProductAbstract()) {
299
            DataImporterPublisher::addEvent(
300
                ProductImageEvents::PRODUCT_IMAGE_PRODUCT_ABSTRACT_PUBLISH,
301
                $productImageSetEntity->getFkProductAbstract(),
302
            );
303
            DataImporterPublisher::addEvent(
304
                ProductEvents::PRODUCT_ABSTRACT_PUBLISH,
305
                $productImageSetEntity->getFkProductAbstract(),
306
            );
307
        } elseif ($productImageSetEntity->getFkProduct()) {
308
            DataImporterPublisher::addEvent(
309
                ProductImageEvents::PRODUCT_IMAGE_PRODUCT_CONCRETE_PUBLISH,
310
                $productImageSetEntity->getFkProduct(),
311
            );
312
        }
313
    }
314
315
    /**
316
     * @param string $productImageKey
317
     *
318
     * @return \Orm\Zed\ProductImage\Persistence\SpyProductImage
319
     */
320
    protected function findOrCreateProductImageEntityByProductImageKey(string $productImageKey): SpyProductImage
321
    {
322
        return $this->productImageRepository->getProductImageEntity($productImageKey);
323
    }
324
}
325