Skip to content

Commit

Permalink
add adapter repository for snapshot store
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Oct 15, 2024
1 parent 9c96e5a commit 3c6a340
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/Snapshot/AdapterRepository.php
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;
}
27 changes: 27 additions & 0 deletions src/Snapshot/ArrayAdapterRepository.php
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 ArrayAdapterRepository 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];
}
}
21 changes: 13 additions & 8 deletions src/Snapshot/DefaultSnapshotStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 $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();
}
Expand Down Expand Up @@ -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 */
Expand All @@ -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),
);
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/Snapshot/ArrayAdapterRepositoryTest.php
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\ArrayAdapterRepository;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

/** @covers \Patchlevel\EventSourcing\Snapshot\ArrayAdapterRepository */
final class ArrayAdapterRepositoryTest extends TestCase
{
use ProphecyTrait;

public function testGetAdapter(): void
{
$adapter = $this->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');
}
}

0 comments on commit 3c6a340

Please sign in to comment.