From 0351c7b04ae7ade87ee488b45ec5359706c20694 Mon Sep 17 00:00:00 2001 From: theUniC Date: Sun, 4 Sep 2022 17:41:12 +0200 Subject: [PATCH] 06. Introduction of CommandBus --- config/packages/messenger.yaml | 6 +++++ .../Controller/PostFollowersController.php | 6 ++--- src/Cheeper/Application/CommandBus.php | 12 ++++++++++ .../Follow/FollowCommandHandler.php | 2 ++ .../SymfonyMessengerCommandBus.php | 22 +++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/Cheeper/Application/CommandBus.php create mode 100644 src/Cheeper/Infrastructure/Application/SymfonyMessengerCommandBus.php diff --git a/config/packages/messenger.yaml b/config/packages/messenger.yaml index fe1e6a8..a45c1c5 100644 --- a/config/packages/messenger.yaml +++ b/config/packages/messenger.yaml @@ -6,6 +6,12 @@ framework: buses: default: default_middleware: allow_no_handlers + command_bus: + middleware: + - validation + - doctrine_ping_connection + - doctrine_close_connection + - doctrine_transaction transports: # https://symfony.com/doc/current/messenger.html#transport-configuration diff --git a/src/App/Controller/PostFollowersController.php b/src/App/Controller/PostFollowersController.php index 1b5c344..c4d4923 100644 --- a/src/App/Controller/PostFollowersController.php +++ b/src/App/Controller/PostFollowersController.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace App\Controller; +use Cheeper\Application\CommandBus; use Cheeper\Application\Follow\FollowCommand; -use Cheeper\Application\Follow\FollowCommandHandler; use Cheeper\DomainModel\Author\AuthorDoesNotExist; use OpenApi\Attributes as OA; use Psl\Json; @@ -20,8 +20,8 @@ use Symfony\Component\Validator\Validator\ValidatorInterface; final class PostFollowersController extends AbstractController { public function __construct( - private readonly FollowCommandHandler $followCommandHandler, private readonly ValidatorInterface $validator, + private readonly CommandBus $commandBus, ) { } @@ -95,7 +95,7 @@ final class PostFollowersController extends AbstractController } try { - ($this->followCommandHandler)( + $this->commandBus->handle( new FollowCommand( $data['from_author_id'], $data['to_author_id'] diff --git a/src/Cheeper/Application/CommandBus.php b/src/Cheeper/Application/CommandBus.php new file mode 100644 index 0000000..e38b00f --- /dev/null +++ b/src/Cheeper/Application/CommandBus.php @@ -0,0 +1,12 @@ +commandBus->dispatch($command); + } +} \ No newline at end of file