Passed
Push — master ( 3aecf0...39a7f4 )
by Radu
01:48
created

AbstractApplication::haltHttp()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 76
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 76
rs 7.246
c 0
b 0
f 0
cc 10
nc 12
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace WebServCo\Framework;
3
4
use WebServCo\Framework\Environment;
5
6
abstract class AbstractApplication
7
{
8
    protected $projectNamespace;
9
    protected $projectPath;
10
11
    abstract protected function config();
12
    abstract protected function request();
13
14
    public function __construct($publicPath, $projectPath, $projectNamespace = 'Project')
15
    {
16
        $this->projectNamespace = $projectNamespace;
17
        $publicPath = rtrim($publicPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
18
        $this->projectPath = rtrim($projectPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
19
20
        if (!is_readable($publicPath . 'index.php') || !is_readable($this->projectPath . '.env')) {
21
            throw new \WebServCo\Framework\Exceptions\ApplicationException(
22
                'Invalid paths specified when initializing Application.'
23
            );
24
        }
25
26
        $this->config()->set(sprintf('app%1$spath%1$sweb', Settings::DIVIDER), $publicPath);
27
        $this->config()->set(sprintf('app%1$spath%1$sproject', Settings::DIVIDER), $this->projectPath);
28
    }
29
30
    /**
31
     * Sets the env value from the project .env file.
32
     */
33
    final public function setEnvironmentValue()
34
    {
35
        /**
36
         * Env file existence is verified in the controller.
37
         */
38
        $this->config()->setEnv(trim((string)file_get_contents($this->projectPath . '.env')));
39
40
        return true;
41
    }
42
43
    /**
44
     * Handle Errors.
45
     *
46
     * @param mixed $exception An \Error or \Exception object.
47
     */
48
    final protected function handleErrors($exception = null)
49
    {
50
        $errorInfo = [
51
            'code' => 0,
52
            'message' => null,
53
            'file' => null,
54
            'line' => null,
55
            'trace' => null,
56
        ];
57
        if ($exception instanceof \Throwable ||
58
            $exception instanceof \Exception
59
        ) {
60
            $errorInfo['code'] = $exception->getCode();
61
            $errorInfo['message'] = $exception->getMessage();
62
            $errorInfo['file'] = $exception->getFile();
63
            $errorInfo['line'] = $exception->getLine();
64
            $errorInfo['trace'] = $exception->getTrace();
65
        } else {
66
            $last_error = error_get_last();
67
            if (!empty($last_error['message'])) {
68
                $errorInfo['message'] = $last_error['message'];
69
            }
70
            if (!empty($last_error['file'])) {
71
                $errorInfo['file'] = $last_error['file'];
72
            }
73
            if (!empty($last_error['line'])) {
74
                $errorInfo['line'] = $last_error['line'];
75
            }
76
        }
77
        if (!empty($errorInfo['message'])) {
78
            return $this->halt($errorInfo);
79
        }
80
        return false;
81
    }
82
83
    final protected function halt($errorInfo = [])
84
    {
85
        if (\WebServCo\Framework\Framework::isCLI()) {
86
            return $this->haltCli($errorInfo);
87
        } else {
88
            return $this->haltHttp($errorInfo);
89
        }
90
    }
91
92
    protected function haltHttp($errorInfo = [])
93
    {
94
        switch ($errorInfo['code']) {
95
            case 404:
96
                $statusCode = 404; //not found
97
                $title = 'Resource not found';
98
                break;
99
            case 500: //application
100
            case 0: //default
101
            default:
102
                $statusCode = 500;
103
                $title = 'The App made a boo boo';
104
                break;
105
        }
106
107
        $output = '<!doctype html>
108
<html>
109
<head>
110
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
111
    <title>Oups</title>
112
    <style>
113
    * {background: #f2dede; color: #a94442; overflow-wrap: break-word;}
114
    .i {margin-left: auto; margin-right: auto; text-align: center; width: auto;}
115
    small {font-size: 0.8em;}
116
    </style>
117
</head>
118
<body><div class="i"><br>' .
119
        "<h1>{$title}</h1>";
120
        if (Environment::ENV_PROD !== $this->config()->getEnv()) {
121
            $output .= sprintf(
122
                '<p><i>%s</i></p><p>%s:%s</p>',
123
                $errorInfo['message'],
124
                basename($errorInfo['file']),
125
                $errorInfo['line']
126
            );
127
            if (!empty($errorInfo['trace'])) {
128
                $output .= "<p>";
129
                $output .= "<small>";
130
                foreach ($errorInfo['trace'] as $item) {
131
                    if (!empty($item['class'])) {
132
                        $output .= sprintf(
133
                            '%s%s',
134
                            $item['class'],
135
                            $item['type']
136
                        );
137
                        $output .= "";
138
                    }
139
                    if (!empty($item['function'])) {
140
                        $output .= sprintf(
141
                            '%s',
142
                            $item['function']
143
                        );
144
                        $output .= "";
145
                    }
146
                    if (!empty($item['file'])) {
147
                        $output .= sprintf(
148
                            ' [%s:%s]',
149
                            basename($item['file']),
150
                            $item['line']
151
                        );
152
                        $output .= " ";
153
                    }
154
                    $output .= "<br>";
155
                }
156
                $output .= "</small></p>";
157
            }
158
        }
159
        $output .= '</div></body></html>';
160
161
        $response = new \WebServCo\Framework\HttpResponse(
162
            $output,
163
            $statusCode,
164
            ['Content-Type' => 'text/html']
165
        );
166
        $response->send();
167
        return true;
168
    }
169
170
    protected function haltCli($errorInfo = [])
171
    {
172
        $output = 'The App made a boo boo' . PHP_EOL;
173
        if (Environment::ENV_PROD !== $this->config()->getEnv()) {
174
            $output .= $errorInfo['message'] . PHP_EOL;
175
            $output .= "$errorInfo[file]:$errorInfo[line]" . PHP_EOL;
176
        }
177
        $response = new \WebServCo\Framework\CliResponse(
178
            $output,
179
            1
180
        );
181
        $response->send();
182
        return true;
183
    }
184
}
185