From 5fe169de3455386fdfc41b0a31e5fe0144ba8470 Mon Sep 17 00:00:00 2001 From: Daniel Badura Date: Fri, 20 May 2022 13:11:20 +0000 Subject: [PATCH] if no decorator is given, use RecordedOnDecorator --- src/Repository/DefaultRepository.php | 12 +++++------- src/Repository/DefaultRepositoryManager.php | 6 ++++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Repository/DefaultRepository.php b/src/Repository/DefaultRepository.php index 6dfced72..40b44f3e 100644 --- a/src/Repository/DefaultRepository.php +++ b/src/Repository/DefaultRepository.php @@ -6,7 +6,9 @@ use DateTimeImmutable; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; +use Patchlevel\EventSourcing\Clock\SystemClock; use Patchlevel\EventSourcing\EventBus\Decorator\MessageDecorator; +use Patchlevel\EventSourcing\EventBus\Decorator\RecordedOnDecorator; use Patchlevel\EventSourcing\EventBus\EventBus; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootMetadata; @@ -37,7 +39,7 @@ final class DefaultRepository implements Repository private ?SnapshotStore $snapshotStore; private LoggerInterface $logger; private AggregateRootMetadata $metadata; - private ?MessageDecorator $messageDecorator; + private MessageDecorator $messageDecorator; /** * @param class-string $aggregateClass @@ -54,7 +56,7 @@ public function __construct( $this->eventBus = $eventBus; $this->aggregateClass = $aggregateClass; $this->snapshotStore = $snapshotStore; - $this->messageDecorator = $messageDecorator; + $this->messageDecorator = $messageDecorator ?? new RecordedOnDecorator(new SystemClock()); $this->logger = $logger ?? new NullLogger(); $this->metadata = $aggregateClass::metadata(); } @@ -131,11 +133,7 @@ static function (object $event) use ($aggregate, &$playhead, $messageDecorator) ->withPlayhead(++$playhead) ->withRecordedOn(new DateTimeImmutable()); - if ($messageDecorator) { - $message = $messageDecorator($message); - } - - return $message; + return $messageDecorator($message); }, $events ); diff --git a/src/Repository/DefaultRepositoryManager.php b/src/Repository/DefaultRepositoryManager.php index d49581a7..a9663e76 100644 --- a/src/Repository/DefaultRepositoryManager.php +++ b/src/Repository/DefaultRepositoryManager.php @@ -5,7 +5,9 @@ namespace Patchlevel\EventSourcing\Repository; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; +use Patchlevel\EventSourcing\Clock\SystemClock; use Patchlevel\EventSourcing\EventBus\Decorator\MessageDecorator; +use Patchlevel\EventSourcing\EventBus\Decorator\RecordedOnDecorator; use Patchlevel\EventSourcing\EventBus\EventBus; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootClassNotRegistered; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry; @@ -22,7 +24,7 @@ final class DefaultRepositoryManager implements RepositoryManager private Store $store; private EventBus $eventBus; private ?SnapshotStore $snapshotStore; - private ?MessageDecorator $messageDecorator; + private MessageDecorator $messageDecorator; private LoggerInterface $logger; /** @var array, Repository> */ @@ -40,7 +42,7 @@ public function __construct( $this->store = $store; $this->eventBus = $eventBus; $this->snapshotStore = $snapshotStore; - $this->messageDecorator = $messageDecorator; + $this->messageDecorator = $messageDecorator ?? new RecordedOnDecorator(new SystemClock()); $this->logger = $logger ?? new NullLogger(); }