/**
  * Finds and instanciates a controller that matches the current request.
  * If no controller can be found, an instance of NotFoundControllerInterface is returned.
  *
  * @param \F3\FLOW3\MVC\RequestInterface $request The request to dispatch
  * @return \F3\FLOW3\MVC\Controller\ControllerInterface
  * @author Bastian Waidelich <*****@*****.**>
  */
 protected function resolveController(\F3\FLOW3\MVC\RequestInterface $request)
 {
     $exception = NULL;
     $packageKey = $request->getControllerPackageKey();
     if (!$this->packageManager->isPackageAvailable($packageKey)) {
         $exception = new \F3\FLOW3\MVC\Controller\Exception\InvalidPackageException($request, 'package "' . $packageKey . '" does not exist');
     } elseif (!$this->packageManager->isPackageActive($packageKey)) {
         $exception = new \F3\FLOW3\MVC\Controller\Exception\InactivePackageException($request, 'package "' . $packageKey . '" is not active');
     } else {
         $controllerObjectName = $request->getControllerObjectName();
         if ($controllerObjectName === '') {
             $exception = new \F3\FLOW3\MVC\Controller\Exception\InvalidControllerException($request, 'no controller could be resolved which would match your request');
         }
     }
     if ($exception !== NULL) {
         $controller = $this->objectManager->getObject($this->settings['mvc']['notFoundController']);
         if (!$controller instanceof \F3\FLOW3\MVC\Controller\NotFoundControllerInterface) {
             throw new \F3\FLOW3\MVC\Exception\InvalidControllerException('The NotFoundController must implement "\\F3\\FLOW3\\MVC\\Controller\\NotFoundControllerInterface", ' . (is_object($controller) ? get_class($controller) : gettype($controller)) . ' given.', 1246714416);
         }
         $controller->setException($exception);
     } else {
         $controller = $this->objectManager->getObject($controllerObjectName);
         if (!$controller instanceof \F3\FLOW3\MVC\Controller\ControllerInterface) {
             throw new \F3\FLOW3\MVC\Exception\InvalidControllerException('Invalid controller "' . $request->getControllerObjectName() . '". The controller must be a valid request handling controller, ' . (is_object($controller) ? get_class($controller) : gettype($controller)) . ' given.', 1202921619);
         }
     }
     return $controller;
 }