From 3c6a340fba8ad88b0ee7a8de1c947ad068fe356b Mon Sep 17 00:00:00 2001 From: David Badura Date: Tue, 15 Oct 2024 15:29:38 +0200 Subject: [PATCH] add adapter repository for snapshot store --- src/Snapshot/AdapterRepository.php | 12 +++++++ src/Snapshot/ArrayAdapterRepository.php | 27 +++++++++++++++ src/Snapshot/DefaultSnapshotStore.php | 21 +++++++----- .../Snapshot/ArrayAdapterRepositoryTest.php | 33 +++++++++++++++++++ 4 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 src/Snapshot/AdapterRepository.php create mode 100644 src/Snapshot/ArrayAdapterRepository.php create mode 100644 tests/Unit/Snapshot/ArrayAdapterRepositoryTest.php diff --git a/src/Snapshot/AdapterRepository.php b/src/Snapshot/AdapterRepository.php new file mode 100644 index 000000000..e9254ecda --- /dev/null +++ b/src/Snapshot/AdapterRepository.php @@ -0,0 +1,12 @@ + $snapshotAdapters */ + public function __construct( + private readonly array $snapshotAdapters, + ) { + } + + public function get(string $name): SnapshotAdapter + { + if (!array_key_exists($name, $this->snapshotAdapters)) { + throw new AdapterNotFound($name); + } + + return $this->snapshotAdapters[$name]; + } +} diff --git a/src/Snapshot/DefaultSnapshotStore.php b/src/Snapshot/DefaultSnapshotStore.php index 6a0716302..c52a60bef 100644 --- a/src/Snapshot/DefaultSnapshotStore.php +++ b/src/Snapshot/DefaultSnapshotStore.php @@ -15,20 +15,29 @@ use Throwable; use function array_key_exists; +use function is_array; use function sprintf; final class DefaultSnapshotStore implements SnapshotStore { + private AdapterRepository $adapterRepository; + private Hydrator $hydrator; private AggregateRootMetadataFactory $metadataFactory; - /** @param array $snapshotAdapters */ + /** @param array|AdapterRepository $adapterRepository */ public function __construct( - private array $snapshotAdapters, + array|AdapterRepository $adapterRepository, Hydrator|null $hydrator = null, AggregateRootMetadataFactory|null $metadataFactory = null, ) { + if (is_array($adapterRepository)) { + $this->adapterRepository = new ArrayAdapterRepository($adapterRepository); + } else { + $this->adapterRepository = $adapterRepository; + } + $this->hydrator = $hydrator ?? new MetadataHydrator(); $this->metadataFactory = $metadataFactory ?? new AggregateRootMetadataAwareMetadataFactory(); } @@ -91,11 +100,7 @@ public function adapter(string $aggregateClass): SnapshotAdapter throw new SnapshotNotConfigured($aggregateClass); } - if (!array_key_exists($adapterName, $this->snapshotAdapters)) { - throw new AdapterNotFound($adapterName); - } - - return $this->snapshotAdapters[$adapterName]; + return $this->adapterRepository->get($adapterName); } /** @param class-string $aggregateClass */ @@ -116,7 +121,7 @@ private function version(string $aggregateClass): string|null public static function createDefault(array $snapshotAdapters, PayloadCryptographer|null $cryptographer = null): self { return new self( - $snapshotAdapters, + new ArrayAdapterRepository($snapshotAdapters), new MetadataHydrator(cryptographer: $cryptographer), ); } diff --git a/tests/Unit/Snapshot/ArrayAdapterRepositoryTest.php b/tests/Unit/Snapshot/ArrayAdapterRepositoryTest.php new file mode 100644 index 000000000..bdfbdaf3d --- /dev/null +++ b/tests/Unit/Snapshot/ArrayAdapterRepositoryTest.php @@ -0,0 +1,33 @@ +prophesize(SnapshotAdapter::class); + $repository = new ArrayAdapterRepository(['memory' => $adapter->reveal()]); + + self::assertSame($adapter->reveal(), $repository->get('memory')); + } + + public function testAdapterNotFound(): void + { + $this->expectException(AdapterNotFound::class); + + $repository = new ArrayAdapterRepository([]); + $repository->get('memory'); + } +}