CmsBlockWriterStep   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 61
c 0
b 0
f 0
dl 0
loc 167
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findOrCreateCmsBlock() 0 15 3
A execute() 0 7 1
B findOrCreateCmsBlockPlaceholderTranslation() 0 42 9
A findOrCreateCmsBlockTemplate() 0 13 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\CmsBlock;
11
12
use Orm\Zed\CmsBlock\Persistence\SpyCmsBlock;
13
use Orm\Zed\CmsBlock\Persistence\SpyCmsBlockGlossaryKeyMappingQuery;
14
use Orm\Zed\CmsBlock\Persistence\SpyCmsBlockQuery;
15
use Orm\Zed\CmsBlock\Persistence\SpyCmsBlockTemplate;
16
use Orm\Zed\CmsBlock\Persistence\SpyCmsBlockTemplateQuery;
17
use Orm\Zed\Glossary\Persistence\SpyGlossaryKeyQuery;
18
use Orm\Zed\Glossary\Persistence\SpyGlossaryTranslationQuery;
19
use Spryker\Zed\CmsBlock\Business\Model\CmsBlockGlossaryKeyGenerator;
20
use Spryker\Zed\CmsBlock\Dependency\CmsBlockEvents;
21
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
22
use Spryker\Zed\DataImport\Business\Model\DataImportStep\LocalizedAttributesExtractorStep;
23
use Spryker\Zed\DataImport\Business\Model\DataImportStep\PublishAwareStep;
24
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
25
use Spryker\Zed\Glossary\Dependency\GlossaryEvents;
26
27
/**
28
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29
 */
30
class CmsBlockWriterStep extends PublishAwareStep implements DataImportStepInterface
31
{
32
    /**
33
     * @var int
34
     */
35
    public const BULK_SIZE = 100;
36
37
    /**
38
     * @var string
39
     */
40
    public const KEY_BLOCK_NAME = 'block_name';
41
42
    /**
43
     * @var string
44
     */
45
    public const KEY_BLOCK_KEY = 'block_key';
46
47
    /**
48
     * @var string
49
     */
50
    public const KEY_TEMPLATE_NAME = 'template_name';
51
52
    /**
53
     * @var string
54
     */
55
    public const KEY_TEMPLATE_PATH = 'template_path';
56
57
    /**
58
     * @var string
59
     */
60
    public const KEY_CATEGORIES = 'categories';
61
62
    /**
63
     * @var string
64
     */
65
    public const KEY_PRODUCTS = 'products';
66
67
    /**
68
     * @var string
69
     */
70
    public const KEY_ACTIVE = 'active';
71
72
    /**
73
     * @var string
74
     */
75
    public const KEY_PLACEHOLDER_TITLE = 'placeholder.title';
76
77
    /**
78
     * @var string
79
     */
80
    public const KEY_PLACEHOLDER_CONTENT = 'placeholder.content';
81
82
    /**
83
     * @var string
84
     */
85
    public const KEY_PLACEHOLDER_LINK = 'placeholder.link';
86
87
    /**
88
     * @var string
89
     */
90
    public const KEY_PLACEHOLDER_IMAGE_URL = 'placeholder.imageUrl';
91
92
    /**
93
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
94
     *
95
     * @return void
96
     */
97
    public function execute(DataSetInterface $dataSet): void
98
    {
99
        $templateEntity = $this->findOrCreateCmsBlockTemplate($dataSet);
100
        $cmsBlockEntity = $this->findOrCreateCmsBlock($dataSet, $templateEntity);
101
102
        $this->findOrCreateCmsBlockPlaceholderTranslation($dataSet, $cmsBlockEntity);
103
        $this->addPublishEvents(CmsBlockEvents::CMS_BLOCK_PUBLISH, $cmsBlockEntity->getIdCmsBlock());
104
    }
105
106
    /**
107
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
108
     *
109
     * @return \Orm\Zed\CmsBlock\Persistence\SpyCmsBlockTemplate
110
     */
111
    protected function findOrCreateCmsBlockTemplate(DataSetInterface $dataSet): SpyCmsBlockTemplate
112
    {
113
        $templateEntity = SpyCmsBlockTemplateQuery::create()
114
            ->filterByTemplateName($dataSet[static::KEY_TEMPLATE_NAME])
115
            ->findOneOrCreate();
116
117
        $templateEntity->setTemplatePath($dataSet[static::KEY_TEMPLATE_PATH]);
118
119
        if ($templateEntity->isNew() || $templateEntity->isModified()) {
120
            $templateEntity->save();
121
        }
122
123
        return $templateEntity;
124
    }
125
126
    /**
127
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
128
     * @param \Orm\Zed\CmsBlock\Persistence\SpyCmsBlockTemplate $templateEntity
129
     *
130
     * @return \Orm\Zed\CmsBlock\Persistence\SpyCmsBlock
131
     */
132
    protected function findOrCreateCmsBlock(DataSetInterface $dataSet, SpyCmsBlockTemplate $templateEntity): SpyCmsBlock
133
    {
134
        $cmsBlockEntity = SpyCmsBlockQuery::create()
135
            ->filterByFkTemplate($templateEntity->getIdCmsBlockTemplate())
136
            ->filterByKey($dataSet[static::KEY_BLOCK_KEY])
137
            ->filterByName($dataSet[static::KEY_BLOCK_NAME])
138
            ->findOneOrCreate();
139
140
        $cmsBlockEntity->setIsActive($dataSet[static::KEY_ACTIVE]);
141
142
        if ($cmsBlockEntity->isNew() || $cmsBlockEntity->isModified()) {
143
            $cmsBlockEntity->save();
144
        }
145
146
        return $cmsBlockEntity;
147
    }
148
149
    /**
150
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
151
     * @param \Orm\Zed\CmsBlock\Persistence\SpyCmsBlock $cmsBlockEntity
152
     *
153
     * @return void
154
     */
155
    protected function findOrCreateCmsBlockPlaceholderTranslation(DataSetInterface $dataSet, SpyCmsBlock $cmsBlockEntity): void
156
    {
157
        foreach ($dataSet[LocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $placeholder) {
158
            foreach ($placeholder as $key => $value) {
159
                $key = str_replace('placeholder.', '', $key);
160
                $keyName = CmsBlockGlossaryKeyGenerator::GENERATED_GLOSSARY_KEY_PREFIX . '.';
161
                $keyName .= str_replace([' ', '.'], '-', $dataSet[static::KEY_TEMPLATE_NAME]) . '.';
162
                $keyName .= str_replace([' ', '.'], '-', $key);
163
                $keyName .= '.idCmsBlock.' . $cmsBlockEntity->getIdCmsBlock();
164
                $keyName .= '.uniqueId.1';
165
166
                $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
167
                    ->filterByKey($keyName)
168
                    ->findOneOrCreate();
169
170
                if ($glossaryKeyEntity->isNew() || $glossaryKeyEntity->isModified()) {
171
                    $glossaryKeyEntity->save();
172
                }
173
174
                $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
175
                    ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
176
                    ->filterByFkLocale($idLocale)
177
                    ->findOneOrCreate();
178
179
                $glossaryTranslationEntity->setValue($value);
180
181
                if ($glossaryTranslationEntity->isNew() || $glossaryTranslationEntity->isModified()) {
182
                    $glossaryTranslationEntity->save();
183
                }
184
185
                $pageKeyMappingEntity = SpyCmsBlockGlossaryKeyMappingQuery::create()
186
                    ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
187
                    ->filterByFkCmsBlock($cmsBlockEntity->getIdCmsBlock())
188
                    ->findOneOrCreate();
189
190
                $pageKeyMappingEntity->setPlaceholder($key);
191
192
                if ($pageKeyMappingEntity->isNew() || $pageKeyMappingEntity->isModified()) {
193
                    $pageKeyMappingEntity->save();
194
                }
195
196
                $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

196
                $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...
197
            }
198
        }
199
    }
200
}
201