git checkout lesson-5
Ecotone
provide us with possibility to handle cross cutting concerns via Interceptors
.
Interceptor
as name suggest, intercepts the process of handling the message.
You may enrich the message, stop or modify usual processing cycle, call some shared functionality, add additional behavior to existing code without modifying the code itself. Administrators should be able to change the cost of a product
product.register
to product.changePrice
but we want to avoid code duplication, especially logic that may happen more often. Let's intercept our Command Handlers.
Annotation
called RequireAdministrator
in new namepace App\Infrastructure\RequireAdministrator
Before Interceptor.
Start by removing old UserService
and create new one in different namespace App\Infrastructure\RequireAdministrator
. Remember to mark return type as void
, we will see why it is so important soon.Before
- marks method as Interceptor
, so it can be be found by Ecotone.
Pointcut
- describes what should be intercepted. CLASS_NAME
- indicates what should be intercepted using specific Class Name
or Attribute Name
annotated at the level of method or classexpression||expression
- Indicating one expression or another e.g. Product\*||Order\*
Before Interceptor
that it should intercept all endpoints with annotation RequireAdministrator.
Now, whenever we will call our command handlers, they will be intercepted by UserService
.
You can try it out, by providing different userId
.Before
and After
interceptors are depending on the return type, to decide if they should modify Message or pass it through.
If return type is different than void, Message payload or headers can be enriched with data.
If return type is void then message will be passed through and the process of message flow can be interrupted by throwing exception only.userId
during calling the CommandBus
we will enrich Message with it before it will be handled by Command Handler
using Interceptor
.Interceptor
.changeHeaders
- Tells Ecotone
if this Interceptor modifies payload
or headers
. The default is payload
.
If changeHeaders=true
thenheaders
are picked and associative array must be returned. The returned value is merged with current headers.
If changeHeaders=false
then payload
is picked and current payload is replaced by returned value, the headers stays the same.
You may of course inject current payload and headers into the method if needed, as with usual endpoint.
precedence
- Tells Ecotone
in what order interceptors should be called. The lower the value is the quicker interceptor will be called. The order exists within interceptor type: before/around/after.
We want to call AddUserId Interceptor
before RequireAdministrator Interceptor
as it require userId
to exists, in order to verify.
AddUserIdService
has precedence of 0
as default, so UserService
must have at least 1
.Product
aggregate@AddUserId.
Before
or Around
you decide to break the flow, return null
. Null
indiciates, that there is no message and the current flow ends.
Null can not be returned in header changing interceptor, it does work only for payload changing interceptor.Around Interceptor
is closet to actual endpoint's method call. Thanks to that, it has access to Method Invocation.
This does allow for starting some procedure and ending after the invocation is done. sqlite
if you do not have extension installed, then you will need to install it first. Yet if you are using Quickstart's Docker
container, then you are ready to go.sqlite
database.
Before we do that, we need to remove our In Memory implementation class App\Domain\Product\InMemoryProductRepository
we will replace it with our new implementation.
We will create using new namespace for it App\Infrastructure\Persistence.
Besides we are going to use doctrine/dbal, as this is really helpful abstraction over the PDO.Connection
to sqlite database using dbal librarySerializer
is Gateway registered by Ecotone.
Serializer can handle serialization using Converters.
It this case it will know how to register Cost
class, as we already registered Converter for it.
Serializer give us access for conversion from PHP
type to specific Media Type or from specific Media Type to PHP
type. We will use it to easily serialize our Product
model into JSON
and store it in database.id
of the aggregate, the class
type and serialized data
in JSON
. Take a look at createSharedTableIfNeeded
if you want more details.PHP
JSON
registerProduct
, if one already exists. It will will insert or update if aggregate exists.Command Bus Gateway
with transaction. So whenever we call it, it will invoke our Command Handler within transaction.pointcut="Ecotone\Modelling\CommandBus"
CommandBus.
Command Bus.
In case of Command Bus it may seems not needed, but if we would intercept Aggregate, then it really useful as for example you may verify if executing user have access to it.
You may read more about interceptors in dedicated section.Ecotone
, we can build build up Authorization, Transactions, Delegate duplicated logic, Call some external service, Logging and Tracing before invoking endpoint, the amount of possibilities is endless.
In the next chapter, we will learn about scheduling and polling endpoints