1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Client\CategoryStorage\Storage; |
||
9 | |||
10 | use ArrayObject; |
||
11 | use Generated\Shared\Transfer\CategoryTreeStorageTransfer; |
||
12 | use Generated\Shared\Transfer\SynchronizationDataTransfer; |
||
13 | use Spryker\Client\CategoryStorage\CategoryStorageConfig; |
||
14 | use Spryker\Client\CategoryStorage\Dependency\Client\CategoryStorageToStorageInterface; |
||
15 | use Spryker\Client\CategoryStorage\Dependency\Service\CategoryStorageToSynchronizationServiceInterface; |
||
16 | use Spryker\Client\Kernel\Locator; |
||
17 | use Spryker\Service\Synchronization\Dependency\Plugin\SynchronizationKeyGeneratorPluginInterface; |
||
18 | use Spryker\Shared\CategoryStorage\CategoryStorageConstants; |
||
19 | |||
20 | class CategoryTreeStorageReader implements CategoryTreeStorageReaderInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var \Spryker\Client\CategoryStorage\Dependency\Client\CategoryStorageToStorageInterface |
||
24 | */ |
||
25 | protected $storageClient; |
||
26 | |||
27 | /** |
||
28 | * @var \Spryker\Client\CategoryStorage\Dependency\Service\CategoryStorageToSynchronizationServiceInterface |
||
29 | */ |
||
30 | protected $synchronizationService; |
||
31 | |||
32 | /** |
||
33 | * @var \Spryker\Service\Synchronization\Dependency\Plugin\SynchronizationKeyGeneratorPluginInterface|null |
||
34 | */ |
||
35 | protected static $storageKeyBuilder; |
||
36 | |||
37 | /** |
||
38 | * @param \Spryker\Client\CategoryStorage\Dependency\Client\CategoryStorageToStorageInterface $storageClient |
||
39 | * @param \Spryker\Client\CategoryStorage\Dependency\Service\CategoryStorageToSynchronizationServiceInterface $synchronizationService |
||
40 | */ |
||
41 | public function __construct( |
||
42 | CategoryStorageToStorageInterface $storageClient, |
||
43 | CategoryStorageToSynchronizationServiceInterface $synchronizationService |
||
44 | ) { |
||
45 | $this->storageClient = $storageClient; |
||
46 | $this->synchronizationService = $synchronizationService; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param string $localeName |
||
51 | * @param string $storeName |
||
52 | * |
||
53 | * @return \ArrayObject<int, \Generated\Shared\Transfer\CategoryNodeStorageTransfer> |
||
54 | */ |
||
55 | public function getCategories(string $localeName, string $storeName): ArrayObject |
||
56 | { |
||
57 | $categories = $this->getStorageData($localeName, $storeName); |
||
58 | if (!$categories) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
59 | return new ArrayObject(); |
||
60 | } |
||
61 | |||
62 | $categoryTreeStorageTransfer = (new CategoryTreeStorageTransfer())->fromArray($categories, true); |
||
63 | |||
64 | return $categoryTreeStorageTransfer->getCategoryNodesStorage(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param string $localeName |
||
69 | * @param string $storeName |
||
70 | * |
||
71 | * @return array|null |
||
72 | */ |
||
73 | protected function getStorageData(string $localeName, string $storeName): ?array |
||
74 | { |
||
75 | if (CategoryStorageConfig::isCollectorCompatibilityMode()) { |
||
76 | $clientLocatorClass = Locator::class; |
||
77 | /** @var \Generated\Zed\Ide\AutoCompletion&\Spryker\Shared\Kernel\LocatorLocatorInterface $locator */ |
||
78 | $locator = $clientLocatorClass::getInstance(); |
||
79 | $categoryExporterClient = $locator->categoryExporter()->client(); |
||
80 | |||
81 | $collectorData = $categoryExporterClient->getNavigationCategories($localeName); |
||
82 | $collectorCategories = [ |
||
83 | 'category_nodes_storage' => $this->filterCollectorDataRecursive($collectorData), |
||
84 | ]; |
||
85 | |||
86 | return $collectorCategories; |
||
87 | } |
||
88 | |||
89 | $categoryTreeKey = $this->generateKey($localeName, $storeName); |
||
90 | |||
91 | return $this->storageClient->get($categoryTreeKey); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param array $categories |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | protected function filterCollectorDataRecursive(array $categories): array |
||
100 | { |
||
101 | $filteredCategories = []; |
||
102 | foreach ($categories as $category) { |
||
103 | if (empty($category['parents'])) { |
||
104 | unset($category['parents']); |
||
105 | } |
||
106 | if (isset($category['children'])) { |
||
107 | $category['children'] = $this->filterCollectorDataRecursive($category['children']); |
||
108 | } |
||
109 | $filteredCategories[] = $category; |
||
110 | } |
||
111 | |||
112 | return $filteredCategories; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param string $localeName |
||
117 | * @param string $storeName |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | protected function generateKey(string $localeName, string $storeName): string |
||
122 | { |
||
123 | $synchronizationDataTransfer = (new SynchronizationDataTransfer()) |
||
124 | ->setStore($storeName) |
||
125 | ->setLocale($localeName); |
||
126 | |||
127 | return $this->getStorageKeyBuilder()->generateKey($synchronizationDataTransfer); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return \Spryker\Service\Synchronization\Dependency\Plugin\SynchronizationKeyGeneratorPluginInterface |
||
132 | */ |
||
133 | protected function getStorageKeyBuilder(): SynchronizationKeyGeneratorPluginInterface |
||
134 | { |
||
135 | if (static::$storageKeyBuilder === null) { |
||
136 | static::$storageKeyBuilder = $this->synchronizationService->getStorageKeyBuilder(CategoryStorageConstants::CATEGORY_TREE_RESOURCE_NAME); |
||
137 | } |
||
138 | |||
139 | return static::$storageKeyBuilder; |
||
140 | } |
||
141 | } |
||
142 |