ProductManagementAttributeWriter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 49
dl 0
loc 81
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 69 7
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\ProductManagementAttribute;
11
12
use Orm\Zed\Glossary\Persistence\SpyGlossaryKeyQuery;
13
use Orm\Zed\Glossary\Persistence\SpyGlossaryTranslationQuery;
14
use Orm\Zed\ProductAttribute\Persistence\SpyProductManagementAttributeQuery;
15
use Orm\Zed\ProductAttribute\Persistence\SpyProductManagementAttributeValueQuery;
16
use Orm\Zed\ProductAttribute\Persistence\SpyProductManagementAttributeValueTranslation;
17
use Pyz\Zed\DataImport\Business\Model\ProductAttributeKey\AddProductAttributeKeysStep;
18
use Spryker\Shared\ProductAttribute\ProductAttributeConfig;
19
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
20
use Spryker\Zed\DataImport\Business\Model\DataImportStep\PublishAwareStep;
21
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
22
use Spryker\Zed\Glossary\Dependency\GlossaryEvents;
23
24
class ProductManagementAttributeWriter extends PublishAwareStep implements DataImportStepInterface
25
{
26
    /**
27
     * @var int
28
     */
29
    public const BULK_SIZE = 100;
30
31
    /**
32
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
33
     *
34
     * @return void
35
     */
36
    public function execute(DataSetInterface $dataSet): void
37
    {
38
        $productManagementAttributeEntity = SpyProductManagementAttributeQuery::create()
39
            ->filterByFkProductAttributeKey($dataSet[AddProductAttributeKeysStep::KEY_TARGET][$dataSet['key']])
40
            ->findOneOrCreate();
41
42
        $productManagementAttributeEntity
43
            ->setAllowInput($dataSet['allow_input'])
44
            ->setInputType($dataSet['input_type']);
45
46
        $productManagementAttributeEntity->save();
47
48
        $productManagementAttributeValueEntityCollection = SpyProductManagementAttributeValueQuery::create()
49
            ->findByFkProductManagementAttribute($productManagementAttributeEntity->getIdProductManagementAttribute());
50
51
        foreach ($productManagementAttributeValueEntityCollection as $productManagementAttributeValueEntity) {
52
            foreach ($productManagementAttributeValueEntity->getSpyProductManagementAttributeValueTranslations() as $productManagementAttributeValueTranslation) {
53
                $productManagementAttributeValueTranslation->delete();
54
            }
55
56
            $productManagementAttributeValueEntity->delete();
57
        }
58
59
        $glossaryKey = ProductAttributeConfig::PRODUCT_ATTRIBUTE_GLOSSARY_PREFIX . $dataSet['key'];
60
        $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
61
            ->filterByKey($glossaryKey)
62
            ->findOneOrCreate();
63
64
        $glossaryKeyEntity->save();
65
66
        foreach ($dataSet[ProductManagementLocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $attributes) {
67
            $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
68
                ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
69
                ->filterByFkLocale($idLocale)
70
                ->findOneOrCreate();
71
72
            $glossaryTranslationEntity
73
                ->setValue($attributes['key_translation'])
74
                ->save();
75
76
            $this->addPublishEvents(GlossaryEvents::GLOSSARY_KEY_PUBLISH, $glossaryTranslationEntity->getFkGlossaryKey());
0 ignored issues
show
Deprecated Code introduced by
The constant Spryker\Zed\Glossary\Dep...s::GLOSSARY_KEY_PUBLISH has been deprecated: Use {@link \Spryker\Shared\GlossaryStorage\GlossaryStorageConfig::GLOSSARY_KEY_PUBLISH_WRITE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

76
            $this->addPublishEvents(/** @scrutinizer ignore-deprecated */ GlossaryEvents::GLOSSARY_KEY_PUBLISH, $glossaryTranslationEntity->getFkGlossaryKey());

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
77
78
            if (!empty($attributes['value_translations'])) {
79
                foreach ($attributes['value_translations'] as $value => $translation) {
80
                    $productManagementAttributeValueEntity = SpyProductManagementAttributeValueQuery::create()
81
                        ->filterBySpyProductManagementAttribute($productManagementAttributeEntity)
82
                        ->filterByValue($value)
83
                        ->findOneOrCreate();
84
85
                    $productManagementAttributeValueEntity->save();
86
87
                    $productManagementAttributeValueTranslationEntity = new SpyProductManagementAttributeValueTranslation();
88
                    $productManagementAttributeValueTranslationEntity
89
                        ->setSpyProductManagementAttributeValue($productManagementAttributeValueEntity)
90
                        ->setTranslation($translation)
91
                        ->setFkLocale($idLocale)
92
                        ->save();
93
                }
94
95
                continue;
96
            }
97
98
            foreach ($attributes['values'] as $value) {
99
                $productManagementAttributeValueEntity = SpyProductManagementAttributeValueQuery::create()
100
                    ->filterBySpyProductManagementAttribute($productManagementAttributeEntity)
101
                    ->filterByValue($value)
102
                    ->findOneOrCreate();
103
104
                $productManagementAttributeValueEntity->save();
105
            }
106
        }
107
    }
108
}
109