[Event Sourcing] Making Progress

This commit is contained in:
Carlos Buenosvinos
2022-04-08 07:56:08 +02:00
parent eb7be317c1
commit 0e0dc32919
3 changed files with 34 additions and 23 deletions

View File

@@ -9,6 +9,7 @@ interface EventStore
{
public function append(EventStream $eventStream): void;
public function getEventsFor(string $id): EventStream;
public function fromVersion(string $id, int $version): EventStream;
public function countEventsFor(string $id): int;
}

View File

@@ -6,8 +6,9 @@ namespace Cheeper\Chapter9\Infrastructure\DomainModel\Author;
use Cheeper\AllChapters\DomainModel\Author\AuthorId;
use Cheeper\AllChapters\DomainModel\Author\UserName;
use Cheeper\Chapter7\DomainModel\Author\Author;
use Cheeper\Chapter7\DomainModel\Author\AuthorRepository;
use Cheeper\Chapter9\DomainModel\Author\Author;
use Cheeper\Chapter9\DomainModel\EventStore;
//snippet code
class EventSourcedAuthorRepository implements AuthorRepository
@@ -21,14 +22,16 @@ class EventSourcedAuthorRepository implements AuthorRepository
public function ofId(AuthorId $authorId): Author
{
return Author:: ::reconstitute(
return Author::reconstitute(
$this->eventStore->getEventsFor($authorId->id())
);
}
public function ofUserName(UserName $userName): ?Author
{
// TODO: Implement ofUserName() method.
// It's not possible to look
// for an entity by the username
// except that we consume
}
public function add(Author $author): void

View File

@@ -1,40 +1,47 @@
<?php
namespace Architecture\ES\Infrastructure;
declare(strict_types=1);
use Zumba\JsonSerializer\JsonSerializer;
namespace Cheeper\Chapter9\Infrastructure\DomainModel;
use Architecture\CQRS\Domain\DomainEvent;
use Architecture\ES\Domain\EventStream;
use Predis\Client;
use Cheeper\Chapter7\DomainModel\DomainEvent;
use Cheeper\Chapter9\DomainModel\EventStore;
use Cheeper\Chapter9\DomainModel\EventStream;
use Redis;
use Symfony\Component\Serializer\Serializer;
//snippet event-store
class EventStore
//snippet code
class RedisEventStore implements EventStore
{
private Client $redis;
private JsonSerializer $serializer;
private Redis $redis;
private Serializer $serializer;
public function __construct(Client $redis, JsonSerializer $serializer)
public function __construct(
Redis $redis,
Serializer $serializer
)
{
$this->redis = $redis;
$this->serializer = $serializer;
}
public function append(EventStream $eventstream): void
public function append(EventStream $eventStream): void
{
/** @var DomainEvent */
foreach ($eventstream as $event) {
$data = $this->serializer->serialize($event);
foreach ($eventStream as $event) {
$data = $this->serializer->serialize($event, 'json');
$date = (new \DateTimeImmutable())->format('YmdHis');
$this->redis->rpush(
'events:' . $eventstream->getAggregateId(),
'events:' . $eventStream->getAggregateId(),
$this->serializer->serialize([
'type' => get_class($event),
'created_on' => $date,
'data' => $data
])
'type' => get_class($event),
'created_on' => $date,
'data' => $data
],
'json'
)
);
}
}
@@ -57,12 +64,12 @@ class EventStore
/** @var string */
foreach ($serializedEvents as $serializedEvent) {
$event = (array) $this->serializer->unserialize($serializedEvent);
$event = (array) $this->serializer->deserialize($serializedEvent);
$eventData = (string) $event['data'];
/** @var DomainEvent */
$events[] = $this->serializer->unserialize($eventData);
$events[] = $this->serializer->deserialize($eventData);
}
return new EventStream($id, $events);