Passed
Push — master ( 2b6224...357a76 )
by Radu
01:41
created

AbstractApplication::router()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework;
3
4
use WebServCo\Framework\Framework as Fw;
5
use WebServCo\Framework\Environment as Env;
6
7
abstract class AbstractApplication
8
{
9
    abstract protected function config();
10
    abstract protected function request();
11
12
    /**
13
     * Handle Errors.
14
     *
15
     * @param mixed $exception An \Error or \Exception object.
16
     */
17
    final protected function handleErrors($exception = null)
18
    {
19
        $errorInfo = [
20
            'code' => 0,
21
            'message' => null,
22
            'file' => null,
23
            'line' => null,
24
            'trace' => null,
25
        ];
26
        if ($exception instanceof \Throwable ||
27
            $exception instanceof \Exception
28
        ) {
29
            $errorInfo['code'] = $exception->getCode();
30
            $errorInfo['message'] = $exception->getMessage();
31
            $errorInfo['file'] = $exception->getFile();
32
            $errorInfo['line'] = $exception->getLine();
33
            $errorInfo['trace'] = $exception->getTraceAsString();
34
        } else {
35
            $last_error = error_get_last();
36
            if (!empty($last_error['message'])) {
37
                $errorInfo['message'] = $last_error['message'];
38
            }
39
            if (!empty($last_error['file'])) {
40
                $errorInfo['file'] = $last_error['file'];
41
            }
42
            if (!empty($last_error['line'])) {
43
                $errorInfo['line'] = $last_error['line'];
44
            }
45
        }
46
        if (!empty($errorInfo['message'])) {
47
            return $this->halt($errorInfo);
48
        }
49
        return false;
50
    }
51
52
    final protected function halt($errorInfo = [])
53
    {
54
        if (Fw::isCLI()) {
55
            return $this->haltCli($errorInfo);
56
        } else {
57
            return $this->haltHttp($errorInfo);
58
        }
59
    }
60
61
    protected function haltHttp($errorInfo = [])
62
    {
63
        switch ($errorInfo['code']) {
64
            case 404:
65
                $statusCode = 404; //not found
66
                $title = 'Resource not found';
67
                break;
68
            case 500: //application
69
            case 0: //default
70
            default:
71
                $statusCode = 500;
72
                $title = 'The App made a boo boo';
73
                break;
74
        }
75
76
        $output = '<!doctype html>
77
<html>
78
<head>
79
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
80
    <title>Oups</title>
81
    <style>
82
    * {background: #f2dede; color: #a94442;}
83
    .o {display: table; position: absolute; height: 100%; width: 100%;}
84
    .m {display: table-cell; vertical-align: middle;}
85
    .i {margin-left: auto; margin-right: auto; width: 680px; text-align: center;}
86
    small {font-size: 0.7em;}
87
    </style>
88
</head>
89
<body><div class="o"><div class="m"><div class="i">' .
90
        "<h1>{$title}</h1>";
91
        if (Env::ENV_PROD !== $this->config()->getEnv()) {
92
            $output .= "<p>$errorInfo[message]</p>" .
93
            "<p><small>$errorInfo[file]:$errorInfo[line]</small></p>";
94
        }
95
        $output .= '</div></div></div></body></html>';
96
97
        $response = new \WebServCo\Framework\Libraries\HttpResponse(
98
            $output,
99
            $statusCode,
100
            ['Content-Type' => 'text/html']
101
        );
102
        $response->send($this->request());
103
        return true;
104
    }
105
106
    protected function haltCli($errorInfo = [])
107
    {
108
        $output = 'The App made a boo boo' . PHP_EOL;
109
        if (Env::ENV_PROD !== $this->config()->getEnv()) {
110
            $output .= $errorInfo['message'] . PHP_EOL;
111
            $output .= "$errorInfo[file]:$errorInfo[line]" . PHP_EOL;
112
        }
113
        $response = new \WebServCo\Framework\Libraries\CliResponse(
114
            $output,
115
            1
116
        );
117
        $response->send($this->request());
118
        return true;
119
    }
120
}
121