| 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: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | final class SyncEntityResolver implements SyncEntityResolverInterface |
| 24: | { |
| 25: | |
| 26: | private SyncEntityProviderInterface $EntityProvider; |
| 27: | private string $NameProperty; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | public function __construct( |
| 35: | SyncEntityProviderInterface $entityProvider, |
| 36: | string $nameProperty |
| 37: | ) { |
| 38: | $this->EntityProvider = $entityProvider; |
| 39: | $this->NameProperty = $nameProperty; |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public function getByName(string $name, ?float &$uncertainty = null): ?SyncEntityInterface |
| 46: | { |
| 47: | try { |
| 48: | $entity = $this->doGetByName($name, [$this->NameProperty => $name]); |
| 49: | } catch (FilterPolicyViolationExceptionInterface $ex) { |
| 50: | $entity = $this->doGetByName($name); |
| 51: | } |
| 52: | |
| 53: | $uncertainty = $entity ? 0.0 : null; |
| 54: | return $entity; |
| 55: | } |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | private function doGetByName(string $name, ...$args): ?SyncEntityInterface |
| 62: | { |
| 63: | return $this |
| 64: | ->EntityProvider |
| 65: | ->getList(...$args) |
| 66: | ->getFirstWith($this->NameProperty, $name, true); |
| 67: | } |
| 68: | } |
| 69: | |