Answer the question
In order to leave comments, you need to log in
How do I block the ability to return data in Messenger?
https://symfony.com/doc/current/components/messeng...
# Create buses
buses: # TODO - forbid return values for commands
messenger.bus.commands: ~
messenger.bus.queries: ~
* @param MessageBusInterface $commandBus
* @param MessageBusInterface $queryBus
Answer the question
In order to leave comments, you need to log in
You can try to define a separate interface for "commands":
<?php
namespace App;
interface CommandHanderInterface
{
public function __invoke(object $message): void;
}
<?php
namespace App;
class HandleMessageMiddleware implements MiddlewareInterface
{
private $messageHandlerResolver;
public function __construct(HandlerLocatorInterface $messageHandlerResolver)
{
$this->messageHandlerResolver = $messageHandlerResolver;
}
function handle($message, callable $next)
{
$handler = $this->messageHandlerResolver->resolve($message);
if (!$handler instanceof CommandHandlerInterface) {
throw new InvalidHandlerException('Handler must implement CommandHandlerInterface.');
}
$result = $handler($message);
$next($message);
return $result;
}
}
Аналогично сделать для "запросов".
class CommandMiddleware implements MiddlewareInterface
{
public function handle($message, callable $next)
{
if (null !== $next($message)) {
throw new ReturningValueForbiddenException($message);
}
}
}
class QueryMiddleware implements MiddlewareInterface
{
public function handle($message, callable $next)
{
if (null === $result = $next($message)) {
throw new ReturningValueRequiredException($message);
}
return $result;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question