1: <?php declare(strict_types=1);
2:
3: namespace Salient\Sync\Support;
4:
5: use Salient\Contract\Sync\Exception\FilterPolicyViolationExceptionInterface;
6: use Salient\Contract\Sync\SyncEntityInterface;
7: use Salient\Contract\Sync\SyncEntityProviderInterface;
8: use Salient\Contract\Sync\SyncEntityResolverInterface;
9:
10: /**
11: * Resolves a name to an entity
12: *
13: * @template TEntity of SyncEntityInterface
14: *
15: * @implements SyncEntityResolverInterface<TEntity>
16: */
17: final class SyncEntityResolver implements SyncEntityResolverInterface
18: {
19: /** @var SyncEntityProviderInterface<TEntity> */
20: private $EntityProvider;
21: /** @var string */
22: private $NameProperty;
23:
24: /**
25: * @param SyncEntityProviderInterface<TEntity> $entityProvider
26: */
27: public function __construct(SyncEntityProviderInterface $entityProvider, string $nameProperty)
28: {
29: $this->EntityProvider = $entityProvider;
30: $this->NameProperty = $nameProperty;
31: }
32:
33: public function getByName(string $name, ?float &$uncertainty = null): ?SyncEntityInterface
34: {
35: $match = null;
36: foreach ([[[$this->NameProperty => $name]], []] as $args) {
37: try {
38: $match = $this
39: ->EntityProvider
40: ->getList(...$args)
41: ->nextWithValue($this->NameProperty, $name);
42: break;
43: } catch (FilterPolicyViolationExceptionInterface $ex) {
44: $match = null;
45: continue;
46: }
47: }
48:
49: if ($match === null) {
50: $uncertainty = null;
51: return null;
52: }
53:
54: $uncertainty = 0.0;
55: return $match;
56: }
57: }
58: