NavigationNodeWriterStep::getLink()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 2
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\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
148
            $navigationNodeLocalizedAttributesEntity = SpyNavigationNodeLocalizedAttributesQuery::create()
149
                ->filterByFkNavigationNode($navigationNodeEntity->getIdNavigationNode())
150
                ->filterByFkLocale($idLocale)
151
                ->findOneOrCreate();
152
153
            $navigationNodeLocalizedAttributesEntity->setTitle($this->getTitle($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
154
155
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_LINK) {
156
                $navigationNodeLocalizedAttributesEntity->setLink($this->getLink($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
157
            }
158
159
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_EXTERNAL_URL) {
160
                $navigationNodeLocalizedAttributesEntity->setExternalUrl($this->getExternalUrl($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
161
            }
162
163
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_CATEGORY || $navigationNodeEntity->getNodeType() === static::NODE_TYPE_CMS_PAGE) {
164
                $navigationNodeLocalizedAttributesEntity->setFkUrl($this->getFkUrl($navigationNodeLocalizedAttributesEntity, $localizedAttributes, $idLocale));
165
            }
166
167
            $navigationNodeLocalizedAttributesEntity->setCssClass($this->getCssClass($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
168
169
            $navigationNodeEntity->addSpyNavigationNodeLocalizedAttributes($navigationNodeLocalizedAttributesEntity);
170
        }
171
172
        $navigationNodeEntity->save();
173
174
        $this->addPublishEvents(NavigationEvents::NAVIGATION_KEY_PUBLISH, $navigationNodeEntity->getFkNavigation());
175
    }
176
177
    /**
178
     * @param string $nodeKey
179
     *
180
     * @throws \Pyz\Zed\DataImport\Business\Exception\NavigationNodeByKeyNotFoundException
181
     *
182
     * @return int
183
     */
184
    protected function getFkParentNavigationNode(string $nodeKey): int
185
    {
186
        $parentNavigationNodeEntity = SpyNavigationNodeQuery::create()
187
            ->findOneByNodeKey($nodeKey);
188
189
        if (!$parentNavigationNodeEntity) {
190
            throw new NavigationNodeByKeyNotFoundException(sprintf(
191
                'NavigationNode with key "%s" not found',
192
                $nodeKey,
193
            ));
194
        }
195
196
        return $parentNavigationNodeEntity->getIdNavigationNode();
197
    }
198
199
    /**
200
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNode $navigationNodeEntity
201
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
202
     *
203
     * @return int|null
204
     */
205
    protected function getPosition(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): ?int
206
    {
207
        if (isset($dataSet[static::KEY_POSITION]) && !empty($dataSet[static::KEY_POSITION])) {
208
            return (int)$dataSet[static::KEY_POSITION];
209
        }
210
211
        return $navigationNodeEntity->getPosition();
212
    }
213
214
    /**
215
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNode $navigationNodeEntity
216
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
217
     *
218
     * @return bool
219
     */
220
    protected function isActive(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): bool
221
    {
222
        if (isset($dataSet[static::KEY_IS_ACTIVE]) && !empty($dataSet[static::KEY_IS_ACTIVE])) {
223
            return (bool)$dataSet[static::KEY_IS_ACTIVE];
224
        }
225
226
        return $navigationNodeEntity->getIsActive();
227
    }
228
229
    /**
230
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNode $navigationNodeEntity
231
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
232
     *
233
     * @return string|null
234
     */
235
    protected function getNodeType(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): ?string
236
    {
237
        if (isset($dataSet[static::KEY_NODE_TYPE]) && !empty($dataSet[static::KEY_NODE_TYPE])) {
238
            return $dataSet[static::KEY_NODE_TYPE];
239
        }
240
241
        return $navigationNodeEntity->getNodeType();
242
    }
243
244
    /**
245
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
246
     * @param array<string, mixed> $localizedAttributes
247
     *
248
     * @return string
249
     */
250
    protected function getTitle(
251
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
252
        array $localizedAttributes,
253
    ): string {
254
        if (isset($localizedAttributes[static::KEY_TITLE]) && !empty($localizedAttributes[static::KEY_TITLE])) {
255
            return $localizedAttributes[static::KEY_TITLE];
256
        }
257
258
        return $navigationNodeLocalizedAttributes->getTitle();
259
    }
260
261
    /**
262
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
263
     * @param array<string, mixed> $localizedAttributes
264
     *
265
     * @return string|null
266
     */
267
    protected function getLink(
268
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
269
        array $localizedAttributes,
270
    ): ?string {
271
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
272
            return $localizedAttributes[static::KEY_URL];
273
        }
274
275
        return $navigationNodeLocalizedAttributes->getLink();
276
    }
277
278
    /**
279
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
280
     * @param array<string, mixed> $localizedAttributes
281
     *
282
     * @return string|null
283
     */
284
    protected function getExternalUrl(
285
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
286
        array $localizedAttributes,
287
    ): ?string {
288
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
289
            return $localizedAttributes[static::KEY_URL];
290
        }
291
292
        return $navigationNodeLocalizedAttributes->getExternalUrl();
293
    }
294
295
    /**
296
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
297
     * @param array<string, mixed> $localizedAttributes
298
     * @param int $idLocale
299
     *
300
     * @return int|null
301
     */
302
    protected function getFkUrl(
303
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
304
        array $localizedAttributes,
305
        int $idLocale,
306
    ): ?int {
307
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
308
            $urlEntity = SpyUrlQuery::create()
309
                ->filterByFkLocale($idLocale)
310
                ->filterByUrl($localizedAttributes[static::KEY_URL])
311
                ->findOne();
312
313
            if ($urlEntity) {
314
                return $urlEntity->getIdUrl();
315
            }
316
        }
317
318
        return $navigationNodeLocalizedAttributes->getFkUrl();
319
    }
320
321
    /**
322
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
323
     * @param array<string, mixed> $localizedAttributes
324
     *
325
     * @return string|null
326
     */
327
    protected function getCssClass(
328
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
329
        array $localizedAttributes,
330
    ): ?string {
331
        if (isset($localizedAttributes[static::KEY_CSS_CLASS]) && !empty($localizedAttributes[static::KEY_CSS_CLASS])) {
332
            return $localizedAttributes[static::KEY_CSS_CLASS];
333
        }
334
335
        return $navigationNodeLocalizedAttributes->getCssClass();
336
    }
337
}
338