-
Notifications
You must be signed in to change notification settings - Fork 0
Query Bus
Pau F. Grau edited this page Jan 10, 2021
·
2 revisions
We use the query bus for all use cases need be executed without transaction
We use the follow open source packages:
If you want add a new query you need do the follow steps:
- Add your query handler on QueryBusFactory::map
- Configure your command handler on QueryBusProvider
Example of use:
...
$query = new GetShoutsByAuthorQuery($authorId, (int) $limit);
$response = $this->queryBus->ask($query)
...
...
public function __invoke(GetShoutsByAuthorQuery $query): GetShoutsByAuthorQueryResponse
{
if ($query->limit() <= 0 || $query->limit() > 10) {
$msg = \sprintf('Limit must be between 1 and 10. Got(%d)', $query->limit());
throw new BadRequestException($msg);
}
$keyCache = ShoutsByAuthorRequestedV1::EVENT_NAME.$query->authorId().$query->limit();
if (! $shouts = $this->cacheRepository->get($keyCache)) {
$author = $this->authorRepository->getOne($query->authorId());
$shouts = $this->quoteRepository->findByAuthor($author->id(), $query->limit());
$shouts = ShoutCollection::fromQuotes(...$shouts);
$this->domainEventPublisher->publish(
new ShoutsByAuthorRequestedV1($this->serializer->serialize($shouts), $author->id(), $query->limit())
);
}
$response = new GetShoutsByAuthorQueryResponse($shouts);
return $response;
}
...