Framework::getRouteError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FMUP;
3
4
use FMUP\Exception\Status\NotFound;
5
6
/**
7
 * Class Framework - extends FMU
8
 * @package FMUP
9
 */
10
class Framework extends \Framework
0 ignored issues
show
Deprecated Code introduced by
The class Framework has been deprecated with message: use \FMUP\Framework instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
11
{
12
    use Config\OptionalTrait;
13
14
    /**
15
     * @var Response
16
     */
17
    private $response;
18
    /**
19
     * @var Routing
20
     */
21
    private $routingSystem;
22
    /**
23
     * @var ErrorHandler
24
     */
25
    private $errorHandler;
26
27
    /**
28
     * @var Dispatcher
29
     */
30
    private $preDispatcherSystem;
31
32
    /**
33
     * @var Dispatcher
34
     */
35
    private $postDispatcherSystem;
36
    /**
37
     * @var Bootstrap
38
     */
39
    private $bootstrap;
40
41
    /**
42
     * @param Routing $routingSystem
43
     * @return $this
44
     */
45 2
    public function setRoutingSystem(Routing $routingSystem)
46
    {
47 2
        $this->routingSystem = $routingSystem;
48 2
        return $this;
49
    }
50
51
    /**
52
     * @return Routing
53
     */
54 3
    public function getRoutingSystem()
55
    {
56 3
        return $this->routingSystem = $this->routingSystem ?: new Routing();
57
    }
58
59
    /**
60
     * @return Response
61
     */
62 6
    public function getResponse()
63
    {
64 6
        return $this->response = $this->response ?: new Response();
65
    }
66
67
    /**
68
     * @param Response $response
69
     * @return $this
70
     */
71 1
    public function setResponse(Response $response)
72
    {
73 1
        $this->response = $response;
74 1
        return $this;
75
    }
76
77
    /**
78
     * @return array
79
     */
80 2
    public function getRoute()
81
    {
82 2
        $route = $this->getRoutingSystem()->dispatch($this->getRequest());
83 2
        if (is_null($route)) {
84
            //Dirty fix to be compliant with old system
85 1
            return parent::getRoute();
86
        }
87
        //dirty to be compliant with old system
88 1
        return array($route->getControllerName(), $route->getAction());
89
    }
90
91
    /**
92
     * @param string $directory
93
     * @param string $controller
94
     * Real 404 errors
95
     * @throws NotFound
96
     */
97 2
    public function getRouteError($directory, $controller)
98
    {
99 2
        throw new NotFound('Controller not found ' . $directory . '/' . $controller);
100
    }
101
102
    /**
103
     * @param string $controllerName
104
     * @param string $action
105
     * @return Controller
106
     * @throws Exception\Status\NotFound
107
     */
108 4
    protected function instantiate($controllerName, $action)
109
    {
110
        //To be compliant with old system @todo
111 4
        if (!class_exists($controllerName)) {
112 1
            throw new Exception\Status\NotFound('Controller does not exist');
113
        }
114
        /* @var $controllerInstance Controller */
115 3
        $controllerInstance = new $controllerName();
116
        $controllerInstance
117 3
            ->setRequest($this->getRequest())
118 3
            ->setResponse($this->getResponse())
119 3
            ->setBootstrap($this->getBootstrap());
120
121 3
        $controllerInstance->preFilter($action);
0 ignored issues
show
Unused Code introduced by
The call to the method FMUP\Controller::preFilter() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
122 3
        $callable = $controllerInstance->getActionMethod($action);
123 3
        $actionReturn = null;
0 ignored issues
show
Unused Code introduced by
$actionReturn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124 3
        if (!is_callable(array($controllerInstance, $callable))) {
125 1
            throw new Exception\Status\NotFound("Undefined function $callable");
126
        }
127 2
        $actionReturn = call_user_func(array($controllerInstance, $callable));
128 2
        $controllerInstance->postFilter($action);
0 ignored issues
show
Unused Code introduced by
The call to the method FMUP\Controller::postFilter() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
129
130 2
        if (!is_null($actionReturn)) {
131 1
            $controllerInstance->getResponse()
132 1
                ->setBody($actionReturn instanceof View ? $actionReturn->render() : $actionReturn);
133
        }
134 2
        return $controllerInstance;
135
    }
136
137
    /**
138
     * @return $this
139
     */
140 2
    protected function dispatch()
141
    {
142
        try {
143 2
            $this->preDispatch();
144 1
            parent::dispatch();
145 2
        } catch (Exception\Location $exception) {
146 1
            $this->getResponse()->addHeader(new Response\Header\Location($exception->getLocation()));
147 1
        } catch (\Exception $exception) {
148 1
            $this->getErrorHandler()
149 1
                ->setBootstrap($this->getBootstrap())
150 1
                ->setRequest($this->getRequest())
151 1
                ->setResponse($this->getResponse())
152 1
                ->handle($exception);
153
        }
154 1
        $this->postDispatch();
155 1
        return $this;
156
    }
157
158
    /**
159
     * @return ErrorHandler\Plugin\Mail
160
     * @codeCoverageIgnore
161
     */
162
    protected function createPluginMail()
163
    {
164
        return new ErrorHandler\Plugin\Mail();
165
    }
166
167
    /**
168
     * @param int $code
169
     * @param string $msg
170
     * @param null $errFile
171
     * @param int $errLine
172
     * @param array $errContext
173
     */
174 1
    public function errorHandler($code, $msg, $errFile = null, $errLine = 0, array $errContext = array())
175
    {
176 1
        $block = E_PARSE | E_ERROR | E_USER_ERROR;
177 1
        $binary = $code & $block;
178 1
        $message = $msg . ' in file ' . $errFile . ' on line ' . $errLine;
179 1
        if ($binary) {
180 1
            $message .= ' {' . serialize($errContext) . '}';
181 1
            $this->createPluginMail()
182 1
                ->setBootstrap($this->getBootstrap())
183 1
                ->setRequest($this->getRequest())
184 1
                ->setException(new Exception($message, $code))
185 1
                ->handle();
186
        }
187
188
        $translate = array(
189 1
            E_NOTICE => Logger::NOTICE,
190 1
            E_WARNING => Logger::WARNING,
191 1
            E_ERROR => Logger::ERROR,
192 1
            E_PARSE => Logger::CRITICAL,
193 1
            E_DEPRECATED => Logger::INFO,
194 1
            E_USER_ERROR => Logger::NOTICE,
195 1
            E_USER_WARNING => Logger::WARNING,
196 1
            E_USER_ERROR => Logger::ERROR,
197 1
            E_USER_DEPRECATED => Logger::INFO,
198 1
            E_STRICT => Logger::INFO,
199 1
            E_RECOVERABLE_ERROR => Logger::ERROR,
200 1
            E_ALL => Logger::CRITICAL,
201 1
            E_COMPILE_ERROR => Logger::ERROR,
202 1
            E_COMPILE_WARNING => Logger::WARNING,
203 1
            E_CORE_ERROR => Logger::ERROR,
204 1
            E_CORE_WARNING => Logger::WARNING,
205 1
            E_USER_NOTICE => Logger::NOTICE,
206
        );
207 1
        $this->getBootstrap()->getLogger()->log(Logger\Channel\System::NAME, $translate[$code], $message, $errContext);
208 1
        if ($binary && $this->getSapi()->get() == Sapi::CLI) {
209 1
            $this->phpExit($binary);
210
        }
211 1
    }
212
213
    /**
214
     * @param int $code
215
     * @codeCoverageIgnore
216
     */
217
    protected function phpExit($code)
218
    {
219
        exit($code);
0 ignored issues
show
Coding Style Compatibility introduced by
The method phpExit() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
220
    }
221
222
    /**
223
     * @return ErrorHandler
224
     */
225 2
    public function getErrorHandler()
226
    {
227 2
        return $this->errorHandler = $this->errorHandler ?: new ErrorHandler\Base();
228
    }
229
230
    /**
231
     * @param ErrorHandler $errorHandler
232
     * @return $this
233
     */
234 3
    public function setErrorHandler(ErrorHandler $errorHandler)
235
    {
236 3
        $this->errorHandler = $errorHandler;
237 3
        return $this;
238
    }
239
240
    /**
241
     * PreDispatch Request
242
     * @return $this
243
     */
244 1
    protected function preDispatch()
245
    {
246 1
        $this->getPreDispatcherSystem()->dispatch($this->getRequest(), $this->getResponse());
247 1
        return $this;
248
    }
249
250
    /**
251
     * Treatment on response if needed
252
     */
253 1
    protected function postDispatch()
254
    {
255 1
        $this->getPostDispatcherSystem()->dispatch($this->getRequest(), $this->getResponse());
256 1
        return $this;
257
    }
258
259
    /**
260
     * @todo rewrite to be SOLID
261
     */
262 2
    public function shutDown()
263
    {
264 2
        $error = $this->errorGetLast();
265 2
        $isDebug = $this->isDebug();
266 2
        $code = E_PARSE | E_ERROR | E_USER_ERROR;
267 2
        $canHeader = $this->getSapi()->get() != Sapi::CLI;
268 2
        if ($error !== null && ($error['type'] & $code)) {
269 1
            $this->errorHandler($code, $error['message'], $error['file'], $error['line']);
270 1
            if ($canHeader) {
271 1
                $this->getErrorHeader()->render();
272 1
                if (!$isDebug) {
273
                    echo "<br/>Une erreur est survenue !<br/>"
274
                        . "Le support informatique a été prévenu "
275
                        . "et règlera le problême dans les plus brefs délais.<br/>"
276
                        . "<br/>"
277 1
                        . "L'équipe des développeurs vous prie de l'excuser pour le désagrément.<br/>";
278
                }
279
            }
280
        }
281 2
    }
282
283
    /**
284
     * @return Response\Header\Status
285
     * @codeCoverageIgnore
286
     */
287
    protected function getErrorHeader()
288
    {
289
        return new Response\Header\Status(Response\Header\Status::VALUE_INTERNAL_SERVER_ERROR);
290
    }
291
292
    /**
293
     * @return array
294
     * @codeCoverageIgnore
295
     */
296
    protected function errorGetLast()
297
    {
298
        return error_get_last();
299
    }
300
301
    /**
302
     * @return bool
303
     * @codeCoverageIgnore
304
     */
305
    protected function isDebug()
306
    {
307
        return (bool)ini_get('display_errors');
308
    }
309
310
    /**
311
     * @return Dispatcher
312
     */
313 2
    public function getPreDispatcherSystem()
314
    {
315 2
        return $this->preDispatcherSystem = $this->preDispatcherSystem ?: new Dispatcher();
316
    }
317
318
    /**
319
     * @param Dispatcher $preDispatch
320
     * @return $this
321
     */
322 1
    public function setPreDispatcherSystem(Dispatcher $preDispatch)
323
    {
324 1
        $this->preDispatcherSystem = $preDispatch;
325 1
        return $this;
326
    }
327
328
    /**
329
     * @return Dispatcher
330
     */
331 2
    public function getPostDispatcherSystem()
332
    {
333 2
        return $this->postDispatcherSystem = $this->postDispatcherSystem ?: new Dispatcher\Post();
334
    }
335
336
    /**
337
     * @param Dispatcher $postDispatch
338
     * @return $this
339
     */
340 2
    public function setPostDispatcherSystem(Dispatcher $postDispatch)
341
    {
342 2
        $this->postDispatcherSystem = $postDispatch;
343 2
        return $this;
344
    }
345
346
    /**
347
     * @return Bootstrap
348
     */
349 8
    public function getBootstrap()
350
    {
351 8
        return $this->bootstrap = $this->bootstrap ?: new Bootstrap;
352
    }
353
354
    /**
355
     * @param Bootstrap $bootstrap
356
     * @return $this
357
     */
358 6
    public function setBootstrap(Bootstrap $bootstrap)
359
    {
360 6
        $this->bootstrap = $bootstrap;
361 6
        return $this;
362
    }
363
364 4
    public function initialize()
365
    {
366 4
        if (!$this->getBootstrap()->hasSapi()) {
367 3
            $this->getBootstrap()->setSapi($this->getSapi());
368
        }
369 4
        if (!$this->getBootstrap()->hasRequest()) {
370 3
            $this->getBootstrap()->setRequest($this->getRequest());
371
        }
372 4
        if (!$this->getBootstrap()->hasConfig()) {
373 3
            $this->getBootstrap()->setConfig($this->getConfig());
374
        }
375 4
        $this->getBootstrap()->warmUp();
376 4
        parent::initialize();
377 3
    }
378
}
379