-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add adapter repository for snapshot store
- Loading branch information
1 parent
9c96e5a
commit 6b385c5
Showing
4 changed files
with
85 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Snapshot; | ||
|
||
use Patchlevel\EventSourcing\Snapshot\Adapter\SnapshotAdapter; | ||
|
||
interface AdapterRepository | ||
{ | ||
public function get(string $name): SnapshotAdapter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Snapshot; | ||
|
||
use Patchlevel\EventSourcing\Snapshot\Adapter\SnapshotAdapter; | ||
|
||
use function array_key_exists; | ||
|
||
final class DefaultAdapterRepository implements AdapterRepository | ||
{ | ||
/** @param array<string, SnapshotAdapter> $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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<string, SnapshotAdapter> $snapshotAdapters */ | ||
/** @param array<string, SnapshotAdapter>|AdapterRepository $snapshotAdapters */ | ||
Check failure on line 29 in src/Snapshot/DefaultSnapshotStore.php GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)InvalidDocblockParamName
|
||
public function __construct( | ||
Check failure on line 30 in src/Snapshot/DefaultSnapshotStore.php GitHub Actions / Static Analysis by PHPStan (locked, 8.3, ubuntu-latest)
Check failure on line 30 in src/Snapshot/DefaultSnapshotStore.php GitHub Actions / Static Analysis by PHPStan (locked, 8.3, ubuntu-latest)
|
||
private array $snapshotAdapters, | ||
array|AdapterRepository $adapterRepository, | ||
Hydrator|null $hydrator = null, | ||
AggregateRootMetadataFactory|null $metadataFactory = null, | ||
) { | ||
if (is_array($adapterRepository)) { | ||
$this->adapterRepository = new DefaultAdapterRepository($adapterRepository); | ||
Check failure on line 36 in src/Snapshot/DefaultSnapshotStore.php GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)MixedArgumentTypeCoercion
|
||
} 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<AggregateRoot> $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 DefaultAdapterRepository($snapshotAdapters), | ||
new MetadataHydrator(cryptographer: $cryptographer), | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\Snapshot; | ||
|
||
use Patchlevel\EventSourcing\Snapshot\Adapter\SnapshotAdapter; | ||
use Patchlevel\EventSourcing\Snapshot\AdapterNotFound; | ||
use Patchlevel\EventSourcing\Snapshot\DefaultAdapterRepository; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** @covers \Patchlevel\EventSourcing\Snapshot\DefaultAdapterRepository */ | ||
final class DefaultAdapterRepositoryTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testGetAdapter(): void | ||
{ | ||
$adapter = $this->prophesize(SnapshotAdapter::class); | ||
$repository = new DefaultAdapterRepository(['memory' => $adapter->reveal()]); | ||
|
||
self::assertSame($adapter->reveal(), $repository->get('memory')); | ||
} | ||
|
||
public function testAdapterNotFound(): void | ||
{ | ||
$this->expectException(AdapterNotFound::class); | ||
|
||
$repository = new DefaultAdapterRepository([]); | ||
$repository->get('memory'); | ||
} | ||
} |