CmsBlockWriterStep::findOrCreateCmsBlockTemplate()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
c 0
b 0
f 0
rs 10
cc 3
nc 2
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
    public const BULK_SIZE = 100;
33
34
    public const KEY_BLOCK_NAME = 'block_name';
35
36
    public const KEY_BLOCK_KEY = 'block_key';
37
38
    public const KEY_TEMPLATE_NAME = 'template_name';
39
40
    public const KEY_TEMPLATE_PATH = 'template_path';
41
42
    public const KEY_CATEGORIES = 'categories';
43
44
    public const KEY_PRODUCTS = 'products';
45
46
    public const KEY_ACTIVE = 'active';
47
48
    public const KEY_PLACEHOLDER_TITLE = 'placeholder.title';
49
50
    public const KEY_PLACEHOLDER_CONTENT = 'placeholder.content';
51
52
    public const KEY_PLACEHOLDER_LINK = 'placeholder.link';
53
54
    public const KEY_PLACEHOLDER_IMAGE_URL = 'placeholder.imageUrl';
55
56
    public function execute(DataSetInterface $dataSet): void
57
    {
58
        $templateEntity = $this->findOrCreateCmsBlockTemplate($dataSet);
59
        $cmsBlockEntity = $this->findOrCreateCmsBlock($dataSet, $templateEntity);
60
61
        $this->findOrCreateCmsBlockPlaceholderTranslation($dataSet, $cmsBlockEntity);
62
        $this->addPublishEvents(CmsBlockEvents::CMS_BLOCK_PUBLISH, $cmsBlockEntity->getIdCmsBlock());
63
    }
64
65
    protected function findOrCreateCmsBlockTemplate(DataSetInterface $dataSet): SpyCmsBlockTemplate
66
    {
67
        $templateEntity = SpyCmsBlockTemplateQuery::create()
68
            ->filterByTemplateName($dataSet[static::KEY_TEMPLATE_NAME])
69
            ->findOneOrCreate();
70
71
        $templateEntity->setTemplatePath($dataSet[static::KEY_TEMPLATE_PATH]);
72
73
        if ($templateEntity->isNew() || $templateEntity->isModified()) {
74
            $templateEntity->save();
75
        }
76
77
        return $templateEntity;
78
    }
79
80
    protected function findOrCreateCmsBlock(DataSetInterface $dataSet, SpyCmsBlockTemplate $templateEntity): SpyCmsBlock
81
    {
82
        $cmsBlockEntity = SpyCmsBlockQuery::create()
83
            ->filterByFkTemplate($templateEntity->getIdCmsBlockTemplate())
84
            ->filterByKey($dataSet[static::KEY_BLOCK_KEY])
85
            ->filterByName($dataSet[static::KEY_BLOCK_NAME])
86
            ->findOneOrCreate();
87
88
        $cmsBlockEntity->setIsActive($dataSet[static::KEY_ACTIVE]);
89
90
        if ($cmsBlockEntity->isNew() || $cmsBlockEntity->isModified()) {
91
            $cmsBlockEntity->save();
92
        }
93
94
        return $cmsBlockEntity;
95
    }
96
97
    protected function findOrCreateCmsBlockPlaceholderTranslation(DataSetInterface $dataSet, SpyCmsBlock $cmsBlockEntity): void
98
    {
99
        foreach ($dataSet[LocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $placeholder) {
100
            foreach ($placeholder as $key => $value) {
101
                $key = str_replace('placeholder.', '', $key);
102
                $keyName = CmsBlockGlossaryKeyGenerator::GENERATED_GLOSSARY_KEY_PREFIX . '.';
103
                $keyName .= str_replace([' ', '.'], '-', $dataSet[static::KEY_TEMPLATE_NAME]) . '.';
104
                $keyName .= str_replace([' ', '.'], '-', $key);
105
                $keyName .= '.idCmsBlock.' . $cmsBlockEntity->getIdCmsBlock();
106
                $keyName .= '.uniqueId.1';
107
108
                $glossaryKeyEntity = SpyGlossaryKeyQuery::create()
109
                    ->filterByKey($keyName)
110
                    ->findOneOrCreate();
111
112
                if ($glossaryKeyEntity->isNew() || $glossaryKeyEntity->isModified()) {
113
                    $glossaryKeyEntity->save();
114
                }
115
116
                $glossaryTranslationEntity = SpyGlossaryTranslationQuery::create()
117
                    ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
118
                    ->filterByFkLocale($idLocale)
119
                    ->findOneOrCreate();
120
121
                $glossaryTranslationEntity->setValue($value);
122
123
                if ($glossaryTranslationEntity->isNew() || $glossaryTranslationEntity->isModified()) {
124
                    $glossaryTranslationEntity->save();
125
                }
126
127
                $pageKeyMappingEntity = SpyCmsBlockGlossaryKeyMappingQuery::create()
128
                    ->filterByFkGlossaryKey($glossaryKeyEntity->getIdGlossaryKey())
129
                    ->filterByFkCmsBlock($cmsBlockEntity->getIdCmsBlock())
130
                    ->findOneOrCreate();
131
132
                $pageKeyMappingEntity->setPlaceholder($key);
133
134
                if ($pageKeyMappingEntity->isNew() || $pageKeyMappingEntity->isModified()) {
135
                    $pageKeyMappingEntity->save();
136
                }
137
138
                $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

138
                $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...
139
            }
140
        }
141
    }
142
}
143