FrontController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Test Coverage

Coverage 70.83%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 17
dl 0
loc 138
ccs 51
cts 72
cp 0.7083
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
C render() 0 63 11
A getResponse() 0 4 1
A getRequest() 0 4 1
A getConfigurationFactory() 0 4 1
A getDiContainer() 0 4 1
A getViewConfiguration() 0 4 1
A getBuilder() 0 4 1
A getConfiguration() 0 4 1
A getMergedStructure() 0 4 1
A getContextFile() 0 4 1
A getStorage() 0 4 1
1
<?php
2
3
namespace Magium\Configuration\View;
4
5
use Interop\Container\ContainerInterface;
6
use Magium\Configuration\Config\Repository\ConfigurationRepository;
7
use Magium\Configuration\MagiumConfigurationFactoryInterface;
8
use Magium\Configuration\View\Controllers\Layout;
9
use Magium\Configuration\View\Controllers\Rebuild;
10
use Magium\Configuration\View\Controllers\Save;
11
use Magium\Configuration\View\Controllers\View;
12
use Zend\View\Model\JsonModel;
13
use Zend\View\Model\ViewModel;
14
use Zend\View\Renderer\PhpRenderer;
15
use Zend\View\Resolver\TemplatePathStack;
16
17
class FrontController
18
{
19
20
    protected $viewConfiguration;
21
    protected $magiumConfigurationFactory;
22
    protected $diContainer;
23
    protected $view;
24
25 1
    public function __construct(
26
        ViewConfiguration $viewConfiguration,
27
        MagiumConfigurationFactoryInterface $magiumConfigurationFactory,
28
        ContainerInterface $diContainer
29
    )
30
    {
31 1
        $this->viewConfiguration = $viewConfiguration;
32 1
        $this->magiumConfigurationFactory = $magiumConfigurationFactory;
33 1
        $this->diContainer = $diContainer;
34 1
    }
35
36
    /**
37
     * @return \Psr\Http\Message\ResponseInterface
38
     */
39
40 10
    public function render()
41
    {
42 10
        $request = $this->getRequest();
43 10
        $params = $request->getQueryParams();
44 10
        $viewModel = null;
45
46
        // This pseudo router section is because I don't want to create yet-another-dependency in the project.
47 10
        if ($request->getMethod() == 'POST' && isset($params['rebuild'])) {
48
            $rebuild = new Rebuild($this->getBuilder(), $this->getContextFile());
49
            $viewModel = $rebuild->execute($request);
50 10
        } else if ($request->getMethod() == 'GET' && !isset($params['section'])) {
51 9
            $layout = new Layout(
52 9
                $this->getViewConfiguration(),
53 9
                $this->getContextFile(),
54 9
                $this->getConfigurationFactory(),
55 9
                $this->getMergedStructure()
56
            );
57 9
            $viewModel = $layout->execute($request);
58 1
        } else if ($request->getMethod() == 'GET' && isset($params['section'])) {
59 1
            $context = ConfigurationRepository::CONTEXT_DEFAULT;
60 1
            if (isset($params['context'])) {
61
                $context = $params['context'];
62
            }
63 1
            $view = new View(
64 1
                $this->getViewConfiguration(),
65 1
                $this->getBuilder(),
66 1
                $this->getMergedStructure(),
67 1
                $this->getStorage(),
68 1
                $context,
69 1
                $this->getDiContainer()
70
            );
71 1
            $viewModel = $view->execute($request);
72
        } else if ($request->getMethod() == 'POST') {
73
            $save = new Save(
74
                $this->getConfigurationFactory()->getBuilder()
75
            );
76
            $viewModel = $save->execute($request);
77
        } else {
78
            $this->getResponse()->withStatus(404);
79
        }
80
81 10
        $content = '';
82
83 10
        if ($viewModel instanceof JsonModel) {
84
            $content = $viewModel->serialize();
85 10
        } else if ($viewModel instanceof ViewModel) {
86 10
            $renderer = new PhpRenderer();
87 10
            $mRenderer = new MagiumRenderer();
88 10
            $mRenderer->setView($renderer);
89 10
            $renderer->getHelperPluginManager()->setService('magiumRenderer', $mRenderer);
90
91 10
            $mRenderer = new MagiumRecursiveContextRenderer();
92 10
            $mRenderer->setView($renderer);
93 10
            $renderer->getHelperPluginManager()->setService('magiumRecursiveContextRenderer', $mRenderer);
94
95 10
            $renderer->setResolver(new TemplatePathStack(['script_paths' => [$this->getViewConfiguration()->getViewDirectory()]]));
96 10
            $content = $renderer->render($viewModel);
97
        }
98
99 10
        $response = $this->getResponse();
100 10
        $response->getBody()->write($content);
101 10
        return $response;
102
    }
103
104 1
    public function getResponse()
105
    {
106 1
        return $this->viewConfiguration->getResponse();
107
    }
108
109 1
    public function getRequest()
110
    {
111 1
        return $this->viewConfiguration->getRequest();
112
    }
113
114
    public function getConfigurationFactory()
115
    {
116
        return $this->magiumConfigurationFactory;
117
    }
118
119 1
    public function getDiContainer()
120
    {
121 1
        return $this->diContainer;
122
    }
123
124 1
    public function getViewConfiguration()
125
    {
126 1
        return $this->viewConfiguration;
127
    }
128
129
    public function getBuilder()
130
    {
131
        return $this->magiumConfigurationFactory->getBuilder();
132
    }
133
134
    public function getConfiguration($context = ConfigurationRepository::CONTEXT_DEFAULT)
135
    {
136
        return $this->magiumConfigurationFactory->getManager()->getConfiguration($context);
137
    }
138
139
    public function getMergedStructure()
140
    {
141
        return $this->magiumConfigurationFactory->getBuilder()->getMergedStructure();
142
    }
143
144
    public function getContextFile()
145
    {
146
        return $this->magiumConfigurationFactory->getContextFile();
147
    }
148
149
    public function getStorage()
150
    {
151
        return $this->magiumConfigurationFactory->getBuilder()->getStorage();
152
    }
153
154
}
155