NavigationNodeWriterStep::getExternalUrl()   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
    public const BULK_SIZE = 100;
28
29
    public const DEFAULT_IS_ACTIVE = true;
30
31
    public const KEY_NAVIGATION_KEY = 'navigation_key';
32
33
    public const KEY_NODE_KEY = 'node_key';
34
35
    public const KEY_PARENT_NODE_KEY = 'parent_node_key';
36
37
    public const KEY_POSITION = 'position';
38
39
    public const KEY_NODE_TYPE = 'node_type';
40
41
    public const KEY_TITLE = 'title';
42
43
    public const KEY_URL = 'url';
44
45
    public const KEY_IS_ACTIVE = 'is_active';
46
47
    public const KEY_CSS_CLASS = 'css_class';
48
49
    public const KEY_VALID_FROM = 'valid_from';
50
51
    public const KEY_VALID_TO = 'valid_to';
52
53
    public const NODE_TYPE_LINK = 'link';
54
55
    public const NODE_TYPE_EXTERNAL_URL = 'external_url';
56
57
    public const NODE_TYPE_CATEGORY = 'category';
58
59
    public const NODE_TYPE_CMS_PAGE = 'cms_page';
60
61
    public function execute(DataSetInterface $dataSet): void
62
    {
63
        $navigationNodeEntity = SpyNavigationNodeQuery::create()
64
            ->filterByFkNavigation($dataSet[NavigationKeyToIdNavigationStep::KEY_TARGET])
65
            ->filterByNodeKey($dataSet[static::KEY_NODE_KEY])
66
            ->findOneOrCreate();
67
68
        $navigationNodeEntity
69
            ->setPosition($this->getPosition($navigationNodeEntity, $dataSet))
70
            ->setIsActive($this->isActive($navigationNodeEntity, $dataSet))
71
            ->setNodeType($this->getNodeType($navigationNodeEntity, $dataSet));
72
73
        if ($dataSet[static::KEY_VALID_FROM] !== '') {
74
            $navigationNodeEntity->setValidFrom($dataSet[static::KEY_VALID_FROM]);
75
        }
76
77
        if ($dataSet[static::KEY_VALID_TO] !== '') {
78
            $navigationNodeEntity->setValidTo($dataSet[static::KEY_VALID_TO]);
79
        }
80
81
        if (!empty($dataSet[static::KEY_PARENT_NODE_KEY])) {
82
            $navigationNodeEntity->setFkParentNavigationNode(
83
                $this->getFkParentNavigationNode($dataSet[static::KEY_PARENT_NODE_KEY]),
84
            );
85
        }
86
87
        foreach ($dataSet[ProductLocalizedAttributesExtractorStep::KEY_LOCALIZED_ATTRIBUTES] as $idLocale => $localizedAttributes) {
88
            if ($localizedAttributes === []) {
89
                continue;
90
            }
91
            $navigationNodeLocalizedAttributesEntity = SpyNavigationNodeLocalizedAttributesQuery::create()
92
                ->filterByFkNavigationNode($navigationNodeEntity->getIdNavigationNode())
93
                ->filterByFkLocale($idLocale)
94
                ->findOneOrCreate();
95
96
            $navigationNodeLocalizedAttributesEntity->setTitle($this->getTitle($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
97
98
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_LINK) {
99
                $navigationNodeLocalizedAttributesEntity->setLink($this->getLink($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
100
            }
101
102
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_EXTERNAL_URL) {
103
                $navigationNodeLocalizedAttributesEntity->setExternalUrl($this->getExternalUrl($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
104
            }
105
106
            if ($navigationNodeEntity->getNodeType() === static::NODE_TYPE_CATEGORY || $navigationNodeEntity->getNodeType() === static::NODE_TYPE_CMS_PAGE) {
107
                $navigationNodeLocalizedAttributesEntity->setFkUrl($this->getFkUrl($navigationNodeLocalizedAttributesEntity, $localizedAttributes, $idLocale));
108
            }
109
110
            $navigationNodeLocalizedAttributesEntity->setCssClass($this->getCssClass($navigationNodeLocalizedAttributesEntity, $localizedAttributes));
111
112
            $navigationNodeEntity->addSpyNavigationNodeLocalizedAttributes($navigationNodeLocalizedAttributesEntity);
113
        }
114
115
        $navigationNodeEntity->save();
116
117
        $this->addPublishEvents(NavigationEvents::NAVIGATION_KEY_PUBLISH, $navigationNodeEntity->getFkNavigation());
118
    }
119
120
    /**
121
     * @param string $nodeKey
122
     *
123
     * @throws \Pyz\Zed\DataImport\Business\Exception\NavigationNodeByKeyNotFoundException
124
     */
125
    protected function getFkParentNavigationNode(string $nodeKey): int
126
    {
127
        $parentNavigationNodeEntity = SpyNavigationNodeQuery::create()
128
            ->findOneByNodeKey($nodeKey);
129
130
        if (!$parentNavigationNodeEntity) {
131
            throw new NavigationNodeByKeyNotFoundException(sprintf(
132
                'NavigationNode with key "%s" not found',
133
                $nodeKey,
134
            ));
135
        }
136
137
        return $parentNavigationNodeEntity->getIdNavigationNode();
138
    }
139
140
    protected function getPosition(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): ?int
141
    {
142
        if (isset($dataSet[static::KEY_POSITION]) && !empty($dataSet[static::KEY_POSITION])) {
143
            return (int)$dataSet[static::KEY_POSITION];
144
        }
145
146
        return $navigationNodeEntity->getPosition();
147
    }
148
149
    protected function isActive(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): bool
150
    {
151
        if (isset($dataSet[static::KEY_IS_ACTIVE]) && !empty($dataSet[static::KEY_IS_ACTIVE])) {
152
            return (bool)$dataSet[static::KEY_IS_ACTIVE];
153
        }
154
155
        /** @var bool|null $isActive */
156
        $isActive = $navigationNodeEntity->getIsActive();
157
        if ($isActive !== null) {
158
            return $navigationNodeEntity->getIsActive();
159
        }
160
161
        return static::DEFAULT_IS_ACTIVE;
162
    }
163
164
    protected function getNodeType(SpyNavigationNode $navigationNodeEntity, DataSetInterface $dataSet): ?string
165
    {
166
        if (isset($dataSet[static::KEY_NODE_TYPE]) && !empty($dataSet[static::KEY_NODE_TYPE])) {
167
            return $dataSet[static::KEY_NODE_TYPE];
168
        }
169
170
        return $navigationNodeEntity->getNodeType();
171
    }
172
173
    /**
174
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
175
     * @param array<string, mixed> $localizedAttributes
176
     */
177
    protected function getTitle(
178
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
179
        array $localizedAttributes,
180
    ): string {
181
        if (isset($localizedAttributes[static::KEY_TITLE]) && !empty($localizedAttributes[static::KEY_TITLE])) {
182
            return $localizedAttributes[static::KEY_TITLE];
183
        }
184
185
        return $navigationNodeLocalizedAttributes->getTitle();
186
    }
187
188
    /**
189
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
190
     * @param array<string, mixed> $localizedAttributes
191
     */
192
    protected function getLink(
193
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
194
        array $localizedAttributes,
195
    ): ?string {
196
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
197
            return $localizedAttributes[static::KEY_URL];
198
        }
199
200
        return $navigationNodeLocalizedAttributes->getLink();
201
    }
202
203
    /**
204
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
205
     * @param array<string, mixed> $localizedAttributes
206
     */
207
    protected function getExternalUrl(
208
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
209
        array $localizedAttributes,
210
    ): ?string {
211
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
212
            return $localizedAttributes[static::KEY_URL];
213
        }
214
215
        return $navigationNodeLocalizedAttributes->getExternalUrl();
216
    }
217
218
    /**
219
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
220
     * @param array<string, mixed> $localizedAttributes
221
     * @param int $idLocale
222
     */
223
    protected function getFkUrl(
224
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
225
        array $localizedAttributes,
226
        int $idLocale,
227
    ): ?int {
228
        if (isset($localizedAttributes[static::KEY_URL]) && !empty($localizedAttributes[static::KEY_URL])) {
229
            $urlEntity = SpyUrlQuery::create()
230
                ->filterByFkLocale($idLocale)
231
                ->filterByUrl($localizedAttributes[static::KEY_URL])
232
                ->findOne();
233
234
            if ($urlEntity) {
235
                return $urlEntity->getIdUrl();
236
            }
237
        }
238
239
        return $navigationNodeLocalizedAttributes->getFkUrl();
240
    }
241
242
    /**
243
     * @param \Orm\Zed\Navigation\Persistence\SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes
244
     * @param array<string, mixed> $localizedAttributes
245
     */
246
    protected function getCssClass(
247
        SpyNavigationNodeLocalizedAttributes $navigationNodeLocalizedAttributes,
248
        array $localizedAttributes,
249
    ): ?string {
250
        if (isset($localizedAttributes[static::KEY_CSS_CLASS]) && !empty($localizedAttributes[static::KEY_CSS_CLASS])) {
251
            return $localizedAttributes[static::KEY_CSS_CLASS];
252
        }
253
254
        return $navigationNodeLocalizedAttributes->getCssClass();
255
    }
256
}
257