streak icon indicating copy to clipboard operation
streak copied to clipboard

Create Listener\Corellatable interface and Corellating trait

Open alanbem opened this issue 8 months ago • 0 comments

interface Correlatable
{
     /**
      * @throws InvalidEventGiven
      */
     public function correlate(Event\Envelope $envelope): Listener\Id;
}
class OrderSaga implements Listener, Listener\Correlatable
{
    use Listener\Correlating;

	public static function correlateByOrderPlaced(OrderPlaced $event): OrderSaga\Id
    {
          return new OrderSaga\Id($event->orderId);
    }

	public static function correlateByInventoryReserved(InventoryReserved $event): OrderSaga\Id
    {
          return new OrderSaga\Id($event->orderId);
    }

    // etc
}
class Factory implements Listener\Factory
{
    public function create(OrderSaga\Id $id): Listener
    {
        return new OrderSaga($id, $this->commandBus);
    }

    /**
     * Somehow automate or make it not part of the factory but part of the Streak infrastructure.
     */ 
    public function createFor(Event\Envelope $envelope): Listener
    {
         return OrderSaga::correlate($envelope);
    }
}

alanbem avatar Apr 12 '25 02:04 alanbem