Extended upcasting components for EventSauce
composer require andreo/eventsauce-upcasting
- PHP >=8.2
use Andreo\EventSauce\Upcasting\MessageUpcaster\MessageUpcaster;
use EventSauce\EventSourcing\Message;
final class FooUpcaster implements MessageUpcaster
{
public function upcast(Message $message): Message
{
$event = $message->payload();
if (!$event instanceof FooEvent)) {
return $message;
}
return new Message(new FooEventV2());
}
}
use Andreo\EventSauce\Upcasting\MessageUpcasterChain;
new MessageUpcasterChain(
new SomeUpcaster(),
new AnotherUpcaster(),
)
For use in MessageRepository
use Andreo\EventSauce\Upcasting\UpcastingMessageObjectSerializer;
new UpcastingMessageObjectSerializer(
messageSerializer: $messageSerializer, // default EventSauce\EventSourcing\Serialization\MessageSerializer
upcaster: new MessageUpcasterChain(new SomeUpcaster())
)
use EventSauce\EventSourcing\Message;
use Andreo\EventSauce\Upcasting\MessageUpcaster\MessageUpcaster;
use Andreo\EventSauce\Upcasting\MessageUpcaster\Event;
final class FooUpcaster implements MessageUpcaster
{
#[Event(event: FooEvent::class)]
public function upcast(Message $message): Message
{
$event = $message->payload();
assert($event instanceof FooEvent);
return new Message(new FooEventV2());
}
}
By default, EventSauce applies the events based on method name convention apply{EventClassName}.
So you need to rename the method in the aggregate
use EventSauce\EventSourcing\AggregateRoot;
use EventSauce\EventSourcing\AggregateRootBehaviour;
final class FooAggregate implements AggregateRoot
{
use AggregateRootBehaviour;
// before applyFooEvent
public function applyFooEventV2(FooEventV2 $event): void
{
}
}
You can skip this by using component