Skip to content

Commit

Permalink
Merge pull request #464 from sebastiandittrich/main
Browse files Browse the repository at this point in the history
Use custom serializer while persisting events
  • Loading branch information
freekmurze authored May 28, 2024
2 parents 9d2f438 + d685776 commit 0625ce8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/StoredEvents/Repositories/EloquentStoredEventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,8 +87,19 @@ public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent

$createdAt = Carbon::now();

try {
$reflectionClass = new ReflectionClass(get_class($event));
} catch (ReflectionException) {
throw new InvalidStoredEvent();
}

$serializerClass = EventSerializer::class;
if ($serializerAttribute = $reflectionClass->getAttributes(EventSerializerAttribute::class)[0] ?? null) {
$serializerClass = $serializerAttribute->newInstance()->serializerClass;
}

$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(),
Expand Down
11 changes: 11 additions & 0 deletions tests/EloquentStoredEventRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
15 changes: 15 additions & 0 deletions tests/TestClasses/EventSerializer/DummySerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\EventSourcing\Tests\TestClasses\EventSerializer;

use DateTimeZone;
use Spatie\EventSourcing\EventSerializers\JsonEventSerializer;
use Spatie\EventSourcing\StoredEvents\ShouldBeStored;

class DummySerializer extends JsonEventSerializer
{
public function serialize(ShouldBeStored $event): string
{
return '{"message":"message set by custom serializer"}';
}
}
18 changes: 18 additions & 0 deletions tests/TestClasses/Events/EventWithCustomSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Spatie\EventSourcing\Tests\TestClasses\Events;

use Spatie\EventSourcing\Attributes\EventSerializer;
use Spatie\EventSourcing\StoredEvents\ShouldBeStored;
use Spatie\EventSourcing\Tests\TestClasses\EventSerializer\DummySerializer;

#[EventSerializer(DummySerializer::class)]
class EventWithCustomSerializer extends ShouldBeStored
{
public string $message;

public function __construct(string $message)
{
$this->message = $message;
}
}

0 comments on commit 0625ce8

Please sign in to comment.