B
B
BonBon Slick2018-10-06 16:54:55
symfony
BonBon Slick, 2018-10-06 16:54:55

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: ~

https://symfony.com/doc/current/messenger.html
Now how to separate them, because commands should be void and query should always return something.
* @param MessageBusInterface      $commandBus
   * @param MessageBusInterface      $queryBus

How to share responsibility?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
voronkovich, 2018-10-07
@BonBonSlick

You can try to define a separate interface for "commands":

<?php

namespace App;

interface CommandHanderInterface
{
    public function __invoke(object $message): void;
}

And create your own implementation of HandleMessageMiddleware :
<?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;
    }
}

Аналогично сделать для "запросов".

UPD. Perhaps it's better to just make two middlewares and "hook" them to the corresponding tires:
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 question

Ask a Question

731 491 924 answers to any question