Skip to content

Query Bus

Pau F. Grau edited this page Jan 10, 2021 · 2 revisions

🎯 Query Bus

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:

Example of use:

Controller:

     ...
     
     $query = new GetShoutsByAuthorQuery($authorId, (int) $limit);

     $response = $this->queryBus->ask($query)

     ...

Query Handler

    ...
    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;
    }
    ...
Clone this wiki locally