ErrorController::statusCodeAction()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 14
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 22
rs 9.2
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
   /**
17
     * Display a page for a HTTP status code.
18
     *
19
     * @param string code    status code to set the http header.
20
     * @param string message an optional message to display together with the error code.
21
     *
22
     * @return void
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 (!$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