Passed
Push — 9.0-dev ( bf3f06...4f14a6 )
by Radu
01:13
created

AbstractApplication::haltCli()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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