Dispatching Events
Events PHP
Previous pages provide the background on how to handle event messages in your application. The dispatching process is the starting point for event message. You will mostly send events after successfully handling Command Message, examples will base on that assumption.
You can inject EventBus and send events wherever necessary. Ecotone tries not to impose any specific solutions.

Event Bus

Event Bus is special type of Messaging Gateway.
1
namespace Ecotone\Modelling;
2
3
interface EventBus
4
{
5
// 1
6
public function publish(object $event, array $metadata = []) : void;
7
// 2
8
public function publishWithRouting(string $routingKey, mixed $event = [], string $eventMediaType = MediaType::APPLICATION_X_PHP, array $metadata = []) : void;
9
}
Copied!

Publishing

Event is routed to the Handler by class type.
Command Handler
1
class CloseTicketCommandHandler
2
{
3
#[CommandHandler]
4
public function closeTicket(CloseTicketCommand $command, EventBus $eventBus) : void
5
{
6
$eventBus->publish(new TicketWasClosed($command->getTicketId()));
7
}
8
}
Copied!
Event Handler
1
class NotificationService
2
{
3
#[EventHandler]
4
public function closeTicket(TicketWasClosed $event) : void
5
{
6
// notify about closing the ticket
7
}
8
}
Copied!

Publishing with routing

Is used with Command Handlers,routed by name and converted using Converter if needed. Sending events by name instead of class type, may be found useful in integration with external application, when events are in different Media Type than PHP class.
Command Handler
1
class CloseTicketCommandHandler
2
{
3
#[CommandHandler]
4
public function closeTicket(CloseTicketCommand $command, EventBus $eventBus) : void
5
{
6
$eventBus->publishWithRouting(
7
"ticket.wasClosed",
8
'{"ticketId": 123}'
9
"application/json"
10
);
11
}
12
}
Copied!
Event Handler
1
class NotificationService
2
{
3
#[EventHandler("ticket.wasClosed")]
4
public function closeTicket(TicketWasClosed $event) : void
5
{
6
// notify about closing the ticket
7
}
8
}
Copied!
JSON will be automatically converted to specific class type hinted in method declaration of Event Handler. You could also use in here simple array if you have JSON to array Converter or a string, if you like to receive JSON string.

Publishing from Aggregate

Ecotone does provide possibility to automatically gather events from Aggregate and publish them using EventBus.

State-Stored Aggregate

To tell Ecotone, which method it should use for retrieving Event objects when using State-Stored Aggregate mark method containing events with annotation @AggregateEvents. After handling Command or Event on Aggregate events will be published.
1
#[AggregateEvents]
2
public function getRecordedEvents() : array
Copied!
If you do want to bother with implementation you can make use of trait WithAggregateEventIf you want to record event for publication just use recordmethod.
1
$this->recordThat(new OrderWasPlaced());
Copied!
Events will be automatically retrieved and published after handling current Command or Event.

Event Sourcing Aggregate

When using Event Sourcing Aggregate you do not need to do anything extra. Each method should return events after handling, those events will automatically published using Event Bus.
1
public function assignWorker(AssignWorkerCommand $command) : array
Copied!
Events published from aggregate, are publish by class name and routing, if event is named. #[NamedEvent("order_was_placed")] class OrderWasPlaced
Last modified 6mo ago