CmsBlockWriterStep::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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_ACTIVE = 'active';
61
62
    /**
63
     * @var string
64
     */
65
    public const KEY_PLACEHOLDER_TITLE = 'placeholder.title';
66
67
    /**
68
     * @var string
69
     */
70
    public const KEY_PLACEHOLDER_DESCRIPTION = 'placeholder.description';
71
72
    /**
73
     * @var string
74
     */
75
    public const KEY_PLACEHOLDER_CONTENT = 'placeholder.content';
76
77
    /**
78
     * @var string
79
     */
80
    public const KEY_PLACEHOLDER_LINK = 'placeholder.link';
81
82
    /**
83
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
84
     *
85
     * @return void
86
     */
87
    public function execute(DataSetInterface $dataSet): void
88
    {
89
        $templateEntity = $this->findOrCreateCmsBlockTemplate($dataSet);
90
        $cmsBlockEntity = $this->findOrCreateCmsBlock($dataSet, $templateEntity);
91
92
        $this->findOrCreateCmsBlockPlaceholderTranslation($dataSet, $cmsBlockEntity);
93
        $this->addPublishEvents(CmsBlockEvents::CMS_BLOCK_PUBLISH, $cmsBlockEntity->getIdCmsBlock());
94
    }
95
96
    /**
97
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
98
     *
99
     * @return \Orm\Zed\CmsBlock\Persistence\SpyCmsBlockTemplate
100
     */
101
    protected function findOrCreateCmsBlockTemplate(DataSetInterface $dataSet): SpyCmsBlockTemplate
102
    {
103
        $templateEntity = SpyCmsBlockTemplateQuery::create()
104
            ->filterByTemplateName($dataSet[static::KEY_TEMPLATE_NAME])
105
            ->findOneOrCreate();
106
107
        $templateEntity->setTemplatePath($dataSet[static::KEY_TEMPLATE_PATH]);
108
109
        if ($templateEntity->isNew() || $templateEntity->isModified()) {
110
            $templateEntity->save();
111
        }
112
113
        return $templateEntity;
114
    }
115
116
    /**
117
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
118
     * @param \Orm\Zed\CmsBlock\Persistence\SpyCmsBlockTemplate $templateEntity
119
     *
120
     * @return \Orm\Zed\CmsBlock\Persistence\SpyCmsBlock
121
     */
122
    protected function findOrCreateCmsBlock(DataSetInterface $dataSet, SpyCmsBlockTemplate $templateEntity): SpyCmsBlock
123
    {
124
        $cmsBlockEntity = SpyCmsBlockQuery::create()
125
            ->filterByFkTemplate($templateEntity->getIdCmsBlockTemplate())
126
            ->filterByKey($dataSet[static::KEY_BLOCK_KEY])
127
            ->filterByName($dataSet[static::KEY_BLOCK_NAME])
128
            ->findOneOrCreate();
129
130
        $cmsBlockEntity->setIsActive($dataSet[static::KEY_ACTIVE]);
131
132
        if ($cmsBlockEntity->isNew() || $cmsBlockEntity->isModified()) {
133
            $cmsBlockEntity->save();
134
        }
135
136
        return $cmsBlockEntity;
137
    }
138
139
    /**
140
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
141
     * @param \Orm\Zed\CmsBlock\Persistence\SpyCmsBlock $cmsBlockEntity
142
     *
143
     * @return void
144
     */
145
    protected function findOrCreateCmsBlockPlaceholderTranslation(DataSetInterface $dataSet, SpyCmsBlock $cmsBlockEntity): void
146
    {
147
        foreach ($dataSet[LocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $placeholder) {
148
            foreach ($placeholder as $key => $value) {
149
                /** @var string $key */
150
                $key = str_replace('placeholder.', '', $key);
151
                $keyName = CmsBlockGlossaryKeyGenerator::GENERATED_GLOSSARY_KEY_PREFIX . '.';
152
                $keyName .= str_replace([' ', '.'], '-', $dataSet[static::KEY_TEMPLATE_NAME]) . '.';
153
                $keyName .= str_replace([' ', '.'], '-', $key);
154
                $keyName .= '.idCmsBlock.' . $cmsBlockEntity->getIdCmsBlock();
155
                $keyName .= '.uniqueId.1';
156
157
                $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
158
                    ->filterByKey($keyName)
159
                    ->findOneOrCreate();
160
161
                if ($glossaryKeyEntity->isNew() || $glossaryKeyEntity->isModified()) {
162
                    $glossaryKeyEntity->save();
163
                }
164
165
                $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
166
                    ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
167
                    ->filterByFkLocale($idLocale)
168
                    ->findOneOrCreate();
169
170
                $glossaryTranslationEntity->setValue($value);
171
172
                if ($glossaryTranslationEntity->isNew() || $glossaryTranslationEntity->isModified()) {
173
                    $glossaryTranslationEntity->save();
174
                }
175
176
                $pageKeyMappingEntity = SpyCmsBlockGlossaryKeyMappingQuery::create()
177
                    ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
178
                    ->filterByFkCmsBlock($cmsBlockEntity->getIdCmsBlock())
179
                    ->findOneOrCreate();
180
181
                $pageKeyMappingEntity->setPlaceholder($key);
182
183
                if ($pageKeyMappingEntity->isNew() || $pageKeyMappingEntity->isModified()) {
184
                    $pageKeyMappingEntity->save();
185
                }
186
187
                $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

187
                $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...
188
            }
189
        }
190
    }
191
}
192