Dispatching Commands
Commands PHP
Previous pages provide the background on how to handle command messages in your application. The dispatching process is the starting point for command message.

Command Bus

Command Bus is special type of Messaging Gateway.
namespace Ecotone\Modelling;
interface CommandBus
{
// 1
public function send(object $command, array $metadata = []) : mixed;
// 2
public function sendWithRouting(string $routingKey, mixed $command, string $commandMediaType = MediaType::APPLICATION_X_PHP, array $metadata = []) : mixed;
}

Send method

Command is routed to the Handler by class type.
Symfony / Laravel
Lite
// Command Bus will be auto registered in Depedency Container.
class TicketController
{
private CommandBus $commandBus;
public function __construct(CommandBus $commandBus)
{
$this->commandBus = $commandBus;
}
public function closeTicketAction(Request $request) : Response
{
$this->commandBus->send(
new CloseTicketCommand($request->get("ticketId"))
);
}
}
$commandBus = $messagingSystem->getCommandBus();
$commandBus->send(new CloseTicketCommand($ticketId));
Command Handler
class CloseTicketCommandHandler
{
#[CommandHandler]
public function closeTicket(CloseTicketCommand $command)
{
// handle closing ticket
}
}

Sending To Aggregate

Ecotone knows which instance of Ticket it should call by mapping the property.
Command
class CloseTicketCommand
{
private $ticketId;
}
Command Handler
#[Aggregate]
class Ticket
{
#[AggregateIdentifier]
private $ticketId;
#[CommandHandler]
public function closeTicket(CloseTicketCommand $command)
{
// handle closing ticket
}
}
If you want to know more about identifier correlation, you can read here.

Send With Metadata

Does allow for passing extra meta information, that can be used on targeted Command Handler.
Symfony / Laravel
Lite
class TicketController
{
private CommandBus $commandBus;
public function __construct(CommandBus $commandBus)
{
$this->commandBus = $commandBus;
}
public function closeTicketAction(Request $request, Security $security) : Response
{
$this->commandBus->sendWithMetadata(
new CloseTicketCommand($request->get("ticketId")),
["executorUsername" => $security->getUser()->getUsername()]
);
}
}
$commandBus = $messagingSystem->getGatewayByName(CommandBus::class);
$commandBus->sendWithMetadata(
new CloseTicketCommand($ticketId),
["executorUsername" => $executorUsername]
);
Command Handler
class CloseTicketCommandHandler
{
#[CommandHandler]
public function closeTicket(CloseTicketCommand $command, array $metadata)
{
$ticket = ; // get Ticket using command
if ($metadata["executorUsername"] !== $ticket->getOwner()) {
throw new \InvalidArgumentException("Insufficient permissions")
}
// handle closing ticket
}
}

Send With Routing

Is used with Command Handlers, routed by name and converted using Converter if needed.
Symfony / Laravel
Lite
class TicketController
{
private CommandBus $commandBus;
public function __construct(CommandBus $commandBus)
{
$this->commandBus = $commandBus;
}
public function closeTicketAction(Request $request) : Response
{
$commandBus->convertAndSend("closeTicket", "application/json", '{"ticketId": 123}');
}
}
$commandBus = $messagingSystem->getGatewayByName(CommandBus::class);
$commandBus->convertAndSend(
"closeTicket",
"application/json",
'{"ticketId": 123}'
);
Command Handler
class CloseTicketCommandHandler
{
#[CommandHandler("closeTicket")]
public function closeTicket(CloseTicketCommand $command)
{
// handle closing ticket
}
}

Sending To Aggregate

Ecotone knows which instance of Ticket it should call by mapping the property.
Command
class CloseTicketCommand
{
private $ticketId;
}
Command Handler
#[Aggregate]
class Ticket
{
#[AggregateIdentifier]
private $ticketId;
#[CommandHandler("closeTicket")]
public function closeTicket(CloseTicketCommand $command)
{
// handle closing ticket
}
}
You may also tell about the identifiers, when sending an command using meta data. Then you command does not need to include identifier.
public function controllerAction()
{
$this->commandBus->sendWithRouting("startPayment", metadata: ["aggregate.id" => 123])
}
If you want to know more about identifier correlation, you can read here.
Export as PDF
Copy link
On this page
Command Bus
Send method
Sending To Aggregate
Send With Metadata
Send With Routing
Sending To Aggregate