ProductManagementAttributeWriter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

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

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
    public const BULK_SIZE = 100;
27
28
    public function execute(DataSetInterface $dataSet): void
29
    {
30
        $productManagementAttributeEntity = SpyProductManagementAttributeQuery::create()
31
            ->filterByFkProductAttributeKey($dataSet[AddProductAttributeKeysStep::KEY_TARGET][$dataSet['key']])
32
            ->findOneOrCreate();
33
34
        $productManagementAttributeEntity
35
            ->setAllowInput($dataSet['allow_input'])
36
            ->setInputType($dataSet['input_type']);
37
38
        $productManagementAttributeEntity->save();
39
40
        $productManagementAttributeValueEntityCollection = SpyProductManagementAttributeValueQuery::create()
41
            ->findByFkProductManagementAttribute($productManagementAttributeEntity->getIdProductManagementAttribute());
42
43
        foreach ($productManagementAttributeValueEntityCollection as $productManagementAttributeValueEntity) {
44
            foreach ($productManagementAttributeValueEntity->getSpyProductManagementAttributeValueTranslations() as $productManagementAttributeValueTranslation) {
45
                $productManagementAttributeValueTranslation->delete();
46
            }
47
48
            $productManagementAttributeValueEntity->delete();
49
        }
50
51
        $glossaryKey = ProductAttributeConfig::PRODUCT_ATTRIBUTE_GLOSSARY_PREFIX . $dataSet['key'];
52
        $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
53
            ->filterByKey($glossaryKey)
54
            ->findOneOrCreate();
55
56
        $glossaryKeyEntity->save();
57
58
        foreach ($dataSet[ProductManagementLocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $attributes) {
59
            $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
60
                ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
61
                ->filterByFkLocale($idLocale)
62
                ->findOneOrCreate();
63
64
            $glossaryTranslationEntity
65
                ->setValue($attributes['key_translation'])
66
                ->save();
67
68
            $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

68
            $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...
69
70
            if (!empty($attributes['value_translations'])) {
71
                foreach ($attributes['value_translations'] as $value => $translation) {
72
                    $productManagementAttributeValueEntity = SpyProductManagementAttributeValueQuery::create()
73
                        ->filterBySpyProductManagementAttribute($productManagementAttributeEntity)
74
                        ->filterByValue($value)
75
                        ->findOneOrCreate();
76
77
                    $productManagementAttributeValueEntity->save();
78
79
                    $productManagementAttributeValueTranslationEntity = new SpyProductManagementAttributeValueTranslation();
80
                    $productManagementAttributeValueTranslationEntity
81
                        ->setSpyProductManagementAttributeValue($productManagementAttributeValueEntity)
82
                        ->setTranslation($translation)
83
                        ->setFkLocale($idLocale)
84
                        ->save();
85
                }
86
87
                continue;
88
            }
89
90
            foreach ($attributes['values'] as $value) {
91
                $productManagementAttributeValueEntity = SpyProductManagementAttributeValueQuery::create()
92
                    ->filterBySpyProductManagementAttribute($productManagementAttributeEntity)
93
                    ->filterByValue($value)
94
                    ->findOneOrCreate();
95
96
                $productManagementAttributeValueEntity->save();
97
            }
98
        }
99
    }
100
}
101