Completed
Pull Request — master (#4)
by Matthias
02:20 queued 21s
created

ControllerService::runControllerAction()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7.0119

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 30
cts 32
cp 0.9375
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 20
nop 3
crap 7.0119
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mglaub
5
 * Date: 22.06.2015
6
 * Time: 15:53
7
 */
8
9
namespace MaglLegacyApplication\Service;
10
11
12
use Zend\EventManager\Event;
13
use Zend\EventManager\EventManager;
14
use Zend\EventManager\EventManagerAwareInterface;
15
use Zend\EventManager\EventManagerInterface;
16
use Zend\Http\Response;
17
use Zend\Mvc\Controller\AbstractActionController;
18
use Zend\Mvc\MvcEvent;
19
use Zend\Mvc\Router\RouteMatch;
20
use MaglLegacyApplication\Application\MaglLegacy;
21
use Zend\Mvc\View\Http\DefaultRenderingStrategy;
22
use Zend\Mvc\View\Http\ViewManager;
23
use Zend\ServiceManager\ServiceManager;
24
use Zend\View\Model\JsonModel;
25
use Zend\View\Model\ViewModel;
26
use Zend\View\ViewEvent;
27
28
class ControllerService
29
{
30
31
    /**
32
     * @var MvcEvent
33
     */
34
    private $event;
35
36
    /**
37
     * @var EventManager
38
     */
39
    private $eventManager;
40
41
    /**
42
     * @var RouteMatch
43
     */
44
    private $routeMatch;
45
46 1
    public function __construct(EventManager $eventManager, MvcEvent $event)
47
    {
48 1
        $this->eventManager = $eventManager;
49 1
        $this->event = $event;
50 1
    }
51
52
    /**
53
     * @param $controllerName
54
     * @param $action
55
     * @param array $params
56
     * @return string|\Zend\Stdlib\ResponseInterface
57
     * @throws \Exception
58
     */
59 1
    public function runControllerAction($controllerName, $action, $params = array())
60
    {
61
62 1
        $this->event->getRouteMatch()
63 1
            ->setParam('controller', $controllerName)
64 1
            ->setParam('action', $action);
65
66 1
        foreach ($params as $key => $value) {
67
            $this->event->getRouteMatch()->setParam($key, $value);
68 1
        }
69
70 1
        $serviceManager = $this->event->getApplication()->getServiceManager();
71 1
        $controllerManager = $serviceManager->get('ControllerLoader');
72
73
        /** @var AbstractActionController $controller */
74 1
        $controller = $controllerManager->get($controllerName);
75
76 1
        $controller->setEvent($this->event);
77 1
        $result = $controller->dispatch($this->event->getRequest());
78
79 1
        if ($result instanceof Response) {
80
            return $result;
81
        }
82
83
        /** @var DefaultRenderingStrategy $renderingStrategy */
84 1
        $renderingStrategy = null;
85 1
        foreach (['HttpDefaultRenderingStrategy', 'DefaultRenderingStrategy'] as $serviceName) {
86 1
            if ($serviceManager->has($serviceName)) {
87 1
                $renderingStrategy = $serviceManager->get($serviceName);
88 1
            }
89 1
        }
90
91 1
        $this->event->setViewModel($result);
92
93 1
        if($renderingStrategy instanceof DefaultRenderingStrategy) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
94
            /** @var ViewModel $result */
95 1
            if (!$result->terminate()) {
96 1
                $layout = new ViewModel();
97 1
                $layoutTemplate = $renderingStrategy->getLayoutTemplate();
98 1
                $layout->setTemplate($layoutTemplate);
99 1
                $layout->addChild($result);
100 1
                $this->event->setViewModel($layout);
101 1
            }
102 1
        }
103
104 1
        $response = $renderingStrategy->render($this->event);
105
106 1
        return $response;
107
    }
108
109
}