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.
1
namespace Ecotone\Modelling;
2
3
interface QueryBus
4
{
5
public function send(object $query, array $metadata = []) : mixed;
6
7
public function sendWithRouting(string $routingKey, mixed $query, string $queryMediaType = MediaType::APPLICATION_X_PHP, array $metadata = []) : mixed;
8
}
Copied!

Send method

Query is routed to the Handler by class type.
Symfony / Laravel
Lite
1
class TicketController
2
{
3
private QueryBus $queryBus;
4
5
public function __construct(QueryBus $queryBus)
6
{
7
$this->queryBus = $queryBus;
8
}
9
10
public function getTicketStatusAction(Request $request) : Response
11
{
12
$result = $this->queryBus->send(
13
new GetTicketStatusQuery($request->get("ticketId"))
14
);
15
}
16
}
Copied!
1
$queryBus = $messagingSystem->getGatewayByName(QueryBus::class);
2
3
$result = $queryBus->send(new GetTicketStatusQuery($ticketId));
Copied!
Handler
1
class GetTicketStatusQueryHandler
2
{
3
#[QueryHandler]
4
public function getTicketStatus(GetTicketStatusQuery $query)
5
{
6
// handle retrieving ticket status
7
}
8
}
Copied!

Sending With Metadata

Does allow for passing extra meta information, that can be used on targeted Query Handler.
Symfony / Laravel
Lite
1
class TicketController
2
{
3
private QueryBus $queryBus;
4
5
public function __construct(QueryBus $queryBus)
6
{
7
$this->queryBus = $queryBus;
8
}
9
10
public function getTicketStatusAction(Request $request, Security $security) : Response
11
{
12
$result = $this->queryBus->sendWithMetadata(
13
new GetTicketStatusQuery($request->get("ticketId")),
14
["executorUsername" => $security->getUser()->getUsername()]
15
);
16
}
17
}
Copied!
1
$queryBus = $messagingSystem->getGatewayByName(QueryBus::class);
2
3
$result = $queryBus->sendWithMetadata(
4
new GetTicketStatusQuery($ticketId),
5
["executorUsername" => $executorUsername]
6
);
Copied!
Handler
1
class GetTicketStatusQueryHandler
2
{
3
#[QueryHandler]
4
public function getTicketStatus(GetTicketStatusQuery $query, array $metadata)
5
{
6
$ticket = ; // get Ticket using query
7
8
if ($metadata["executorUsername"] !== $ticket->getOwner()) {
9
throw new \InvalidArgumentException("Insufficient permissions")
10
}
11
// handle retrieving ticket status
12
}
13
}
Copied!

Send With Routing

Is used with Query Handlersrouted by name and converted using Converter if needed.
Symfony / Laravel
Lite
1
class TicketController
2
{
3
private QueryBus $queryBus;
4
5
public function __construct(QueryBus $queryBus)
6
{
7
$this->queryBus = $queryBus;
8
}
9
10
public function getTicketStatusAction(Request $request) : Response
11
{
12
$result = $queryBus->convertAndSend(
13
"getTicketStatus",
14
"application/json",
15
'{"ticketId": 123}'
16
);
17
}
18
}
Copied!
1
$queryBus = $messagingSystem->getGatewayByName(QueryBus::class);
2
3
$result = $queryBus->convertAndSend(
4
"getTicketStatus",
5
"application/json",
6
'{"ticketId": 123}'
7
);
Copied!
Handler
1
class GetTicketStatusQueryHandler
2
{
3
#[QueryHandler("getTicketStatus")]
4
public function getTicketStatus(GetTicketStatusQuery $query)
5
{
6
// handle retrieving ticket status
7
}
8
}
Copied!
Last modified 8mo ago