CmsBlockStoreWriterStep   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 27
c 0
b 0
f 0
dl 0
loc 91
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdCmsBlockByKey() 0 15 3
A execute() 0 11 1
A getIdStoreByName() 0 15 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\CmsBlockStore;
11
12
use Orm\Zed\CmsBlock\Persistence\SpyCmsBlockQuery;
13
use Orm\Zed\CmsBlock\Persistence\SpyCmsBlockStoreQuery;
14
use Orm\Zed\Store\Persistence\SpyStoreQuery;
15
use Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException;
16
use Spryker\Zed\CmsBlock\Dependency\CmsBlockEvents;
17
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
18
use Spryker\Zed\DataImport\Business\Model\DataImportStep\PublishAwareStep;
19
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
20
21
class CmsBlockStoreWriterStep extends PublishAwareStep implements DataImportStepInterface
22
{
23
    /**
24
     * @var int
25
     */
26
    public const BULK_SIZE = 100;
27
28
    /**
29
     * @var string
30
     */
31
    public const KEY_BLOCK_KEY = 'block_key';
32
33
    /**
34
     * @var string
35
     */
36
    public const KEY_STORE_NAME = 'store_name';
37
38
    /**
39
     * @var array<int> Keys are CMS Block names, values are CMS Block IDs.
40
     */
41
    protected static $idCmsBlockBuffer = [];
42
43
    /**
44
     * @var array<int> Keys are store names, values are store ids.
45
     */
46
    protected static $idStoreBuffer = [];
47
48
    /**
49
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
50
     *
51
     * @return void
52
     */
53
    public function execute(DataSetInterface $dataSet): void
54
    {
55
        $idCmsBlock = $this->getIdCmsBlockByKey($dataSet[static::KEY_BLOCK_KEY]);
56
57
        (new SpyCmsBlockStoreQuery())
58
            ->filterByFkCmsBlock($idCmsBlock)
59
            ->filterByFkStore($this->getIdStoreByName($dataSet[static::KEY_STORE_NAME]))
60
            ->findOneOrCreate()
61
            ->save();
62
63
        $this->addPublishEvents(CmsBlockEvents::CMS_BLOCK_PUBLISH, $idCmsBlock);
64
    }
65
66
    /**
67
     * @param string $cmsBlockKey
68
     *
69
     * @throws \Spryker\Zed\DataImport\Business\Exception\EntityNotFoundException
70
     *
71
     * @return int
72
     */
73
    protected function getIdCmsBlockByKey(string $cmsBlockKey): int
74
    {
75
        if (isset(static::$idCmsBlockBuffer[$cmsBlockKey])) {
76
            return static::$idCmsBlockBuffer[$cmsBlockKey];
77
        }
78
79
        $cmsBlockEntity = SpyCmsBlockQuery::create()->findOneByKey($cmsBlockKey);
80
81
        if (!$cmsBlockEntity) {
82
            throw new EntityNotFoundException(sprintf('CmsBlock not found by block key "%s"', $cmsBlockKey));
83
        }
84
85
        static::$idCmsBlockBuffer[$cmsBlockKey] = $cmsBlockEntity->getIdCmsBlock();
86
87
        return static::$idCmsBlockBuffer[$cmsBlockKey];
88
    }
89
90
    /**
91
     * @param string $storeName
92
     *
93
     * @throws \Spryker\Zed\DataImport\Business\Exception\EntityNotFoundException
94
     *
95
     * @return int
96
     */
97
    protected function getIdStoreByName(string $storeName): int
98
    {
99
        if (isset(static::$idStoreBuffer[$storeName])) {
100
            return static::$idStoreBuffer[$storeName];
101
        }
102
103
        $storeEntity = SpyStoreQuery::create()->findOneByName($storeName);
104
105
        if (!$storeEntity) {
106
            throw new EntityNotFoundException(sprintf('Store not found by store name "%s"', $storeName));
107
        }
108
109
        static::$idStoreBuffer[$storeName] = $storeEntity->getIdStore();
110
111
        return static::$idStoreBuffer[$storeName];
112
    }
113
}
114