findOrCreateCmsBlockPlaceholderTranslation()   B
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 42
c 0
b 0
f 0
rs 8.0555
cc 9
nc 10
nop 2
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
                $key = str_replace('placeholder.', '', $key);
150
                $keyName = CmsBlockGlossaryKeyGenerator::GENERATED_GLOSSARY_KEY_PREFIX . '.';
151
                $keyName .= str_replace([' ', '.'], '-', $dataSet[static::KEY_TEMPLATE_NAME]) . '.';
152
                $keyName .= str_replace([' ', '.'], '-', $key);
153
                $keyName .= '.idCmsBlock.' . $cmsBlockEntity->getIdCmsBlock();
154
                $keyName .= '.uniqueId.1';
155
156
                $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
157
                    ->filterByKey($keyName)
158
                    ->findOneOrCreate();
159
160
                if ($glossaryKeyEntity->isNew() || $glossaryKeyEntity->isModified()) {
161
                    $glossaryKeyEntity->save();
162
                }
163
164
                $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
165
                    ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
166
                    ->filterByFkLocale($idLocale)
167
                    ->findOneOrCreate();
168
169
                $glossaryTranslationEntity->setValue($value);
170
171
                if ($glossaryTranslationEntity->isNew() || $glossaryTranslationEntity->isModified()) {
172
                    $glossaryTranslationEntity->save();
173
                }
174
175
                $pageKeyMappingEntity = SpyCmsBlockGlossaryKeyMappingQuery::create()
176
                    ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
177
                    ->filterByFkCmsBlock($cmsBlockEntity->getIdCmsBlock())
178
                    ->findOneOrCreate();
179
180
                $pageKeyMappingEntity->setPlaceholder($key);
181
182
                if ($pageKeyMappingEntity->isNew() || $pageKeyMappingEntity->isModified()) {
183
                    $pageKeyMappingEntity->save();
184
                }
185
186
                $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

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