Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add adapter repository for snapshot store #639

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}
Loading