ErrorHandlerController::catchAll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 35
ccs 0
cts 14
cp 0
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Anax\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\Route\Exception\NotFoundException;
8
9
/**
10
 * A controller to ease with development and debugging information.
11
 */
12
class ErrorHandlerController implements ContainerInjectableInterface
13
{
14
    use ContainerInjectableTrait;
15
16
17
18
    /**
19
     * Add internal routes for 403, 404 and 500 that provides a page with
20
     * error information, using the default page layout.
21
     *
22
     * @param string $message with details.
23
     *
24
     * @throws Anax\Route\Exception\NotFoundException
25
26
     * @return object as the response.
27
     */
28
    public function catchAll() : object
29
    {
30
        $title = " | Anax";
31
        $pages = [
32
            "403" => [
33
                "Anax 403: Forbidden",
34
                "You are not permitted to do this."
35
            ],
36
            "404" => [
37
                "Anax 404: Not Found",
38
                "The page you are looking for is not here."
39
            ],
40
            "500" => [
41
                "Anax 500: Internal Server Error",
42
                "An unexpected condition was encountered."
43
            ],
44
        ];
45
46
        $path = $this->di->get("router")->getMatchedPath();
47
        if (!array_key_exists($path, $pages)) {
48
            throw new NotFoundException("Internal route for '$path' is not found.");
49
        }
50
51
        $page = $this->di->get("page");
52
        $page->add(
53
            "anax/v2/error/default",
54
            [
55
                "header" => $pages[$path][0],
56
                "text" => $pages[$path][1],
57
            ]
58
        );
59
60
        return $page->render([
61
            "title" => $pages[$path][0] . $title
62
        ], $path);
63
    }
64
}
65