Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cqrs Laravel Package

digital-craftsman/cqrs

View on GitHub
Deep Wiki
Context7

Handler examples

Interfaces

The interfaces are simple marker interfaces.

/** [@method](https://github.com/method) void __invoke(Command $command) */
interface CommandHandler
{
}
/** [@method](https://github.com/method) void __invoke(Query $query) */
interface QueryHandler
{
}

See position in process

Command handler

A command handler implementation to create a new user account might look like this:

final class CreateUserAccountCommandHandler implements CommandHandler
{
    public function __construct(
        private readonly PasswordHasherFactoryInterface $passwordHasherFactory,
        private readonly EntityManagerInterface $entityManager,
        private readonly UserRepository $userRepository,
    ) {
    }

    public function __invoke(CreateUserAccountCommand $command): void
    {
      $this->requestingUserMustBeAdmin($command);

      $this->noUserWithTheSameEmailAddressMustExist($command);

      $this->createNewUser($command);

      $this->sendUserWasCreatedInAppNotificationsToAllAdminUsersExceptRequestingUser($command);

      $this->sendUserWasCreatedEmailNotificationsToAllAdminUsersExceptRequestingUser($command);
    }

    ...

}

Query handler

The query handler always returns a value (if there is no exception). This return value can be anything from an object, array or even a callable. When it returns data, it must not return entities, but always custom read models instead. This is an example where the query handler would return a user read model.

final readonly class GetUserQueryHandler implements QueryHandler
{
    public function __construct(
        private UserRepository $userRepository,
    ) {
    }

    public function __invoke(GetUserQuery $query): ReadModel\User
    {
        $this->requestingUserMustBeAdmin($query);

        return $this->getTargetUser($query);
    }

    ...

    private function getTargetUser(GetUserQuery $query): ReadModel\User
    {
        $targetUser = $this->userRepository->findOneById($query->targetUserId);
        if ($targetUser === null) {
            throw new TargetUserNotFound();
        }

        return new ReadModel\User(
            $targetUser->userId,
            $targetUser->name,
            $targetUser->emailAddress,
        );
    }
}

Whatever is returned is not send to the client but rather transformed to a response object through the configured response constructor.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
adachsoft/php-code-style