forked from marein/php-gaming-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryEventStore.php
40 lines (33 loc) · 994 Bytes
/
InMemoryEventStore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
namespace Gaming\Common\EventStore;
use Gaming\Common\Domain\DomainEvent;
use Psr\Clock\ClockInterface;
final class InMemoryEventStore implements EventStore
{
/**
* @var StoredEvent[]
*/
private array $storedEvents = [];
public function __construct(
private readonly ClockInterface $clock
) {
}
public function byAggregateId(string $aggregateId, int $sinceId = 0): array
{
return array_filter(
$this->storedEvents,
static function (StoredEvent $storedEvent) use ($aggregateId, $sinceId): bool {
return $storedEvent->domainEvent()->aggregateId() === $aggregateId && $storedEvent->id() > $sinceId;
}
);
}
public function append(DomainEvent $domainEvent): void
{
$this->storedEvents[] = new StoredEvent(
count($this->storedEvents) + 1,
$this->clock->now(),
$domainEvent
);
}
}