getFkParentNavigationNode()   A
last analyzed

Complexity

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