Skip to content
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
16 changes: 12 additions & 4 deletions src/Serializer/DefaultEventSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@
use Patchlevel\EventSourcing\Serializer\Encoder\JsonEncoder;
use Patchlevel\EventSourcing\Serializer\Hydrator\EventHydrator;
use Patchlevel\EventSourcing\Serializer\Hydrator\MetadataEventHydrator;
use Patchlevel\EventSourcing\Serializer\Upcast\Upcast;
use Patchlevel\EventSourcing\Serializer\Upcast\Upcaster;
use Patchlevel\EventSourcing\Serializer\Upcast\UpcasterChain;

final class DefaultEventSerializer implements EventSerializer
{
private EventRegistry $eventRegistry;
private EventHydrator $hydrator;
private Encoder $encoder;
private Upcaster $upcaster;

public function __construct(EventRegistry $eventRegistry, EventHydrator $hydrator, Encoder $encoder)
public function __construct(EventRegistry $eventRegistry, EventHydrator $hydrator, Encoder $encoder, Upcaster $upcaster)
{
$this->eventRegistry = $eventRegistry;
$this->hydrator = $hydrator;
$this->encoder = $encoder;
$this->upcaster = $upcaster;
}

public function serialize(object $event, array $options = []): SerializedEvent
Expand All @@ -38,10 +43,12 @@ public function serialize(object $event, array $options = []): SerializedEvent

public function deserialize(SerializedEvent $data, array $options = []): object
{
$class = $this->eventRegistry->eventClass($data->name);
$payload = $this->encoder->decode($data->payload, $options);
$upcast = ($this->upcaster)(new Upcast($data->name, $payload));

return $this->hydrator->hydrate($class, $payload);
$class = $this->eventRegistry->eventClass($upcast->eventName);

return $this->hydrator->hydrate($class, $upcast->payload);
}

/**
Expand All @@ -52,7 +59,8 @@ public static function createFromPaths(array $paths): static
return new self(
(new AttributeEventRegistryFactory())->create($paths),
new MetadataEventHydrator(new AttributeEventMetadataFactory()),
new JsonEncoder()
new JsonEncoder(),
new UpcasterChain([])
);
}
}
18 changes: 18 additions & 0 deletions src/Serializer/Upcast/Upcast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Serializer\Upcast;

/**
* @psalm-immutable
*/
final class Upcast
{
/**
* @param array<string, mixed> $payload
*/
public function __construct(public string $eventName, public array $payload)
{
}
}
10 changes: 10 additions & 0 deletions src/Serializer/Upcast/Upcaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Serializer\Upcast;

interface Upcaster
{
public function __invoke(Upcast $upcast): Upcast;
}
24 changes: 24 additions & 0 deletions src/Serializer/Upcast/UpcasterChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Serializer\Upcast;

class UpcasterChain implements Upcaster
{
/**
* @param array<Upcaster> $upcaster
*/
public function __construct(private array $upcaster)
{
}

public function __invoke(Upcast $upcast): Upcast
{
foreach ($this->upcaster as $upcaster) {
$upcast = $upcaster($upcast);
}

return $upcast;
}
}
42 changes: 42 additions & 0 deletions tests/Unit/Serializer/DefaultEventSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

namespace Patchlevel\EventSourcing\Tests\Unit\Serializer;

use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventRegistryFactory;
use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer;
use Patchlevel\EventSourcing\Serializer\Encoder\JsonEncoder;
use Patchlevel\EventSourcing\Serializer\Hydrator\MetadataEventHydrator;
use Patchlevel\EventSourcing\Serializer\SerializedEvent;
use Patchlevel\EventSourcing\Serializer\Upcast\Upcast;
use Patchlevel\EventSourcing\Serializer\Upcast\Upcaster;
use Patchlevel\EventSourcing\Serializer\Upcast\UpcasterChain;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
Expand Down Expand Up @@ -49,4 +56,39 @@ public function testDeserialize(): void

self::assertEquals($expected, $event);
}

public function testSerializeWithUpcasting(): void
{
$upcaster = new class implements Upcaster {
public function __invoke(Upcast $upcast): Upcast
{
if ($upcast->eventName !== 'profile_created_old') {
return $upcast;
}

return new Upcast('profile_created', $upcast->payload + ['email' => 'info@patchlevel.de']);
}
};

$serializer = new DefaultEventSerializer(
(new AttributeEventRegistryFactory())->create([__DIR__ . '/../Fixture']),
new MetadataEventHydrator(new AttributeEventMetadataFactory()),
new JsonEncoder(),
new UpcasterChain([$upcaster])
);

$expected = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('info@patchlevel.de')
);

$event = $serializer->deserialize(
new SerializedEvent(
'profile_created_old',
'{"profileId":"1"}'
)
);

self::assertEquals($expected, $event);
}
}
55 changes: 55 additions & 0 deletions tests/Unit/Serializer/Upcaster/UpcasterChainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Serializer\Upcaster;

use Patchlevel\EventSourcing\Serializer\Upcast\Upcast;
use Patchlevel\EventSourcing\Serializer\Upcast\Upcaster;
use Patchlevel\EventSourcing\Serializer\Upcast\UpcasterChain;
use PHPUnit\Framework\TestCase;

final class UpcasterChainTest extends TestCase
{
public function testChainSuccessful(): void
{
$upcasterOne = new class implements Upcaster {
public int $counter = 0;

public function __invoke(Upcast $upcast): Upcast
{
$this->counter++;

return new Upcast('profile_1', $upcast->payload);
}
};

$upcasterTwo = new class implements Upcaster {
public int $counter = 0;

public function __invoke(Upcast $upcast): Upcast
{
$this->counter++;

return new Upcast('profile_2', $upcast->payload + ['foo' => 'bar']);
}
};

$inputPayload = ['bar' => 'baz'];
$inputEventName = 'profile';

$chain = new UpcasterChain([$upcasterOne, $upcasterTwo]);
$upcast = ($chain)(new Upcast($inputEventName, $inputPayload));

self::assertSame(1, $upcasterOne->counter);
self::assertSame(1, $upcasterTwo->counter);
self::assertSame('profile_2', $upcast->eventName);
self::assertSame(
[
'bar' => 'baz',
'foo' => 'bar',
],
$upcast->payload
);
}
}