public function onDispatch(SystemEvent $event)
    {
        $server  = $this->getServer();
        $request = $server->getRequest();

        $controllerName = $request->getAttribute('controller');
        if (! $controllerName) {
            throw new RuntimeException(
                'Unable to dispatch the system event, the server request not '
                . 'contains the "controller" attribute.'
            );
        }
        $actionName = $request->getAttribute('action', 'index');

        $events = $this->getEvents();

        $controllers = $this->getControllers();
        $controller  = $controllers->get($controllerName);
        $event->setContext($controller);

        $dispatchEvent = new DispatchEvent(
            $controller, $controllerName, $actionName, $event->getParams()
        );

        $events->trigger($dispatchEvent);
        $result = $dispatchEvent->getResult();
        $target = SystemEvent::DISPATCH;
        if ($result instanceof ResponseInterface) {
            $target = SystemEvent::FINISH;
        }
        $event->setResult($target, $result);
    }