Event Handling
Event CQRS PHP
Be sure to read CQRS Introduction before diving in this chapter.
Handling Events
External Event Handlers
are Services available in your dependency container, which are defined to handle Events
.
Events are Plain Old PHP Objects:
The difference between Events and Command is in intention. Commands are meant to trigger an given action and events are information that given action was performed successfully.
EventBus
is available in your Dependency Container by default, just like Command and Query buses. You may use Ecotone's feature to inject it directly into your Command Handler's method.
Just like EventBus is injected directly into your Command Handler, you may inject any other Service. This way you may make is clear what object your Command Handler needs in order to perform his action.
Multiple Subscriptions
Unlike Command Handlers which points to specific Command Handler, Event Handlers can have multiple subscribing Event Handlers.
Each Event Handler can be defined as Asynchronous. If multiple Event Handlers are marked for asynchronous processing, each of them is handled in isolation. This ensures that in case of failure, we can safely retry, as only failed Event Handler will be performed again.
Subscribe to Interface or Abstract Class
If your Event Handler is interested in all Events around specific business concept, you may subscribe to Interface or Abstract Class.
And then instead of subscribing to TicketWasCreated
or TicketWasCancelled
, we will subscribe to TicketEvent
.
Subscribing by Union Classes
We can also subscribe to different Events using union type hint. This way we can ensure that only given set of events will be delivered to our Event Handler.
Subscribing to All Events
We may subscribe to all Events published within the application. To do it we type hint for generic object
.
Subscribing to Events by Routing
Events can also be subscribed by Routing.
And then Event is published with routing key
Ecotone is using message routing for cross application communication. This way applications can stay decoupled from each other, as there is no need to share the classes between them.
Sending Events with Metadata
Just like with Command Bus
, we may pass metadata to the Event Bus
:
If you make your Event Handler Asynchronous, Ecotone will ensure your metadata will be serialized and deserialized correctly.
Metadata Propagation
By default Ecotone will ensure that your Metadata is propagated. This way you can simplify your code by avoiding passing around Headers and access them only in places where it matters for your business logic.
To better understand that, let's consider example in which we pass the metadata to the Command.
However in order to perform closing ticket logic, information about the executorId
is not needed, so we don't access that.
However Ecotone will ensure that your metadata is propagated from Handler to Handler. This means that the context is preserved and you will be able to access executorId in your Event Handler.
Last updated