Dispatching Queries

Queries PHP

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

Query Bus

Query Bus is special type of Messaging Gateway.

namespace Ecotone\Modelling;
interface QueryBus
{
public function send(object $query, array $metadata = []) : mixed;
public function sendWithRouting(string $routingKey, mixed $query, string $queryMediaType = MediaType::APPLICATION_X_PHP, array $metadata = []) : mixed;
}

Send method

Query is routed to the Handler by class type.

Symfony / Laravel
Lite
Symfony / Laravel
class TicketController
{
private QueryBus $queryBus;
public function __construct(QueryBus $queryBus)
{
$this->queryBus = $queryBus;
}
public function getTicketStatusAction(Request $request) : Response
{
$result = $this->queryBus->send(
new GetTicketStatusQuery($request->get("ticketId"))
);
}
}
Lite
$queryBus = $messagingSystem->getGatewayByName(QueryBus::class);
$result = $queryBus->send(new GetTicketStatusQuery($ticketId));
Handler
Handler
class GetTicketStatusQueryHandler
{
#[QueryHandler]
public function getTicketStatus(GetTicketStatusQuery $query)
{
// handle retrieving ticket status
}
}

Sending With Metadata

Does allow for passing extra meta information, that can be used on targeted Query Handler.

Symfony / Laravel
Lite
Symfony / Laravel
class TicketController
{
private QueryBus $queryBus;
public function __construct(QueryBus $queryBus)
{
$this->queryBus = $queryBus;
}
public function getTicketStatusAction(Request $request, Security $security) : Response
{
$result = $this->queryBus->sendWithMetadata(
new GetTicketStatusQuery($request->get("ticketId")),
["executorUsername" => $security->getUser()->getUsername()]
);
}
}
Lite
$queryBus = $messagingSystem->getGatewayByName(QueryBus::class);
$result = $queryBus->sendWithMetadata(
new GetTicketStatusQuery($ticketId),
["executorUsername" => $executorUsername]
);
Handler
Handler
class GetTicketStatusQueryHandler
{
#[QueryHandler]
public function getTicketStatus(GetTicketStatusQuery $query, array $metadata)
{
$ticket = ; // get Ticket using query
if ($metadata["executorUsername"] !== $ticket->getOwner()) {
throw new \InvalidArgumentException("Insufficient permissions")
}
// handle retrieving ticket status
}
}

Send With Routing

Is used with Query Handlersrouted by name and converted using Converter if needed.

Symfony / Laravel
Lite
Symfony / Laravel
class TicketController
{
private QueryBus $queryBus;
public function __construct(QueryBus $queryBus)
{
$this->queryBus = $queryBus;
}
public function getTicketStatusAction(Request $request) : Response
{
$result = $queryBus->convertAndSend(
"getTicketStatus",
"application/json",
'{"ticketId": 123}'
);
}
}
Lite
$queryBus = $messagingSystem->getGatewayByName(QueryBus::class);
$result = $queryBus->convertAndSend(
"getTicketStatus",
"application/json",
'{"ticketId": 123}'
);
Handler
Handler
class GetTicketStatusQueryHandler
{
#[QueryHandler("getTicketStatus")]
public function getTicketStatus(GetTicketStatusQuery $query)
{
// handle retrieving ticket status
}
}