andreo/eventsauce-upcasting

Extended upcasting components for EventSauce.

3.0 2023-02-02 19:05 UTC

This package is auto-updated.

Last update: 2024-11-30 02:08:07 UTC


README

Extended upcasting components for EventSauce

About Upcasting

Installation

composer require andreo/eventsauce-upcasting

Previous versions doc

Requirements

  • PHP >=8.2

Message upcaster

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()); 
    }
}

Multiple message upcasters

use Andreo\EventSauce\Upcasting\MessageUpcasterChain;

new MessageUpcasterChain(
    new SomeUpcaster(),
    new AnotherUpcaster(),
)
    

Upcaster as MessageSerializer

For use in MessageRepository

use Andreo\EventSauce\Upcasting\UpcastingMessageObjectSerializer;

new UpcastingMessageObjectSerializer(
    messageSerializer: $messageSerializer, // default EventSauce\EventSourcing\Serialization\MessageSerializer
    upcaster: new MessageUpcasterChain(new SomeUpcaster())
)
    

Event guessing

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()); 
    }
}

Handling events of aggregate

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