Completed
Push — master ( 0d492a...3b127f )
by Flo
12:04
created

AbstractController::__call()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 14

Duplication

Lines 16
Ratio 48.48 %

Importance

Changes 0
Metric Value
dl 16
loc 33
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A AbstractController::route() 0 4 1
A AbstractController::getRequest() 0 4 1
A AbstractController::addAssets() 0 3 1
1
<?php
2
3
namespace Faulancer\Controller;
4
5
use Faulancer\Exception\ConfigInvalidException;
6
use Faulancer\Exception\FileNotFoundException;
7
use Faulancer\Exception\InvalidArgumentException;
8
use Faulancer\Exception\PluginException;
9
use Faulancer\Exception\ServiceNotFoundException;
10
use Faulancer\Http\Request;
11
use Faulancer\Http\Response;
12
use Faulancer\Service\AuthenticatorService;
13
use Faulancer\Service\Config;
14
use Faulancer\Service\DbService;
15
use Faulancer\Service\HttpService;
16
use Faulancer\Service\ResponseService;
17
use Faulancer\Service\SessionManagerService;
18
use Faulancer\ServiceLocator\ServiceInterface;
19
use Faulancer\View\Helper\Route;
20
use Faulancer\View\ViewController;
21
use Faulancer\ServiceLocator\ServiceLocator;
22
23
/**
24
 * Class AbstractController
25
 *
26
 * @category Controller
27
 * @package  Faulancer\AbstractController
28
 * @author   Florian Knapp <[email protected]>
29
 */
30
abstract class AbstractController
31
{
32
33
    /**
34
     * Holds the views per controller request
35
     *
36
     * @var array
37
     */
38
    private $_viewArray = [];
39
40
    /**
41
     * Holds the request
42
     *
43
     * @var Request
44
     */
45
    protected $request;
46
47
    /**
48
     * AbstractController constructor.
49
     *
50
     * @param Request $request The request object
51
     */
52
    public function __construct(Request $request)
53
    {
54
        $this->request = $request;
55
    }
56
57
    /**
58
     * Returns the service locator
59
     *
60
     * @return ServiceLocator
61
     */
62
    public function getServiceLocator() :ServiceLocator
63
    {
64
        return ServiceLocator::instance();
65
    }
66
67
    /**
68
     * Returns the session manager
69
     *
70
     * @return SessionManagerService|ServiceInterface
71
     *
72
     * @throws ServiceNotFoundException
73
     */
74
    public function getSessionManager() :SessionManagerService
75
    {
76
        return $this->getServiceLocator()->get(SessionManagerService::class);
77
    }
78
79
    /**
80
     * Returns the view controller
81
     *
82
     * @return ViewController
83
     */
84
    public function getView() :ViewController
85
    {
86
        $calledClass = get_called_class();
87
88
        if (in_array($calledClass, array_keys($this->_viewArray), true)) {
89
            return $this->_viewArray[$calledClass];
90
        }
91
92
        $viewController = new ViewController();
93
94
        $this->_viewArray[$calledClass] = $viewController;
95
96
        return $viewController;
97
98
    }
99
100
    /**
101
     * Returns the orm/entity manager
102
     *
103
     * @return DbService|ServiceInterface
104
     *
105
     * @throws ServiceNotFoundException
106
     */
107
    public function getDb() :DbService
108
    {
109
        return $this->getServiceLocator()->get(DbService::class);
110
    }
111
112
    /**
113
     * Render view with given template
114
     *
115
     * @param string $template  The template to be rendered
116
     * @param array  $variables The variables for the template
117
     *
118
     * @return Response
119
     *
120
     * @throws ServiceNotFoundException
121
     */
122
    public function render(string $template = '', $variables = []) :Response
123
    {
124
        $this->addAssets();
125
126
        /** @var Response $response */
127
        $response = $this->getServiceLocator()->get(ResponseService::class);
128
129
        try {
130
131
            $viewResult = $this->getView()
132
                ->setTemplate($template)
133
                ->setVariables($variables)
134
                ->render();
135
136
        } catch (FileNotFoundException $e) {
137
            $viewResult = $e->getMessage();
138
        } catch (ConfigInvalidException $e) {
139
            $viewResult = $e->getMessage();
140
        }
141
142
        return $response->setContent($viewResult);
143
    }
144
145
    /**
146
     * Check if user is permitted based on his role(s)
147
     *
148
     * @param array $roles The corresponding user roles
149
     *
150
     * @return bool|null
151
     *
152
     * @throws ServiceNotFoundException
153
     */
154
    public function isPermitted($roles = [])
155
    {
156
        /** @var AuthenticatorService $authService */
157
        $authService = $this->getServiceLocator()->get(AuthenticatorService::class);
158
159
        return $authService->isPermitted($roles);
160
    }
161
162
    /**
163
     * Redirect to specific uri
164
     *
165
     * @param string $uri The target uri
166
     *
167
     * @return bool
168
     *
169
     * @throws ServiceNotFoundException
170
     * @throws InvalidArgumentException
171
     */
172
    public function redirect(string $uri) :bool
173
    {
174
        /** @var HttpService $httpService */
175
        $httpService = $this->getServiceLocator()->get(HttpService::class);
176
        return $httpService->redirect($uri);
177
    }
178
179
    /**
180
     * Set a generic text token which is valid for exactly one call
181
     *
182
     * @param string $key     Key for the flash message
183
     * @param string $message Content for the flash message
184
     *
185
     * @return void
186
     *
187
     * @throws ServiceNotFoundException
188
     */
189
    public function setFlashMessage(string $key, string $message)
190
    {
191
        $sessionManager = $this->getSessionManager();
192
        $sessionManager->setFlashMessage($key, $message);
193
    }
194
195
    /**
196
     * Retrieve a flash message
197
     *
198
     * @param string $key The flash message key
199
     *
200
     * @return string|null
201
     *
202
     * @throws ServiceNotFoundException
203
     */
204
    public function getFlashMessage(string $key)
205
    {
206
        $sessionManager = $this->getSessionManager();
207
        return $sessionManager->getFlashMessage($key);
208
    }
209
210
    /**
211
     * Get the url for a specific route name
212
     *
213
     * @param string $name       Name of the route
214
     * @param array  $parameters Apply parameters where necessary
215
     * @param bool   $absolute   Return an absolute url with host as prefix
216
     *
217
     * @return string
218
     */
219
    public function route(string $name, array $parameters = [], $absolute = false)
220
    {
221
        return (new Route())($this->getView(), $name, $parameters, $absolute);
222
    }
223
224
    /**
225
     * Return the current request object
226
     *
227
     * @return Request
228
     */
229
    public function getRequest() :Request
230
    {
231
        return $this->request;
232
    }
233
234
    /**
235
     * Add default assets for every action
236
     */
237
    protected function addAssets() {
238
        // Should be inherited by child classes
239
    }
240
241
}