/** * @param Command $command * @param CommandCallback $callback * * @throws \Exception */ public function dispatch(Command $command, CommandCallback $callback = null) { $handler = $this->handlerRegistry->findCommandHandlerFor($command); $this->unitOfWork->start(); try { $result = $handler->handle($command); if (null !== $callback) { $callback->onSuccess($result); } } catch (\Exception $exception) { $this->unitOfWork->rollback($exception); if (null !== $callback) { $callback->onFailure($exception); } // Todo: remove this ? throw $exception; } $this->unitOfWork->commit(); }
/** * @param Command $command * @param CommandCallback $callback * * @throws \Exception */ public function dispatch(Command $command, CommandCallback $callback = null) { $command = $this->intercept($command); $handler = $this->handlerRegistry->findCommandHandlerFor($command); $this->unitOfWork->start(); $interceptorChain = new DefaultInterceptorChain($command, $this->unitOfWork, $handler, ...$this->handlerInterceptors); try { $result = $interceptorChain->proceed(); if (null !== $callback) { $callback->onSuccess($result); } } catch (\Exception $exception) { $this->unitOfWork->rollback($exception); if (null !== $callback) { $callback->onFailure($exception); } throw $exception; } $this->unitOfWork->commit(); return $result; }