@param AbstractCommand           $command
@param CommandHandlerInterface[] $commandHandlers

@return CommandDispatcherException
424137
public static function tooManyCommandHandler(AbstractCommand $command, array $commandHandlers)
    {
        $handlerClassNames = array_map(
            function (CommandHandlerInterface $commandHandler) {
                return get_class($commandHandler);
            },
            $commandHandlers
        );

        return new self(
            sprintf(
                'There are too many command handler for the command type %s. Candidates are %s.',
                get_class($command),
                implode(', ', $handlerClassNames)
            )
        );
    }
1236718
protected function handle(CommandInterface $command)
	{
		if($command instanceof SelfHandlingCommandInterface)
		{
			return $command->handle();
		}

		$handler = $this->resolveCommandHandler($command);

		return $this->executeCommandHandler($handler, $command);
	}
1237859
public static function commandHandlerAlreadyDefinedForCommand(
        $commandClassName,
        CommandHandlerInterface $previous,
        CommandHandlerInterface $current
    ) {
        $message = 'Trying to add handler %3$s handling command %1$s which is already handled by %2$s.';
        if (get_class($previous) === get_class($current)) {
            $message = 'Trying to add multiple handling method for the command %1$s in the handler %2$s';
        }

        return new self(sprintf($message, $commandClassName, get_class($previous), get_class($current)));
    }
400420
public function executeCommand(CommandInterface $command)
    {
        $this->initialize();

        if ($this->state->isCAS()) {
            return $this->client->executeCommand($command);
        }

        $response = $this->client->getConnection()->executeCommand($command);

        if ($response instanceof StatusResponse && $response == 'QUEUED') {
            $this->commands->enqueue($command);
        } elseif ($response instanceof ErrorResponseInterface) {
            throw new AbortedMultiExecException($this, $response->getMessage());
        } else {
            $this->onProtocolError('The server did not return a +QUEUED status response.');
        }

        return $this;
    }
428694
public function handle($command)
    {
        if (!is_object($command)) {
            throw InvalidCommandException::forUnknownValue($command);
        }

        $middlewareChain = $this->middlewareChain;
        return $middlewareChain($command);
    }
331204
public static function forCommand(Command $command, CommandHandler $handler, string $message = ''): self
    {
        return new static(
            sprintf(
                'The command [%s] could not be handled by [%s].  %s',
                $command::schema()->getId()->toString(),
                get_class($handler),
                $message
            ),
            Code::INTERNAL
        );
    }
1264962
public function dispatch(CommandInterface $command)
    {
        $this->commandStack[] = $command;

        if ($this->executing) {
            return array();
        }

        $first          = true;
        $aggregateRoots = array();

        while ($command = array_shift($this->commandStack)) {
            $aggregateRoots[] = $this->invokeHandler($command, $first);
            $first            = false;
        }

        return $aggregateRoots;
    }
584367
protected function checkCommandType(CommandInterface $command)
    {
        $expectedCommandClass = $this->getCommandClass();
        if (!is_a($command, $expectedCommandClass, true)) {
            $actualCommandClass      = get_class($command);
            $handlerClass            = get_called_class();
            $invalidCommandException = new InvalidCommandException(
                "Command handler '$handlerClass' should only be registered to handle '$expectedCommandClass' commands"
            );
            $this->logError(
                "{$handlerClass} cannot handle commands of type {$actualCommandClass}",
                $invalidCommandException
            );
            throw $invalidCommandException;
        }
    }
606272
private function wrapCommandWithMessageEnvelope($command)
    {
        if ($command instanceof Command) {
            return $command;
        }

        if ($command instanceof Serializable) {
            return new GenericCommand(MessageIdentifier::generate(), $command);
        }

        throw new \InvalidArgumentException('Unsupported data type.');
    }
933426
protected function execute(CommandInterface $command)
    {
        $runner = $this->getCommandRunner();

        $this->lastReturnValue = $runner->run($command);

        $this->checkReturnValue($runner);
    }
263931
public function handle(CommandInterface $command)
    {
        $object = $command->getParameter('data');

        if ($command->hasParameter('class')) {
            $class = $command->getParameter('class');
            if (!($object instanceof $class)) {
                return CommandResult::error('invalid_instance');
            }
        }

        try {
            $this->objectManager->flush();
        } catch (ForeignKeyConstraintViolationException $e) {
            return CommandResult::error('constraint_violation');
        }
    }
1140423
public function addCommandHandler(CommandHandlerInterface $commandHandler, string $payloadName): CommandDispatcher
    {
        $this->commandHandlers[$payloadName] = $commandHandler;

        return $this;
    }
339054
public function command($command, $method) {

        self::$commands[] = $command;
        self::$callbacks[$command] = $method;
    }
1113628
public function execute($command = null, array $args = array(), array $options = array())
    {
        $command = $command ?: $this->defaultCommand;
        if (!isset($this->commands[$command])) {
            throw new ConsoleException("Command '$command' does not exist");
        }
        
        $callback = $this->commands[$command];
        if (is_callable($callback)) {
            $params = array($args, $options);
            if (is_string($callback)) {
                if (strpos($callback, '::') !== false) {
                    list($classname, $methodname) = explode('::', $callback);
                    $reflection = new ReflectionMethod($classname, $methodname);
                } else {
                    $reflection = new ReflectionFunction($callback);
                }
                $params = Utils::computeFuncParams($reflection, $args, $options);
            }
            $params[] = $this;
            return call_user_func_array($callback, $params);
        }

        $method = new ReflectionMethod($callback, 'execute');
        $params = Utils::computeFuncParams($method, $args, $options);
        return $method->invokeArgs(new $callback($this), $params);
    }
1077184
public function dispatchNow($command, $handler = null)
    {
        if (! is_null($handler) || ! is_null($handler = $this->getCommandHandler($command))) {
            $callback = function ($command) use ($handler)
            {
                return $handler->handle($command);
            };
        }

        // The command is self handling.
        else {
            $callback = function ($command)
            {
                return $this->container->call(array($command, 'handle'));
            };
        }

        $pipeline = new Pipeline($this->container, $this->pipes);

        return $pipeline->handle($command, $callback);
    }
1026245
public function callHandler(array $command, $method, $args = array())
    {
        try {
            $handlers = $this->getHandlers();
            return Handler::call($handlers, $command['command_id'], $method, $args);
        } catch (Exception $ex) {
            return $ex->getMessage();
        }
    }
1122396
public function dispatch($command): void
    {
        $this->commandQueue[] = $command;

        if (! $this->isDispatching) {
            $this->isDispatching = true;

            $actionEventEmitter = $this->events;

            try {
                while ($command = \array_shift($this->commandQueue)) {
                    $actionEvent = $actionEventEmitter->getNewActionEvent(
                        self::EVENT_DISPATCH,
                        $this,
                        [
                            self::EVENT_PARAM_MESSAGE => $command,
                        ]
                    );

                    try {
                        $actionEventEmitter->dispatch($actionEvent);

                        if (! $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLED)) {
                            throw new RuntimeException(\sprintf('Command %s was not handled', $this->getMessageName($command)));
                        }
                    } catch (\Throwable $exception) {
                        $actionEvent->setParam(self::EVENT_PARAM_EXCEPTION, $exception);
                    } finally {
                        $actionEvent->stopPropagation(false);
                        $this->triggerFinalize($actionEvent);
                    }
                }
                $this->isDispatching = false;
            } catch (\Throwable $e) {
                $this->isDispatching = false;
                throw CommandDispatchException::wrap($e, $this->commandQueue);
            }
        }
    }