From 4e84506933b5c9cd04c6b368fcb17ac001f492d5 Mon Sep 17 00:00:00 2001 From: Sebastian Dittrich Date: Sun, 26 May 2024 16:10:17 +0200 Subject: [PATCH 1/2] Use custom serializer while persisting events --- .../EloquentStoredEventRepository.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/StoredEvents/Repositories/EloquentStoredEventRepository.php b/src/StoredEvents/Repositories/EloquentStoredEventRepository.php index 5259732d..a58305c2 100644 --- a/src/StoredEvents/Repositories/EloquentStoredEventRepository.php +++ b/src/StoredEvents/Repositories/EloquentStoredEventRepository.php @@ -5,10 +5,14 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\LazyCollection; +use ReflectionClass; +use ReflectionException; use Spatie\EventSourcing\AggregateRoots\Exceptions\InvalidEloquentStoredEventModel; +use Spatie\EventSourcing\Attributes\EventSerializer as EventSerializerAttribute; use Spatie\EventSourcing\Enums\MetaData; use Spatie\EventSourcing\EventSerializers\EventSerializer; use Spatie\EventSourcing\StoredEvents\Exceptions\EventClassMapMissing; +use Spatie\EventSourcing\StoredEvents\Exceptions\InvalidStoredEvent; use Spatie\EventSourcing\StoredEvents\Models\EloquentStoredEvent; use Spatie\EventSourcing\StoredEvents\Models\EloquentStoredEventQueryBuilder; use Spatie\EventSourcing\StoredEvents\ShouldBeStored; @@ -83,8 +87,20 @@ public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent $createdAt = Carbon::now(); + try { + $reflectionClass = new ReflectionClass(get_class($event)); + } catch (ReflectionException) { + throw new InvalidStoredEvent(); + } + + if ($serializerAttribute = $reflectionClass->getAttributes(EventSerializerAttribute::class)[0] ?? null) { + $serializerClass = ($serializerAttribute->newInstance())->serializerClass; + } else { + $serializerClass = EventSerializer::class; + } + $eloquentStoredEvent->setRawAttributes([ - 'event_properties' => app(EventSerializer::class)->serialize(clone $event), + 'event_properties' => app($serializerClass)->serialize(clone $event), 'aggregate_uuid' => $uuid, 'aggregate_version' => $event->aggregateRootVersion(), 'event_version' => $event->eventVersion(), From d68577656586293ef68f5c755d16b97acd27d501 Mon Sep 17 00:00:00 2001 From: Sebastian Dittrich Date: Mon, 27 May 2024 14:18:39 +0200 Subject: [PATCH 2/2] Add Tests and fix style --- .../EloquentStoredEventRepository.php | 5 ++--- tests/EloquentStoredEventRepositoryTest.php | 11 +++++++++++ .../EventSerializer/DummySerializer.php | 15 +++++++++++++++ .../Events/EventWithCustomSerializer.php | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/TestClasses/EventSerializer/DummySerializer.php create mode 100644 tests/TestClasses/Events/EventWithCustomSerializer.php diff --git a/src/StoredEvents/Repositories/EloquentStoredEventRepository.php b/src/StoredEvents/Repositories/EloquentStoredEventRepository.php index a58305c2..f81046c1 100644 --- a/src/StoredEvents/Repositories/EloquentStoredEventRepository.php +++ b/src/StoredEvents/Repositories/EloquentStoredEventRepository.php @@ -93,10 +93,9 @@ public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent throw new InvalidStoredEvent(); } + $serializerClass = EventSerializer::class; if ($serializerAttribute = $reflectionClass->getAttributes(EventSerializerAttribute::class)[0] ?? null) { - $serializerClass = ($serializerAttribute->newInstance())->serializerClass; - } else { - $serializerClass = EventSerializer::class; + $serializerClass = $serializerAttribute->newInstance()->serializerClass; } $eloquentStoredEvent->setRawAttributes([ diff --git a/tests/EloquentStoredEventRepositoryTest.php b/tests/EloquentStoredEventRepositoryTest.php index f1a313d8..20583785 100644 --- a/tests/EloquentStoredEventRepositoryTest.php +++ b/tests/EloquentStoredEventRepositoryTest.php @@ -8,6 +8,7 @@ use Spatie\EventSourcing\StoredEvents\Repositories\EloquentStoredEventRepository; use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\AccountAggregateRoot; use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyAdded; +use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithCustomSerializer; it('can get the latest version id for a given aggregate uuid', function () { $eloquentStoredEventRepository = new EloquentStoredEventRepository(); @@ -37,3 +38,13 @@ assertSame($originalEvent, $storedEvent->event); }); + +it('uses the custom serializer if one is set', function () { + $eloquentStoredEventRepository = app(EloquentStoredEventRepository::class); + + $originalEvent = new EventWithCustomSerializer('default message'); + $storedEvent = $eloquentStoredEventRepository->persist($originalEvent, 'uuid-1', 1); + + $eventFromDatabase = $eloquentStoredEventRepository->find($storedEvent->id)->event; + assertSame('message set by custom serializer', $eventFromDatabase->message); +}); diff --git a/tests/TestClasses/EventSerializer/DummySerializer.php b/tests/TestClasses/EventSerializer/DummySerializer.php new file mode 100644 index 00000000..7f12eb2b --- /dev/null +++ b/tests/TestClasses/EventSerializer/DummySerializer.php @@ -0,0 +1,15 @@ +message = $message; + } +}