Command Events
This plugin lets you listen to some events emitted during command execution:
command.received
: Emitted when a command is received by the command buscommand.handled
: Emitted when a command is handled without errorscommand.failed
: Emitted when an error occurred during command execution
Setup is simple:
use League\Tactician\CommandBus;
use League\Tactician\CommandEvents\EventMiddleware;
use League\Tactician\CommandEvents\Event\CommandHandled;
$eventMiddleware = new EventMiddleware();
// type-hint is optional
$eventMiddleware->addListener(
'command.handled',
function (CommandHandled $event) {
// log the success
}
);
$commandBus = new CommandBus([$eventMiddleware]);
$commandBus->handle($command);
Optionally you can inject an event emitter into the middleware:
use League\Event\Emitter;
use League\Tactician\CommandEvents\EventMiddleware;
$emitter = new Emitter();
$eventMiddleware = new EventMiddleware($emitter);
// other possible solution
// $eventMiddleware->setEmitter($emitter);
You can also catch an error and prevent it from causing the application to fail:
use League\Tactician\CommandEvents\Event\CommandFailed;
$eventMiddleware->addListener(
'command.failed',
function (CommandFailed $event) {
// log the failure
$event->catchException(); // without calling this the exception will be thrown
}
);
// something bad happens, exception thrown
$commandBus->handle($command);