06. Introduction of CommandBus

This commit is contained in:
theUniC
2022-09-04 17:41:12 +02:00
parent 7ed3deb2b5
commit 0351c7b04a
5 changed files with 45 additions and 3 deletions

View File

@@ -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

View File

@@ -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']

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Cheeper\Application;
/** @template T of Command */
interface CommandBus
{
/** @psalm-param T $command */
public function handle(Command $command): void;
}

View File

@@ -10,7 +10,9 @@ use Cheeper\DomainModel\Author\AuthorDoesNotExist;
use Cheeper\DomainModel\Author\AuthorId;
use Cheeper\DomainModel\Author\AuthorRepository;
use Cheeper\DomainModel\Follow\FollowRepository;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
final class FollowCommandHandler
{
public function __construct(

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Cheeper\Infrastructure\Application;
use Cheeper\Application\Command;
use Cheeper\Application\CommandBus;
use Symfony\Component\Messenger\MessageBusInterface;
final class SymfonyMessengerCommandBus implements CommandBus
{
public function __construct(
private readonly MessageBusInterface $commandBus
) {
}
public function handle(Command $command): void
{
$this->commandBus->dispatch($command);
}
}