forked from marein/php-gaming-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryEventStore.php
35 lines (29 loc) · 905 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
<?php
declare(strict_types=1);
namespace Gaming\Common\EventStore;
use Gaming\Common\Clock\Clock;
use Gaming\Common\Domain\DomainEvent;
final class InMemoryEventStore implements EventStore
{
/**
* @var StoredEvent[]
*/
private array $storedEvents = [];
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,
Clock::instance()->now(),
$domainEvent
);
}
}