1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Sync\Support; |
4: | |
5: | use Salient\Contract\Container\ContainerInterface; |
6: | use Salient\Contract\Core\TextComparisonAlgorithm; |
7: | use Salient\Contract\Iterator\FluentIteratorInterface; |
8: | use Salient\Contract\Sync\DeferralPolicy; |
9: | use Salient\Contract\Sync\HydrationPolicy; |
10: | use Salient\Contract\Sync\SyncContextInterface; |
11: | use Salient\Contract\Sync\SyncDefinitionInterface; |
12: | use Salient\Contract\Sync\SyncEntityInterface; |
13: | use Salient\Contract\Sync\SyncEntityProviderInterface; |
14: | use Salient\Contract\Sync\SyncEntityResolverInterface; |
15: | use Salient\Contract\Sync\SyncOperation; |
16: | use Salient\Contract\Sync\SyncProviderInterface; |
17: | use Salient\Contract\Sync\SyncStoreInterface; |
18: | use Salient\Iterator\IterableIterator; |
19: | use Salient\Sync\Exception\SyncOperationNotImplementedException; |
20: | use Salient\Sync\Reflection\ReflectionSyncProvider; |
21: | use Salient\Sync\SyncUtil; |
22: | use Generator; |
23: | use LogicException; |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | final class SyncEntityProvider implements SyncEntityProviderInterface |
49: | { |
50: | |
51: | private string $Entity; |
52: | |
53: | private SyncProviderInterface $Provider; |
54: | |
55: | private SyncDefinitionInterface $Definition; |
56: | private SyncContextInterface $Context; |
57: | private SyncStoreInterface $Store; |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | public function __construct( |
64: | ContainerInterface $container, |
65: | SyncProviderInterface $provider, |
66: | string $entity, |
67: | ?SyncContextInterface $context = null |
68: | ) { |
69: | if (!is_a($entity, SyncEntityInterface::class, true)) { |
70: | throw new LogicException(sprintf( |
71: | '%s does not implement %s', |
72: | $entity, |
73: | SyncEntityInterface::class, |
74: | )); |
75: | } |
76: | |
77: | if ($context && $context->getProvider() !== $provider) { |
78: | throw new LogicException(sprintf( |
79: | 'Context has a different provider (%s, expected %s)', |
80: | $context->getProvider()->getName(), |
81: | $provider->getName(), |
82: | )); |
83: | } |
84: | |
85: | if (!(new ReflectionSyncProvider($provider))->isSyncEntityProvider($entity)) { |
86: | throw new LogicException(sprintf( |
87: | '%s does not service %s', |
88: | get_class($provider), |
89: | $entity, |
90: | )); |
91: | } |
92: | |
93: | $this->Entity = $entity; |
94: | $this->Provider = $provider; |
95: | $this->Definition = $provider->getDefinition($entity); |
96: | $this->Context = ($context ?? $provider->getContext())->withContainer($container); |
97: | $this->Store = $provider->getStore(); |
98: | } |
99: | |
100: | |
101: | |
102: | |
103: | public function getProvider(): SyncProviderInterface |
104: | { |
105: | return $this->Provider; |
106: | } |
107: | |
108: | |
109: | |
110: | |
111: | public function entity(): string |
112: | { |
113: | return $this->Entity; |
114: | } |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | private function _run(int $operation, ...$args) |
127: | { |
128: | $closure = |
129: | $this |
130: | ->Definition |
131: | ->getOperationClosure($operation); |
132: | |
133: | if (!$closure) { |
134: | throw new SyncOperationNotImplementedException( |
135: | $this->Provider, |
136: | $this->Entity, |
137: | $operation |
138: | ); |
139: | } |
140: | |
141: | return $closure( |
142: | $this->Context->withOperation($operation, $this->Entity, ...$args), |
143: | ...$args |
144: | ); |
145: | } |
146: | |
147: | |
148: | |
149: | |
150: | public function run(int $operation, ...$args) |
151: | { |
152: | $fromCheckpoint = $this->Store->getDeferralCheckpoint(); |
153: | $deferralPolicy = $this->Context->getDeferralPolicy(); |
154: | |
155: | if (!SyncUtil::isListOperation($operation)) { |
156: | $result = $this->_run($operation, ...$args); |
157: | |
158: | if ($deferralPolicy === DeferralPolicy::RESOLVE_LATE) { |
159: | $this->Store->resolveDeferrals($fromCheckpoint); |
160: | } |
161: | |
162: | return $result; |
163: | } |
164: | |
165: | switch ($deferralPolicy) { |
166: | case DeferralPolicy::DO_NOT_RESOLVE: |
167: | case DeferralPolicy::RESOLVE_EARLY: |
168: | $result = $this->_run($operation, ...$args); |
169: | break; |
170: | |
171: | case DeferralPolicy::RESOLVE_LATE: |
172: | $result = $this->resolveDeferredEntitiesAfterRun( |
173: | $fromCheckpoint, |
174: | $operation, |
175: | ...$args, |
176: | ); |
177: | break; |
178: | |
179: | default: |
180: | throw new LogicException(sprintf( |
181: | 'Invalid deferral policy: %d', |
182: | $deferralPolicy, |
183: | )); |
184: | } |
185: | |
186: | if (!$result instanceof FluentIteratorInterface) { |
187: | return new IterableIterator($result); |
188: | } |
189: | |
190: | return $result; |
191: | } |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | private function resolveDeferredEntitiesAfterRun(int $fromCheckpoint, int $operation, ...$args): Generator |
199: | { |
200: | yield from $this->_run($operation, ...$args); |
201: | $this->Store->resolveDeferrals($fromCheckpoint); |
202: | } |
203: | |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | |
224: | |
225: | |
226: | public function create($entity, ...$args): SyncEntityInterface |
227: | { |
228: | return $this->run(SyncOperation::CREATE, $entity, ...$args); |
229: | } |
230: | |
231: | |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | |
249: | |
250: | |
251: | |
252: | |
253: | |
254: | |
255: | public function get($id, ...$args): SyncEntityInterface |
256: | { |
257: | $offline = $this->Context->getOffline(); |
258: | if ($offline !== false) { |
259: | if ($id === null) { |
260: | throw new LogicException('$id cannot be null when working offline'); |
261: | } |
262: | $entity = $this->Store->registerEntityType($this->Entity)->getEntity( |
263: | $this->Provider->getProviderId(), |
264: | $this->Entity, |
265: | $id, |
266: | $offline, |
267: | ); |
268: | if ($entity) { |
269: | return $entity; |
270: | } |
271: | } |
272: | |
273: | return $this->run(SyncOperation::READ, $id, ...$args); |
274: | } |
275: | |
276: | |
277: | |
278: | |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | |
286: | |
287: | |
288: | |
289: | |
290: | |
291: | |
292: | |
293: | |
294: | |
295: | |
296: | |
297: | |
298: | public function update($entity, ...$args): SyncEntityInterface |
299: | { |
300: | return $this->run(SyncOperation::UPDATE, $entity, ...$args); |
301: | } |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | |
308: | |
309: | |
310: | |
311: | |
312: | |
313: | |
314: | |
315: | |
316: | |
317: | |
318: | |
319: | |
320: | |
321: | |
322: | |
323: | |
324: | |
325: | |
326: | |
327: | |
328: | public function delete($entity, ...$args): SyncEntityInterface |
329: | { |
330: | return $this->run(SyncOperation::DELETE, $entity, ...$args); |
331: | } |
332: | |
333: | |
334: | |
335: | |
336: | |
337: | |
338: | |
339: | |
340: | |
341: | |
342: | |
343: | |
344: | |
345: | |
346: | |
347: | |
348: | |
349: | |
350: | |
351: | |
352: | |
353: | |
354: | |
355: | |
356: | |
357: | public function createList(iterable $entities, ...$args): FluentIteratorInterface |
358: | { |
359: | return $this->run(SyncOperation::CREATE_LIST, $entities, ...$args); |
360: | } |
361: | |
362: | |
363: | |
364: | |
365: | |
366: | |
367: | |
368: | |
369: | |
370: | |
371: | |
372: | |
373: | |
374: | |
375: | |
376: | |
377: | |
378: | |
379: | |
380: | public function getList(...$args): FluentIteratorInterface |
381: | { |
382: | return $this->run(SyncOperation::READ_LIST, ...$args); |
383: | } |
384: | |
385: | |
386: | |
387: | |
388: | |
389: | |
390: | |
391: | |
392: | |
393: | |
394: | |
395: | |
396: | |
397: | |
398: | |
399: | |
400: | |
401: | |
402: | |
403: | |
404: | |
405: | |
406: | |
407: | |
408: | |
409: | public function updateList(iterable $entities, ...$args): FluentIteratorInterface |
410: | { |
411: | return $this->run(SyncOperation::UPDATE_LIST, $entities, ...$args); |
412: | } |
413: | |
414: | |
415: | |
416: | |
417: | |
418: | |
419: | |
420: | |
421: | |
422: | |
423: | |
424: | |
425: | |
426: | |
427: | |
428: | |
429: | |
430: | |
431: | |
432: | |
433: | |
434: | |
435: | |
436: | |
437: | |
438: | |
439: | |
440: | |
441: | public function deleteList(iterable $entities, ...$args): FluentIteratorInterface |
442: | { |
443: | return $this->run(SyncOperation::DELETE_LIST, $entities, ...$args); |
444: | } |
445: | |
446: | public function runA(int $operation, ...$args): array |
447: | { |
448: | if (!SyncUtil::isListOperation($operation)) { |
449: | throw new LogicException('Not a *_LIST operation: ' . $operation); |
450: | } |
451: | |
452: | $fromCheckpoint = $this->Store->getDeferralCheckpoint(); |
453: | $deferralPolicy = $this->Context->getDeferralPolicy(); |
454: | |
455: | $result = $this->_run($operation, ...$args); |
456: | if (!is_array($result)) { |
457: | $result = iterator_to_array($result, false); |
458: | } |
459: | |
460: | if ($deferralPolicy === DeferralPolicy::RESOLVE_LATE) { |
461: | $this->Store->resolveDeferrals($fromCheckpoint); |
462: | } |
463: | |
464: | return $result; |
465: | } |
466: | |
467: | public function createListA(iterable $entities, ...$args): array |
468: | { |
469: | return $this->runA(SyncOperation::CREATE_LIST, $entities, ...$args); |
470: | } |
471: | |
472: | public function getListA(...$args): array |
473: | { |
474: | return $this->runA(SyncOperation::READ_LIST, ...$args); |
475: | } |
476: | |
477: | public function updateListA(iterable $entities, ...$args): array |
478: | { |
479: | return $this->runA(SyncOperation::UPDATE_LIST, $entities, ...$args); |
480: | } |
481: | |
482: | public function deleteListA(iterable $entities, ...$args): array |
483: | { |
484: | return $this->runA(SyncOperation::DELETE_LIST, $entities, ...$args); |
485: | } |
486: | |
487: | |
488: | |
489: | |
490: | public function online() |
491: | { |
492: | $this->Context = $this->Context->withOffline(false); |
493: | return $this; |
494: | } |
495: | |
496: | |
497: | |
498: | |
499: | public function offline() |
500: | { |
501: | $this->Context = $this->Context->withOffline(true); |
502: | return $this; |
503: | } |
504: | |
505: | |
506: | |
507: | |
508: | public function offlineFirst() |
509: | { |
510: | $this->Context = $this->Context->withOffline(null); |
511: | return $this; |
512: | } |
513: | |
514: | |
515: | |
516: | |
517: | public function doNotResolve() |
518: | { |
519: | $this->Context = $this->Context->withDeferralPolicy( |
520: | DeferralPolicy::DO_NOT_RESOLVE, |
521: | ); |
522: | return $this; |
523: | } |
524: | |
525: | |
526: | |
527: | |
528: | public function resolveEarly() |
529: | { |
530: | $this->Context = $this->Context->withDeferralPolicy( |
531: | DeferralPolicy::RESOLVE_EARLY, |
532: | ); |
533: | return $this; |
534: | } |
535: | |
536: | |
537: | |
538: | |
539: | public function resolveLate() |
540: | { |
541: | $this->Context = $this->Context->withDeferralPolicy( |
542: | DeferralPolicy::RESOLVE_LATE, |
543: | ); |
544: | return $this; |
545: | } |
546: | |
547: | |
548: | |
549: | |
550: | public function doNotHydrate() |
551: | { |
552: | $this->Context = $this->Context->withHydrationPolicy( |
553: | HydrationPolicy::SUPPRESS, |
554: | ); |
555: | |
556: | return $this; |
557: | } |
558: | |
559: | |
560: | |
561: | |
562: | public function hydrate( |
563: | int $policy = HydrationPolicy::EAGER, |
564: | ?string $entity = null, |
565: | $depth = null |
566: | ) { |
567: | $this->Context = $this->Context->withHydrationPolicy( |
568: | $policy, |
569: | $entity, |
570: | $depth, |
571: | ); |
572: | return $this; |
573: | } |
574: | |
575: | |
576: | |
577: | |
578: | public function getResolver( |
579: | ?string $nameProperty = null, |
580: | int $algorithm = TextComparisonAlgorithm::SAME, |
581: | $uncertaintyThreshold = null, |
582: | ?string $weightProperty = null, |
583: | bool $requireOneMatch = false |
584: | ): SyncEntityResolverInterface { |
585: | if ($nameProperty !== null |
586: | && $algorithm === TextComparisonAlgorithm::SAME |
587: | && $weightProperty === null |
588: | && !$requireOneMatch) { |
589: | return new SyncEntityResolver($this, $nameProperty); |
590: | } |
591: | return new SyncEntityFuzzyResolver( |
592: | $this, |
593: | $nameProperty, |
594: | $algorithm, |
595: | $uncertaintyThreshold, |
596: | $weightProperty, |
597: | $requireOneMatch, |
598: | ); |
599: | } |
600: | } |
601: | |