06. Introduction of CommandBus
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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']
|
||||
|
||||
12
src/Cheeper/Application/CommandBus.php
Normal file
12
src/Cheeper/Application/CommandBus.php
Normal 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;
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user