Passed
Push — master ( 9eeeb5...ff15f0 )
by Radu
02:55
created

AbstractApplication::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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