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\ShipmentTypeStorage\Reader; |
||
9 | |||
10 | use Generated\Shared\Transfer\ShipmentTypeStorageCollectionTransfer; |
||
11 | use Generated\Shared\Transfer\ShipmentTypeStorageConditionsTransfer; |
||
12 | use Generated\Shared\Transfer\ShipmentTypeStorageCriteriaTransfer; |
||
13 | use Generated\Shared\Transfer\ShipmentTypeStorageTransfer; |
||
14 | use Spryker\Client\ShipmentTypeStorage\Dependency\Client\ShipmentTypeStorageToStorageClientInterface; |
||
15 | use Spryker\Client\ShipmentTypeStorage\Dependency\Service\ShipmentTypeStorageToUtilEncodingServiceInterface; |
||
16 | use Spryker\Client\ShipmentTypeStorage\Generator\ShipmentTypeStorageKeyGeneratorInterface; |
||
17 | use Spryker\Client\ShipmentTypeStorage\Scanner\ShipmentTypeStorageKeyScannerInterface; |
||
18 | |||
19 | class ShipmentTypeStorageReader implements ShipmentTypeStorageReaderInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected const KEY_ID = 'id'; |
||
25 | |||
26 | /** |
||
27 | * @var \Spryker\Client\ShipmentTypeStorage\Generator\ShipmentTypeStorageKeyGeneratorInterface |
||
28 | */ |
||
29 | protected ShipmentTypeStorageKeyGeneratorInterface $shipmentTypeStorageKeyGenerator; |
||
30 | |||
31 | /** |
||
32 | * @var \Spryker\Client\ShipmentTypeStorage\Dependency\Client\ShipmentTypeStorageToStorageClientInterface |
||
33 | */ |
||
34 | protected ShipmentTypeStorageToStorageClientInterface $storageClient; |
||
35 | |||
36 | /** |
||
37 | * @var \Spryker\Client\ShipmentTypeStorage\Dependency\Service\ShipmentTypeStorageToUtilEncodingServiceInterface |
||
38 | */ |
||
39 | protected ShipmentTypeStorageToUtilEncodingServiceInterface $utilEncodingService; |
||
40 | |||
41 | /** |
||
42 | * @var \Spryker\Client\ShipmentTypeStorage\Scanner\ShipmentTypeStorageKeyScannerInterface |
||
43 | */ |
||
44 | protected ShipmentTypeStorageKeyScannerInterface $shipmentTypeStorageKeyScanner; |
||
45 | |||
46 | /** |
||
47 | * @var list<\Spryker\Client\ShipmentTypeStorageExtension\Dependency\Plugin\ShipmentTypeStorageExpanderPluginInterface> |
||
48 | */ |
||
49 | protected array $shipmentTypeStorageExpanderPlugins; |
||
50 | |||
51 | /** |
||
52 | * @param \Spryker\Client\ShipmentTypeStorage\Generator\ShipmentTypeStorageKeyGeneratorInterface $shipmentTypeStorageKeyGenerator |
||
53 | * @param \Spryker\Client\ShipmentTypeStorage\Dependency\Client\ShipmentTypeStorageToStorageClientInterface $storageClient |
||
54 | * @param \Spryker\Client\ShipmentTypeStorage\Dependency\Service\ShipmentTypeStorageToUtilEncodingServiceInterface $utilEncodingService |
||
55 | * @param \Spryker\Client\ShipmentTypeStorage\Scanner\ShipmentTypeStorageKeyScannerInterface $shipmentTypeStorageKeyScanner |
||
56 | * @param list<\Spryker\Client\ShipmentTypeStorageExtension\Dependency\Plugin\ShipmentTypeStorageExpanderPluginInterface> $shipmentTypeStorageExpanderPlugins |
||
57 | */ |
||
58 | public function __construct( |
||
59 | ShipmentTypeStorageKeyGeneratorInterface $shipmentTypeStorageKeyGenerator, |
||
60 | ShipmentTypeStorageToStorageClientInterface $storageClient, |
||
61 | ShipmentTypeStorageToUtilEncodingServiceInterface $utilEncodingService, |
||
62 | ShipmentTypeStorageKeyScannerInterface $shipmentTypeStorageKeyScanner, |
||
63 | array $shipmentTypeStorageExpanderPlugins |
||
64 | ) { |
||
65 | $this->shipmentTypeStorageKeyGenerator = $shipmentTypeStorageKeyGenerator; |
||
66 | $this->storageClient = $storageClient; |
||
67 | $this->utilEncodingService = $utilEncodingService; |
||
68 | $this->shipmentTypeStorageKeyScanner = $shipmentTypeStorageKeyScanner; |
||
69 | $this->shipmentTypeStorageExpanderPlugins = $shipmentTypeStorageExpanderPlugins; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param \Generated\Shared\Transfer\ShipmentTypeStorageCriteriaTransfer $shipmentTypeStorageCriteriaTransfer |
||
74 | * |
||
75 | * @return \Generated\Shared\Transfer\ShipmentTypeStorageCollectionTransfer |
||
76 | */ |
||
77 | public function getShipmentTypeStorageCollection( |
||
78 | ShipmentTypeStorageCriteriaTransfer $shipmentTypeStorageCriteriaTransfer |
||
79 | ): ShipmentTypeStorageCollectionTransfer { |
||
80 | $shipmentTypeStorageCollectionTransfer = $this->getShipmentTypeStorageCollectionByCriteria($shipmentTypeStorageCriteriaTransfer); |
||
81 | if ($shipmentTypeStorageCollectionTransfer->getShipmentTypeStorages()->count() === 0) { |
||
82 | return $shipmentTypeStorageCollectionTransfer; |
||
83 | } |
||
84 | |||
85 | return $this->executeShipmentTypeStorageExpanderPlugins($shipmentTypeStorageCollectionTransfer); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param \Generated\Shared\Transfer\ShipmentTypeStorageCriteriaTransfer $shipmentTypeStorageCriteriaTransfer |
||
90 | * |
||
91 | * @return \Generated\Shared\Transfer\ShipmentTypeStorageCollectionTransfer |
||
92 | */ |
||
93 | protected function getShipmentTypeStorageCollectionByCriteria( |
||
94 | ShipmentTypeStorageCriteriaTransfer $shipmentTypeStorageCriteriaTransfer |
||
95 | ): ShipmentTypeStorageCollectionTransfer { |
||
96 | $shipmentTypeStorageConditionsTransfer = $shipmentTypeStorageCriteriaTransfer->getShipmentTypeStorageConditionsOrFail(); |
||
97 | if ( |
||
98 | !$shipmentTypeStorageConditionsTransfer->getShipmentTypeIds() |
||
99 | && !$shipmentTypeStorageConditionsTransfer->getUuids() |
||
100 | ) { |
||
101 | return $this->getShipmentTypeStorageByStore($shipmentTypeStorageConditionsTransfer->getStoreNameOrFail()); |
||
102 | } |
||
103 | |||
104 | if ($shipmentTypeStorageConditionsTransfer->getUuids() !== []) { |
||
105 | return $this->getShipmentTypeStorageByUuids($shipmentTypeStorageConditionsTransfer); |
||
106 | } |
||
107 | |||
108 | if ($shipmentTypeStorageConditionsTransfer->getShipmentTypeIds() !== []) { |
||
109 | return $this->getShipmentTypeStorageByShipmentTypeIds($shipmentTypeStorageConditionsTransfer); |
||
110 | } |
||
111 | |||
112 | return new ShipmentTypeStorageCollectionTransfer(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param string $storeName |
||
117 | * |
||
118 | * @return \Generated\Shared\Transfer\ShipmentTypeStorageCollectionTransfer |
||
119 | */ |
||
120 | protected function getShipmentTypeStorageByStore(string $storeName): ShipmentTypeStorageCollectionTransfer |
||
121 | { |
||
122 | $shipmentTypeStorageConditionsTransfer = (new ShipmentTypeStorageConditionsTransfer()) |
||
123 | ->setUuids($this->shipmentTypeStorageKeyScanner->scanShipmentTypeUuids()) |
||
124 | ->setStoreName($storeName); |
||
125 | |||
126 | return $this->getShipmentTypeStorageByUuids($shipmentTypeStorageConditionsTransfer); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param \Generated\Shared\Transfer\ShipmentTypeStorageConditionsTransfer $shipmentTypeStorageConditionsTransfer |
||
131 | * |
||
132 | * @return \Generated\Shared\Transfer\ShipmentTypeStorageCollectionTransfer |
||
133 | */ |
||
134 | protected function getShipmentTypeStorageByUuids( |
||
135 | ShipmentTypeStorageConditionsTransfer $shipmentTypeStorageConditionsTransfer |
||
136 | ): ShipmentTypeStorageCollectionTransfer { |
||
137 | $shipmentTypeStorageCollectionTransfer = new ShipmentTypeStorageCollectionTransfer(); |
||
138 | |||
139 | $storageMappingKeys = $this->shipmentTypeStorageKeyGenerator->generateShipmentTypeStorageUuidMappingKeys( |
||
140 | $shipmentTypeStorageConditionsTransfer->getUuids(), |
||
141 | $shipmentTypeStorageConditionsTransfer->getStoreNameOrFail(), |
||
142 | ); |
||
143 | if ($storageMappingKeys === []) { |
||
144 | return $shipmentTypeStorageCollectionTransfer; |
||
145 | } |
||
146 | |||
147 | $storageMappingData = array_filter($this->storageClient->getMulti($storageMappingKeys)); |
||
148 | if ($storageMappingData === []) { |
||
149 | return $shipmentTypeStorageCollectionTransfer; |
||
150 | } |
||
151 | |||
152 | $shipmentTypeStorageConditionsTransfer->setShipmentTypeIds( |
||
153 | $this->extractShipmentTypeIdsFromStorageMappingData($storageMappingData), |
||
154 | ); |
||
155 | |||
156 | return $this->getShipmentTypeStorageByShipmentTypeIds($shipmentTypeStorageConditionsTransfer); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param \Generated\Shared\Transfer\ShipmentTypeStorageConditionsTransfer $shipmentTypeStorageConditionsTransfer |
||
161 | * |
||
162 | * @return \Generated\Shared\Transfer\ShipmentTypeStorageCollectionTransfer |
||
163 | */ |
||
164 | protected function getShipmentTypeStorageByShipmentTypeIds( |
||
165 | ShipmentTypeStorageConditionsTransfer $shipmentTypeStorageConditionsTransfer |
||
166 | ): ShipmentTypeStorageCollectionTransfer { |
||
167 | $shipmentTypeStorageCollectionTransfer = new ShipmentTypeStorageCollectionTransfer(); |
||
168 | |||
169 | $storageKeys = $this->shipmentTypeStorageKeyGenerator->generateShipmentTypeStorageKeys( |
||
170 | $shipmentTypeStorageConditionsTransfer->getShipmentTypeIds(), |
||
171 | $shipmentTypeStorageConditionsTransfer->getStoreNameOrFail(), |
||
172 | ); |
||
173 | if ($storageKeys === []) { |
||
174 | return $shipmentTypeStorageCollectionTransfer; |
||
175 | } |
||
176 | |||
177 | $shipmentTypeStorageData = array_filter($this->storageClient->getMulti($storageKeys)); |
||
178 | if ($shipmentTypeStorageData === []) { |
||
179 | return $shipmentTypeStorageCollectionTransfer; |
||
180 | } |
||
181 | |||
182 | /** @var array<string, mixed>|string $shipmentTypeStorageDataItem */ |
||
183 | foreach ($shipmentTypeStorageData as $shipmentTypeStorageDataItem) { |
||
184 | if (!is_array($shipmentTypeStorageDataItem)) { |
||
185 | $shipmentTypeStorageDataItem = $this->utilEncodingService->decodeJson($shipmentTypeStorageDataItem, true); |
||
186 | } |
||
187 | if (!$shipmentTypeStorageDataItem) { |
||
188 | continue; |
||
189 | } |
||
190 | |||
191 | $shipmentTypeStorageCollectionTransfer->addShipmentTypeStorage( |
||
192 | (new ShipmentTypeStorageTransfer())->fromArray($shipmentTypeStorageDataItem, true), |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | return $shipmentTypeStorageCollectionTransfer; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param array<string, array<string, string>|string> $storageMappingData |
||
201 | * |
||
202 | * @return list<int> |
||
203 | */ |
||
204 | protected function extractShipmentTypeIdsFromStorageMappingData(array $storageMappingData): array |
||
205 | { |
||
206 | $shipmentTypeIds = []; |
||
207 | foreach ($storageMappingData as $storageMappingDataItem) { |
||
208 | if (!is_array($storageMappingDataItem)) { |
||
209 | $storageMappingDataItem = $this->utilEncodingService->decodeJson($storageMappingDataItem, true); |
||
210 | } |
||
211 | |||
212 | if ($storageMappingDataItem === [] || !isset($storageMappingDataItem[static::KEY_ID])) { |
||
213 | continue; |
||
214 | } |
||
215 | |||
216 | $shipmentTypeIds[] = (int)$storageMappingDataItem[static::KEY_ID]; |
||
217 | } |
||
218 | |||
219 | return $shipmentTypeIds; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param \Generated\Shared\Transfer\ShipmentTypeStorageCollectionTransfer $shipmentTypeStorageCollectionTransfer |
||
224 | * |
||
225 | * @return \Generated\Shared\Transfer\ShipmentTypeStorageCollectionTransfer |
||
226 | */ |
||
227 | protected function executeShipmentTypeStorageExpanderPlugins( |
||
228 | ShipmentTypeStorageCollectionTransfer $shipmentTypeStorageCollectionTransfer |
||
229 | ): ShipmentTypeStorageCollectionTransfer { |
||
230 | foreach ($this->shipmentTypeStorageExpanderPlugins as $shipmentTypeStorageExpanderPlugin) { |
||
231 | $shipmentTypeStorageCollectionTransfer = $shipmentTypeStorageExpanderPlugin->expand($shipmentTypeStorageCollectionTransfer); |
||
232 | } |
||
233 | |||
234 | return $shipmentTypeStorageCollectionTransfer; |
||
235 | } |
||
236 | } |
||
237 |