Test Failed
Push — develop ( 2de2bf...7fb7cc )
by nguereza
02:31
created

ErrorHandlerMiddleware::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 28
rs 9.7666
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file ErrorHandlerMiddleware.php
34
 *
35
 *  The error handler middleware
36
 *
37
 *  @package    Platine\Framework\Http\Middleware
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Http\Middleware;
49
50
use ErrorException;
51
use Platine\Framework\Handler\Error\ErrorHandler;
52
use Platine\Framework\Handler\Error\ErrorHandlerInterface;
53
use Platine\Framework\Http\Exception\HttpException;
54
use Platine\Http\Handler\MiddlewareInterface;
55
use Platine\Http\Handler\RequestHandlerInterface;
56
use Platine\Http\ResponseInterface;
57
use Platine\Http\ServerRequestInterface;
58
use Platine\Logger\LoggerInterface;
59
use Throwable;
60
61
62
/**
63
 * class ErrorHandlerMiddleware
64
 * @package Platine\Framework\Http\Middleware
65
 */
66
class ErrorHandlerMiddleware implements MiddlewareInterface
67
{
68
69
    /**
70
     * The logger instance to use to save error
71
     * @var LoggerInterface
72
     */
73
    protected LoggerInterface $logger;
74
75
    /**
76
     * The default error handler to use
77
     * @var ErrorHandlerInterface
78
     */
79
    protected ErrorHandlerInterface $errorHandler;
80
81
    /**
82
     * Whether to show error details
83
     * @var bool
84
     */
85
    protected bool $detail = true;
86
87
    /**
88
     * List of error handler
89
     * @var array<string, ErrorHandlerInterface>
90
     */
91
    protected array $handlers = [];
92
93
    /**
94
     * List of sub classed error handlers
95
     * @var array<string, ErrorHandlerInterface>
96
     */
97
    protected array $subClassHandlers = [];
98
99
    /**
100
     * Create new instance
101
     * @param bool $detail
102
     * @param LoggerInterface $logger
103
     */
104
    public function __construct(bool $detail, LoggerInterface $logger)
105
    {
106
        $this->detail = $detail;
107
        $this->logger = $logger;
108
        $this->errorHandler = $this->getDefaultErrorHanlder();
109
    }
110
111
    /**
112
     * Set error handler
113
     * @param ErrorHandlerInterface $errorHandler
114
     * @return $this
115
     */
116
    public function setErrorHandler(ErrorHandlerInterface $errorHandler): self
117
    {
118
        $this->errorHandler = $errorHandler;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Add error handler for given type
125
     * @param string $type
126
     * @param ErrorHandlerInterface $handler
127
     * @param bool $useSubClasses
128
     * @return $this
129
     */
130
    public function addErrorHandler(
131
        string $type,
132
        ErrorHandlerInterface $handler,
133
        bool $useSubClasses = false
134
    ): self {
135
        if ($useSubClasses) {
136
            $this->subClassHandlers[$type] = $handler;
137
        } else {
138
            $this->handlers[$type] = $handler;
139
        }
140
141
        return $this;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function process(
148
        ServerRequestInterface $request,
149
        RequestHandlerInterface $handler
150
    ): ResponseInterface {
151
        set_error_handler(static function (
152
            int $severity,
153
            string $message,
154
            string $file,
155
            int $line
156
        ): bool {
157
            // https://www.php.net/manual/en/function.error-reporting.php#8866
158
            // Usages the defined levels of `error_reporting()`.
159
            if (!(error_reporting() & $severity)) {
160
                // This error code is not included in `error_reporting()`.
161
                return true;
162
            }
163
164
            throw new ErrorException($message, 0, $severity, $file, $line);
165
        });
166
167
        try {
168
            $response = $handler->handle($request);
169
        } catch (Throwable $exception) {
170
            $response = $this->handleException($request, $exception);
171
        }
172
173
        restore_error_handler();
174
        return $response;
175
    }
176
177
    /**
178
     * Handle exception
179
     * @param ServerRequestInterface $request
180
     * @param Throwable $exception
181
     * @return ResponseInterface
182
     */
183
    protected function handleException(
184
        ServerRequestInterface $request,
185
        Throwable $exception
186
    ): ResponseInterface {
187
        if ($exception instanceof HttpException) {
188
            $request = $exception->getRequest();
189
        }
190
191
        $type = get_class($exception);
192
193
        $handler = $this->getExceptionErrorHandler($type);
194
195
        return $handler->handle($request, $exception, $this->detail);
196
    }
197
198
    /**
199
     * Get the error handler for the given type
200
     * @param string $type
201
     * @return ErrorHandlerInterface
202
     */
203
    protected function getExceptionErrorHandler(string $type): ErrorHandlerInterface
204
    {
205
        if (isset($this->handlers[$type])) {
206
            return $this->handlers[$type];
207
        }
208
209
        if (isset($this->subClassHandlers[$type])) {
210
            return $this->subClassHandlers[$type];
211
        }
212
213
        foreach ($this->subClassHandlers as $class => $handler) {
214
            if (is_subclass_of($type, $class)) {
215
                return $handler;
216
            }
217
        }
218
219
        return $this->errorHandler;
220
    }
221
222
    /**
223
     * Return the default error handler if none is set before
224
     * @return ErrorHandlerInterface
225
     */
226
    protected function getDefaultErrorHanlder(): ErrorHandlerInterface
227
    {
228
        return new ErrorHandler($this->logger);
229
    }
230
}
231