streak
streak copied to clipboard
Create Listener\Corellatable interface and Corellating trait
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);
}
}