/**
  * @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;
 }
 private function registerUserCommands()
 {
     $simpleEventBus = new SimpleEventBus();
     $userRepository = new PdoUserRepository($this->unitOfWork, $this->pdo, $simpleEventBus);
     $passwordHashingService = new BcryptPasswordHashingService();
     $registerUserService = new RegisterUserService($passwordHashingService);
     // Register User.
     $this->commandHandlerRegistry->subscribe(RegisterUserCommand::class, new RegisterUserCommandHandler($registerUserService, $userRepository));
     // Change Username.
     $this->commandHandlerRegistry->subscribe(ChangeUsernameCommand::class, new ChangeUsernameCommandHandler($userRepository));
     // Change Password.
     $this->commandHandlerRegistry->subscribe(ChangeUserPasswordCommand::class, new ChangeUserPasswordCommandHandler($userRepository, $passwordHashingService));
     $claimedUsernamesProjection = new ClaimedUsernamesProjection($this->pdo);
     $simpleEventBus->subscribe($claimedUsernamesProjection);
 }