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\Zed\Synchronization\Business\Export; |
||
9 | |||
10 | use Generated\Shared\Transfer\SynchronizationDataTransfer; |
||
11 | use Generated\Shared\Transfer\SynchronizationQueueMessageTransfer; |
||
12 | use Iterator; |
||
13 | use Spryker\Zed\Kernel\Persistence\EntityManager\InstancePoolingTrait; |
||
14 | use Spryker\Zed\Synchronization\Business\Iterator\SynchronizationDataBulkRepositoryPluginIterator; |
||
15 | use Spryker\Zed\Synchronization\Business\Iterator\SynchronizationDataRepositoryPluginIterator; |
||
16 | use Spryker\Zed\Synchronization\Business\Message\QueueMessageCreatorInterface; |
||
17 | use Spryker\Zed\Synchronization\Dependency\Client\SynchronizationToQueueClientInterface; |
||
18 | use Spryker\Zed\Synchronization\Dependency\Facade\SynchronizationToStoreFacadeInterface; |
||
19 | use Spryker\Zed\Synchronization\Dependency\Service\SynchronizationToUtilEncodingServiceInterface; |
||
20 | use Spryker\Zed\Synchronization\SynchronizationConfig; |
||
21 | use Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataBulkRepositoryPluginInterface; |
||
22 | use Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataPluginInterface; |
||
23 | use Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataRepositoryPluginInterface; |
||
24 | |||
25 | class RepositoryExporter implements ExporterInterface |
||
26 | { |
||
27 | use InstancePoolingTrait; |
||
28 | |||
29 | /** |
||
30 | * @var \Spryker\Zed\Synchronization\Dependency\Client\SynchronizationToQueueClientInterface |
||
31 | */ |
||
32 | protected $queueClient; |
||
33 | |||
34 | /** |
||
35 | * @var \Spryker\Zed\Synchronization\Business\Message\QueueMessageCreatorInterface |
||
36 | */ |
||
37 | protected $queueMessageCreator; |
||
38 | |||
39 | /** |
||
40 | * @var array<\Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataQueryContainerPluginInterface> |
||
41 | */ |
||
42 | protected $synchronizationDataPlugins; |
||
43 | |||
44 | /** |
||
45 | * @var \Spryker\Zed\Synchronization\SynchronizationConfig |
||
46 | */ |
||
47 | protected SynchronizationConfig $synchronizationConfig; |
||
48 | |||
49 | /** |
||
50 | * @var \Spryker\Zed\Synchronization\Dependency\Service\SynchronizationToUtilEncodingServiceInterface |
||
51 | */ |
||
52 | protected $utilEncodingService; |
||
53 | |||
54 | /** |
||
55 | * @var \Spryker\Zed\Synchronization\Dependency\Facade\SynchronizationToStoreFacadeInterface |
||
56 | */ |
||
57 | protected SynchronizationToStoreFacadeInterface $storeFacade; |
||
58 | |||
59 | /** |
||
60 | * @param \Spryker\Zed\Synchronization\Dependency\Client\SynchronizationToQueueClientInterface $queueClient |
||
61 | * @param \Spryker\Zed\Synchronization\Dependency\Facade\SynchronizationToStoreFacadeInterface $storeFacade |
||
62 | * @param \Spryker\Zed\Synchronization\Business\Message\QueueMessageCreatorInterface $synchronizationQueueMessageCreator |
||
63 | * @param \Spryker\Zed\Synchronization\Dependency\Service\SynchronizationToUtilEncodingServiceInterface $utilEncodingService |
||
64 | * @param \Spryker\Zed\Synchronization\SynchronizationConfig $synchronizationConfig |
||
65 | */ |
||
66 | public function __construct( |
||
67 | SynchronizationToQueueClientInterface $queueClient, |
||
68 | SynchronizationToStoreFacadeInterface $storeFacade, |
||
69 | QueueMessageCreatorInterface $synchronizationQueueMessageCreator, |
||
70 | SynchronizationToUtilEncodingServiceInterface $utilEncodingService, |
||
71 | SynchronizationConfig $synchronizationConfig |
||
72 | ) { |
||
73 | $this->queueClient = $queueClient; |
||
74 | $this->queueMessageCreator = $synchronizationQueueMessageCreator; |
||
75 | $this->utilEncodingService = $utilEncodingService; |
||
76 | $this->storeFacade = $storeFacade; |
||
77 | $this->synchronizationConfig = $synchronizationConfig; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param array<\Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataRepositoryPluginInterface|\Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataBulkRepositoryPluginInterface> $plugins |
||
82 | * @param array<int> $ids |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | public function exportSynchronizedData(array $plugins, array $ids = []): void |
||
87 | { |
||
88 | $isInstancePoolingDisabled = false; |
||
89 | if ($this->synchronizationConfig->isRepositorySyncExportPropelInstancePoolingDisabled()) { |
||
90 | $isInstancePoolingDisabled = $this->disableInstancePooling(); |
||
91 | } |
||
92 | |||
93 | foreach ($plugins as $plugin) { |
||
94 | if ($plugin instanceof SynchronizationDataRepositoryPluginInterface) { |
||
95 | $this->exportData($ids, $plugin); |
||
96 | |||
97 | continue; |
||
98 | } |
||
99 | |||
100 | if ($plugin instanceof SynchronizationDataBulkRepositoryPluginInterface) { |
||
101 | $this->exportDataBulk($plugin, $ids); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | if ($isInstancePoolingDisabled) { |
||
106 | $this->enableInstancePooling(); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param array $ids |
||
112 | * @param \Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataRepositoryPluginInterface $plugin |
||
113 | * |
||
114 | * @return void |
||
115 | */ |
||
116 | protected function exportData(array $ids, SynchronizationDataRepositoryPluginInterface $plugin): void |
||
117 | { |
||
118 | foreach ($this->createSynchronizationDataRepositoryPluginIterator($ids, $plugin) as $synchronizationEntityTransfers) { |
||
119 | $this->syncData($plugin, $synchronizationEntityTransfers); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param array $ids |
||
125 | * @param \Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataRepositoryPluginInterface $plugin |
||
126 | * |
||
127 | * @return \Iterator |
||
128 | */ |
||
129 | protected function createSynchronizationDataRepositoryPluginIterator(array $ids, SynchronizationDataRepositoryPluginInterface $plugin): Iterator |
||
130 | { |
||
131 | return new SynchronizationDataRepositoryPluginIterator( |
||
132 | $plugin, |
||
133 | $this->synchronizationConfig->getSyncExportChunkSize(), |
||
134 | $ids, |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param \Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataBulkRepositoryPluginInterface $plugin |
||
140 | * @param array<int> $ids |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | protected function exportDataBulk(SynchronizationDataBulkRepositoryPluginInterface $plugin, array $ids = []): void |
||
145 | { |
||
146 | foreach ($this->createSynchronizationDataBulkRepositoryPluginIterator($ids, $plugin) as $synchronizationEntityTransfers) { |
||
147 | $this->syncData($plugin, $synchronizationEntityTransfers); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param array<int> $ids |
||
153 | * @param \Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataBulkRepositoryPluginInterface $plugin |
||
154 | * |
||
155 | * @return \Iterator |
||
156 | */ |
||
157 | protected function createSynchronizationDataBulkRepositoryPluginIterator(array $ids, SynchronizationDataBulkRepositoryPluginInterface $plugin): Iterator |
||
158 | { |
||
159 | return new SynchronizationDataBulkRepositoryPluginIterator( |
||
160 | $plugin, |
||
161 | $this->synchronizationConfig->getSyncExportChunkSize(), |
||
162 | $ids, |
||
163 | ); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param \Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataPluginInterface $plugin |
||
168 | * @param array $synchronizationEntities |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | protected function syncData(SynchronizationDataPluginInterface $plugin, array $synchronizationEntities): void |
||
173 | { |
||
174 | $queueSendMessageTransfers = []; |
||
175 | foreach ($synchronizationEntities as $synchronizationEntity) { |
||
176 | $store = $this->getStore($plugin->hasStore(), $synchronizationEntity); |
||
177 | $synchronizationQueueMessageTransfers = $this->getSynchronizationQueueMessageTransfers($plugin, $synchronizationEntity, $store); |
||
178 | |||
179 | foreach ($synchronizationQueueMessageTransfers as $synchronizationQueueMessageTransfer) { |
||
180 | $queueSendMessageTransfers[] = $this->queueMessageCreator->createQueueMessage( |
||
181 | $synchronizationQueueMessageTransfer, |
||
182 | $plugin, |
||
183 | $store, |
||
184 | ); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | $this->queueClient->sendMessages($plugin->getQueueName(), $queueSendMessageTransfers); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param bool $hasStore |
||
193 | * @param \Generated\Shared\Transfer\SynchronizationDataTransfer $entity |
||
194 | * |
||
195 | * @return string|null |
||
196 | */ |
||
197 | protected function getStore(bool $hasStore, SynchronizationDataTransfer $entity): ?string |
||
198 | { |
||
199 | if ($hasStore) { |
||
200 | return $entity->getStore(); |
||
201 | } |
||
202 | |||
203 | return null; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param \Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataPluginInterface $plugin |
||
208 | * @param \Generated\Shared\Transfer\SynchronizationDataTransfer $synchronizationDataTransfer |
||
209 | * @param string|null $store |
||
210 | * |
||
211 | * @return array<\Generated\Shared\Transfer\SynchronizationQueueMessageTransfer> |
||
212 | */ |
||
213 | protected function getSynchronizationQueueMessageTransfers( |
||
214 | SynchronizationDataPluginInterface $plugin, |
||
215 | SynchronizationDataTransfer $synchronizationDataTransfer, |
||
216 | ?string $store = null |
||
217 | ): array { |
||
218 | $synchronizationQueueMessageTransfers = []; |
||
219 | if ($store || !$this->storeFacade->isDynamicStoreEnabled()) { |
||
220 | return $this->createSynchronizationQueueMessageTransfers( |
||
221 | $plugin, |
||
222 | $synchronizationDataTransfer, |
||
223 | $synchronizationQueueMessageTransfers, |
||
224 | $store, |
||
225 | ); |
||
226 | } |
||
227 | |||
228 | foreach ($this->storeFacade->getAllStores() as $storeTransfer) { |
||
229 | $synchronizationQueueMessageTransfers = $this->createSynchronizationQueueMessageTransfers( |
||
230 | $plugin, |
||
231 | $synchronizationDataTransfer, |
||
232 | $synchronizationQueueMessageTransfers, |
||
233 | $storeTransfer->getNameOrFail(), |
||
234 | ); |
||
235 | } |
||
236 | |||
237 | return $synchronizationQueueMessageTransfers; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param \Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataPluginInterface $plugin |
||
242 | * @param \Generated\Shared\Transfer\SynchronizationDataTransfer $synchronizationDataTransfer |
||
243 | * @param array<\Generated\Shared\Transfer\SynchronizationQueueMessageTransfer> $synchronizationQueueMessageTransfers |
||
244 | * @param string|null $store |
||
245 | * |
||
246 | * @return array<\Generated\Shared\Transfer\SynchronizationQueueMessageTransfer> |
||
247 | */ |
||
248 | protected function createSynchronizationQueueMessageTransfers( |
||
249 | SynchronizationDataPluginInterface $plugin, |
||
250 | SynchronizationDataTransfer $synchronizationDataTransfer, |
||
251 | array $synchronizationQueueMessageTransfers, |
||
252 | ?string $store = null |
||
253 | ): array { |
||
254 | /** @var array $data */ |
||
255 | $data = $synchronizationDataTransfer->getData(); |
||
256 | |||
257 | $synchronizationQueueMessageTransfers[] = $this->createSynchronizationQueueMessageTransfer( |
||
258 | $plugin, |
||
259 | $synchronizationDataTransfer->getKey(), |
||
260 | $data, |
||
261 | $store, |
||
262 | ); |
||
263 | |||
264 | foreach ($this->getSynchronizationMappings($synchronizationDataTransfer) as $mappingKey => $mappingData) { |
||
265 | $synchronizationQueueMessageTransfers[] = $this->createSynchronizationQueueMessageTransfer($plugin, $mappingKey, $mappingData, $store); |
||
266 | } |
||
267 | |||
268 | return $synchronizationQueueMessageTransfers; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @param \Spryker\Zed\SynchronizationExtension\Dependency\Plugin\SynchronizationDataPluginInterface $plugin |
||
273 | * @param string $synchronizationKey |
||
274 | * @param array<string, mixed> $data |
||
275 | * @param string|null $store |
||
276 | * |
||
277 | * @return \Generated\Shared\Transfer\SynchronizationQueueMessageTransfer |
||
278 | */ |
||
279 | protected function createSynchronizationQueueMessageTransfer( |
||
280 | SynchronizationDataPluginInterface $plugin, |
||
281 | string $synchronizationKey, |
||
282 | array $data, |
||
283 | ?string $store = null |
||
284 | ): SynchronizationQueueMessageTransfer { |
||
285 | return (new SynchronizationQueueMessageTransfer()) |
||
286 | ->setKey($synchronizationKey) |
||
287 | ->setValue($data) |
||
288 | ->setResource($plugin->getResourceName()) |
||
289 | ->setParams($plugin->getParams()) |
||
290 | ->setStore($store); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param \Generated\Shared\Transfer\SynchronizationDataTransfer $synchronizationDataTransfer |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | protected function getSynchronizationMappings(SynchronizationDataTransfer $synchronizationDataTransfer): array |
||
299 | { |
||
300 | $synchronizationMappings = []; |
||
301 | |||
302 | if ($synchronizationDataTransfer->getAliasKeys()) { |
||
303 | $synchronizationMappings = $this->utilEncodingService->decodeJson($synchronizationDataTransfer->getAliasKeys(), true); |
||
304 | } |
||
305 | |||
306 | return $synchronizationMappings; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
307 | } |
||
308 | } |
||
309 |