ProductOptionWriterStep   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 57
dl 0
loc 96
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findOrCreateTranslation() 0 18 1
A execute() 0 46 5
A isActive() 0 7 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\ProductOption;
11
12
use Orm\Zed\Glossary\Persistence\SpyGlossaryKeyQuery;
13
use Orm\Zed\Glossary\Persistence\SpyGlossaryTranslationQuery;
14
use Orm\Zed\Product\Persistence\Map\SpyProductAbstractTableMap;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Product\Persiste...ProductAbstractTableMap 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\Product\Persistence\SpyProductAbstractQuery;
16
use Orm\Zed\ProductOption\Persistence\Base\SpyProductOptionGroup;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\ProductOption\Pe...e\SpyProductOptionGroup 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...
17
use Orm\Zed\ProductOption\Persistence\SpyProductAbstractProductOptionGroupQuery;
18
use Orm\Zed\ProductOption\Persistence\SpyProductOptionGroupQuery;
19
use Orm\Zed\ProductOption\Persistence\SpyProductOptionValueQuery;
20
use Propel\Runtime\ActiveQuery\Criteria;
21
use Pyz\Zed\DataImport\Business\Model\Product\ProductLocalizedAttributesExtractorStep;
22
use Pyz\Zed\DataImport\Business\Model\Tax\TaxSetNameToIdTaxSetStep;
23
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
24
use Spryker\Zed\DataImport\Business\Model\DataImportStep\PublishAwareStep;
25
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
26
use Spryker\Zed\Glossary\Dependency\GlossaryEvents;
27
use Spryker\Zed\ProductOption\Dependency\ProductOptionEvents;
28
29
/**
30
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31
 */
32
class ProductOptionWriterStep extends PublishAwareStep implements DataImportStepInterface
33
{
34
    public const BULK_SIZE = 100;
35
36
    public const KEY_ABSTRACT_PRODUCT_SKUS = 'abstract_product_skus';
37
38
    public const KEY_GROUP_NAME_TRANSLATION_KEY = 'group_name_translation_key';
39
40
    public const KEY_IS_ACTIVE = 'is_active';
41
42
    public const KEY_SKU = 'sku';
43
44
    public const KEY_OPTION_NAME_TRANSLATION_KEY = 'option_name_translation_key';
45
46
    public const KEY_OPTION_NAME = 'option_name';
47
48
    public const KEY_GROUP_NAME = 'group_name';
49
50
    public const KEY_TAX_SET_NAME = 'tax_set_name';
51
52
    public function execute(DataSetInterface $dataSet): void
53
    {
54
        $productOptionGroupEntity = SpyProductOptionGroupQuery::create()
55
            ->filterByName($dataSet[self::KEY_GROUP_NAME_TRANSLATION_KEY])
56
            ->findOneOrCreate();
57
58
        $productOptionGroupEntity
59
            ->setActive($this->isActive($dataSet, $productOptionGroupEntity))
60
            ->setFkTaxSet($dataSet[TaxSetNameToIdTaxSetStep::KEY_TARGET])
61
            ->save();
62
63
        $productOptionValueEntity = SpyProductOptionValueQuery::create()
64
            ->filterBySku($dataSet[self::KEY_SKU])
65
            ->filterByFkProductOptionGroup($productOptionGroupEntity->getIdProductOptionGroup())
66
            ->findOneOrCreate();
67
68
        $productOptionValueEntity
69
            ->setValue($dataSet[self::KEY_OPTION_NAME_TRANSLATION_KEY])
70
            ->save();
71
72
        if (!empty($dataSet[static::KEY_ABSTRACT_PRODUCT_SKUS])) {
73
            $abstractProductSkuCollection = explode(',', $dataSet[static::KEY_ABSTRACT_PRODUCT_SKUS]);
74
75
            /** @var array<int> $abstractProductIdCollection */
76
            $abstractProductIdCollection = SpyProductAbstractQuery::create()
77
                ->select([SpyProductAbstractTableMap::COL_ID_PRODUCT_ABSTRACT])
78
                ->filterBySku($abstractProductSkuCollection, Criteria::IN)
79
                ->find();
80
81
            foreach ($abstractProductIdCollection as $idProductAbstract) {
82
                SpyProductAbstractProductOptionGroupQuery::create()
83
                    ->filterByFkProductOptionGroup($productOptionGroupEntity->getIdProductOptionGroup())
84
                    ->filterByFkProductAbstract($idProductAbstract)
85
                    ->findOneOrCreate()
86
                    ->save();
87
88
                $this->addPublishEvents(ProductOptionEvents::PRODUCT_ABSTRACT_PRODUCT_OPTION_PUBLISH, $idProductAbstract);
89
            }
90
        }
91
92
        foreach ($dataSet[ProductLocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $attributes) {
93
            if (!isset($attributes[static::KEY_OPTION_NAME])) {
94
                continue;
95
            }
96
            $this->findOrCreateTranslation($dataSet[static::KEY_OPTION_NAME_TRANSLATION_KEY], $attributes[static::KEY_OPTION_NAME], $idLocale);
97
            $this->findOrCreateTranslation($dataSet[static::KEY_GROUP_NAME_TRANSLATION_KEY], $attributes[static::KEY_GROUP_NAME], $idLocale);
98
        }
99
    }
100
101
    protected function isActive(DataSetInterface $dataSet, SpyProductOptionGroup $productOptionGroupEntity): bool
102
    {
103
        if (isset($dataSet[self::KEY_IS_ACTIVE])) {
104
            return isset($dataSet[self::KEY_IS_ACTIVE]);
105
        }
106
107
        return ($productOptionGroupEntity->getActive() !== null) ? $productOptionGroupEntity->getActive() : true;
108
    }
109
110
    protected function findOrCreateTranslation(string $key, string $translation, int $idLocale): void
111
    {
112
        $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
113
            ->filterByKey($key)
114
            ->findOneOrCreate();
115
116
        $glossaryKeyEntity->save();
117
118
        $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
119
            ->filterByFkLocale($idLocale)
120
            ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
121
            ->findOneOrCreate();
122
123
        $glossaryTranslationEntity
124
            ->setValue($translation)
125
            ->save();
126
127
        $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

127
        $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...
128
    }
129
}
130