ErrorController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 4
c 5
b 0
f 0
lcom 1
cbo 2
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A statusCodeAction() 0 22 3
A displayValidRoutesAction() 0 9 1
1
<?php
2
3
namespace Anax\MVC;
4
5
/**
6
 * Anax base class implementing Dependency Injection / Service Locator
7
 * of the services used by the framework, using lazy loading.
8
 *
9
 */
10
class ErrorController
11
{
12
    use \Anax\DI\TInjectionAware;
13
14
15
    /**
16
     * Display a page for a HTTP status code.
17
     *
18
     * @param string $code status code to set the http header.
19
     * @param string $message an optional message to display together with the error code.
20
     *
21
     * @return void
22
     * @throws \Anax\Exception\NotFoundException
23
     */
24
    public function statusCodeAction($code = null, $message = null)
25
    {
26
        $codes = [
27
            403 => "403 Forbidden",
28
            404 => "404 Not Found",
29
            500 => "500 Internal Server Error",
30
        ];
31
32
        // Key being integer also (unintentionally) prevents this action from direct url usage
33
        if (empty($code) || !in_array($code, $codes)) {
34
            throw new \Anax\Exception\NotFoundException("Not a valid HTTP status code.");
35
        }
36
37
        $title = $codes[$code];
38
        $this->di->response->setHeader($code);
39
        $this->di->theme->setTitle($title);
40
        $this->di->views->add('default/error', [
41
            'title' => $title,
42
            'content' => $message,
43
            'details' => $this->di->flash->getMessage(),
44
        ]);
45
    }
46
47
48
49
     /**
50
       * Display a list of valid routes.
51
       *
52
       * @return void
53
       */
54
    public function displayValidRoutesAction()
55
    {
56
        $this->di->views->add('default/error-routes', [
57
            'route'           => $this->di->request->getRoute(),
58
            'routes'          => $this->di->router->getAll(),
59
            'internalRoutes'  => $this->di->router->getInternal(),
60
            'controllers'     => $this->di->getControllers(),
61
        ]);
62
    }
63
}
64