NavigationNodeWriterStep   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 306
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 37
eloc 86
dl 0
loc 306
c 0
b 0
f 0
rs 9.44

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getNodeType() 0 7 3
A getCssClass() 0 9 3
A getExternalUrl() 0 9 3
A getPosition() 0 7 3
B execute() 0 58 10
A getLink() 0 9 3
A getTitle() 0 9 3
A isActive() 0 7 3
A getFkUrl() 0 17 4
A getFkParentNavigationNode() 0 13 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\NavigationNode;
11
12
use Orm\Zed\Navigation\Persistence\SpyNavigationNode;
13
use Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes;
14
use Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributesQuery;
15
use Orm\Zed\Navigation\Persistence\SpyNavigationNodeQuery;
16
use Orm\Zed\Url\Persistence\SpyUrlQuery;
17
use Pyz\Zed\DataImport\Business\Exception\NavigationNodeByKeyNotFoundException;
18
use Pyz\Zed\DataImport\Business\Model\Navigation\NavigationKeyToIdNavigationStep;
19
use Pyz\Zed\DataImport\Business\Model\Product\ProductLocalizedAttributesExtractorStep;
20
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
21
use Spryker\Zed\DataImport\Business\Model\DataImportStep\PublishAwareStep;
22
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
23
use Spryker\Zed\Navigation\Dependency\NavigationEvents;
24
25
class NavigationNodeWriterStep extends PublishAwareStep implements DataImportStepInterface
26
{
27
    /**
28
     * @var int
29
     */
30
    public const BULK_SIZE = 100;
31
32
    /**
33
     * @var string
34
     */
35
    public const KEY_NAVIGATION_KEY = 'navigation_key';
36
37
    /**
38
     * @var string
39
     */
40
    public const KEY_NODE_KEY = 'node_key';
41
42
    /**
43
     * @var string
44
     */
45
    public const KEY_PARENT_NODE_KEY = 'parent_node_key';
46
47
    /**
48
     * @var string
49
     */
50
    public const KEY_POSITION = 'position';
51
52
    /**
53
     * @var string
54
     */
55
    public const KEY_NODE_TYPE = 'node_type';
56
57
    /**
58
     * @var string
59
     */
60
    public const KEY_TITLE = 'title';
61
62
    /**
63
     * @var string
64
     */
65
    public const KEY_URL = 'url';
66
67
    /**
68
     * @var string
69
     */
70
    public const KEY_IS_ACTIVE = 'is_active';
71
72
    /**
73
     * @var string
74
     */
75
    public const KEY_CSS_CLASS = 'css_class';
76
77
    /**
78
     * @var string
79
     */
80
    public const KEY_VALID_FROM = 'valid_from';
81
82
    /**
83
     * @var string
84
     */
85
    public const KEY_VALID_TO = 'valid_to';
86
87
    /**
88
     * @var string
89
     */
90
    public const NODE_TYPE_LINK = 'link';
91
92
    /**
93
     * @var string
94
     */
95
    public const NODE_TYPE_EXTERNAL_URL = 'external_url';
96
97
    /**
98
     * @var string
99
     */
100
    public const NODE_TYPE_CATEGORY = 'category';
101
102
    /**
103
     * @var string
104
     */
105
    public const NODE_TYPE_CMS_PAGE = 'cms_page';
106
107
    /**
108
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
109
     *
110
     * @return void
111
     */
112
    public function execute(DataSetInterface $dataSet): void
113
    {
114
        $navigationNodeEntity = SpyNavigationNodeQuery::create()
115
            ->filterByFkNavigation($dataSet[NavigationKeyToIdNavigationStep::KEY_TARGET])
116
            ->filterByNodeKey($dataSet[static::KEY_NODE_KEY])
117
            ->findOneOrCreate();
118
119
        $navigationNodeEntity
120
            ->setPosition($this->getPosition($navigationNodeEntity, $dataSet))
121
            ->setIsActive($this->isActive($navigationNodeEntity, $dataSet))
122
            ->setNodeType($this->getNodeType($navigationNodeEntity, $dataSet));
123
124
        if ($dataSet[static::KEY_VALID_FROM] !== '') {
125
            $navigationNodeEntity->setValidFrom($dataSet[static::KEY_VALID_FROM]);
126
        }
127
128
        if ($dataSet[static::KEY_VALID_TO] !== '') {
129
            $navigationNodeEntity->setValidTo($dataSet[static::KEY_VALID_TO]);
130
        }
131
132
        if (!empty($dataSet[static::KEY_PARENT_NODE_KEY])) {
133
            $navigationNodeEntity->setFkParentNavigationNode(
134
                $this->getFkParentNavigationNode($dataSet[static::KEY_PARENT_NODE_KEY]),
135
            );
136
        }
137
138
        foreach ($dataSet[ProductLocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) {
139
            if ($localizedAttributes === []) {
140
                continue;
141
            }
142
143
            $navigationNodeLocalizedAttributesEntity = SpyNavigationNodeLocalizedAttributesQuery::create()
144
                ->filterByFkNavigationNode($navigationNodeEntity->getIdNavigationNode())
145
                ->filterByFkLocale($idLocale)
146
                ->findOneOrCreate();
147
148
            $navigationNodeLocalizedAttributesEntity->setTitle($this->getTitle($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
149
150
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_LINK) {
151
                $navigationNodeLocalizedAttributesEntity->setLink($this->getLink($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
152
            }
153
154
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_EXTERNAL_URL) {
155
                $navigationNodeLocalizedAttributesEntity->setExternalUrl($this->getExternalUrl($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
156
            }
157
158
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_CATEGORY || $navigationNodeEntity->getNodeType() === static::NODE_TYPE_CMS_PAGE) {
159
                $navigationNodeLocalizedAttributesEntity->setFkUrl($this->getFkUrl($navigationNodeLocalizedAttributesEntity, $localizedAttributes, $idLocale));
160
            }
161
162
            $navigationNodeLocalizedAttributesEntity->setCssClass($this->getCssClass($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
163
164
            $navigationNodeEntity->addSpyNavigationNodeLocalizedAttributes($navigationNodeLocalizedAttributesEntity);
165
        }
166
167
        $navigationNodeEntity->save();
168
169
        $this->addPublishEvents(NavigationEvents::NAVIGATION_KEY_PUBLISH, $navigationNodeEntity->getFkNavigation());
170
    }
171
172
    /**
173
     * @param string $nodeKey
174
     *
175
     * @throws \Pyz\Zed\DataImport\Business\Exception\NavigationNodeByKeyNotFoundException
176
     *
177
     * @return int
178
     */
179
    protected function getFkParentNavigationNode(string $nodeKey): int
180
    {
181
        $parentNavigationNodeEntity = SpyNavigationNodeQuery::create()
182
            ->findOneByNodeKey($nodeKey);
183
184
        if (!$parentNavigationNodeEntity) {
185
            throw new NavigationNodeByKeyNotFoundException(sprintf(
186
                'NavigationNode with key "%s" not found',
187
                $nodeKey,
188
            ));
189
        }
190
191
        return $parentNavigationNodeEntity->getIdNavigationNode();
192
    }
193
194
    /**
195
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNode $navigationNodeEntity
196
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
197
     *
198
     * @return int|null
199
     */
200
    protected function getPosition(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): ?int
201
    {
202
        if (isset($dataSet[static::KEY_POSITION]) && !empty($dataSet[static::KEY_POSITION])) {
203
            return (int)$dataSet[static::KEY_POSITION];
204
        }
205
206
        return $navigationNodeEntity->getPosition();
207
    }
208
209
    /**
210
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNode $navigationNodeEntity
211
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
212
     *
213
     * @return bool
214
     */
215
    protected function isActive(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): bool
216
    {
217
        if (isset($dataSet[static::KEY_IS_ACTIVE]) && !empty($dataSet[static::KEY_IS_ACTIVE])) {
218
            return (bool)$dataSet[static::KEY_IS_ACTIVE];
219
        }
220
221
        return $navigationNodeEntity->getIsActive();
222
    }
223
224
    /**
225
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNode $navigationNodeEntity
226
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
227
     *
228
     * @return string|null
229
     */
230
    protected function getNodeType(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): ?string
231
    {
232
        if (isset($dataSet[static::KEY_NODE_TYPE]) && !empty($dataSet[static::KEY_NODE_TYPE])) {
233
            return $dataSet[static::KEY_NODE_TYPE];
234
        }
235
236
        return $navigationNodeEntity->getNodeType();
237
    }
238
239
    /**
240
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
241
     * @param array<string, mixed> $localizedAttributes
242
     *
243
     * @return string
244
     */
245
    protected function getTitle(
246
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
247
        array $localizedAttributes,
248
    ): string {
249
        if (isset($localizedAttributes[static::KEY_TITLE]) && !empty($localizedAttributes[static::KEY_TITLE])) {
250
            return $localizedAttributes[static::KEY_TITLE];
251
        }
252
253
        return $navigationNodeLocalizedAttributes->getTitle();
254
    }
255
256
    /**
257
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
258
     * @param array<string, mixed> $localizedAttributes
259
     *
260
     * @return string|null
261
     */
262
    protected function getLink(
263
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
264
        array $localizedAttributes,
265
    ): ?string {
266
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
267
            return $localizedAttributes[static::KEY_URL];
268
        }
269
270
        return $navigationNodeLocalizedAttributes->getLink();
271
    }
272
273
    /**
274
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
275
     * @param array<string, mixed> $localizedAttributes
276
     *
277
     * @return string|null
278
     */
279
    protected function getExternalUrl(
280
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
281
        array $localizedAttributes,
282
    ): ?string {
283
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
284
            return $localizedAttributes[static::KEY_URL];
285
        }
286
287
        return $navigationNodeLocalizedAttributes->getExternalUrl();
288
    }
289
290
    /**
291
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
292
     * @param array<string, mixed> $localizedAttributes
293
     * @param int $idLocale
294
     *
295
     * @return int|null
296
     */
297
    protected function getFkUrl(
298
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
299
        array $localizedAttributes,
300
        int $idLocale,
301
    ): ?int {
302
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
303
            $urlEntity = SpyUrlQuery::create()
304
                ->filterByFkLocale($idLocale)
305
                ->filterByUrl($localizedAttributes[static::KEY_URL])
306
                ->findOne();
307
308
            if ($urlEntity) {
309
                return $urlEntity->getIdUrl();
310
            }
311
        }
312
313
        return $navigationNodeLocalizedAttributes->getFkUrl();
314
    }
315
316
    /**
317
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
318
     * @param array<string, mixed> $localizedAttributes
319
     *
320
     * @return string|null
321
     */
322
    protected function getCssClass(
323
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
324
        array $localizedAttributes,
325
    ): ?string {
326
        if (isset($localizedAttributes[static::KEY_CSS_CLASS]) && !empty($localizedAttributes[static::KEY_CSS_CLASS])) {
327
            return $localizedAttributes[static::KEY_CSS_CLASS];
328
        }
329
330
        return $navigationNodeLocalizedAttributes->getCssClass();
331
    }
332
}
333