Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom serializer while persisting events #464

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}