Test Failed
Push — develop ( acdfb2...2de2bf )
by nguereza
02:53
created

ErrorHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant PHP
7
 * 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 ErrorHandler.php
34
 *
35
 *  The default error handler class
36
 *
37
 *  @package    Platine\Framework\Handler\Error
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\Handler\Error;
49
50
use Platine\Framework\Handler\Error\Renderer\HtmlErrorRenderer;
51
use Platine\Framework\Handler\Error\Renderer\JsonErrorRenderer;
52
use Platine\Framework\Http\Exception\HttpException;
53
use Platine\Framework\Http\Exception\HttpMethodNotAllowedException;
54
use Platine\Http\Response;
55
use Platine\Http\ResponseInterface;
56
use Platine\Http\ServerRequestInterface;
57
use Platine\Logger\LoggerInterface;
58
use Throwable;
59
60
/**
61
 * class ErrorHandler
62
 * @package Platine\Framework\Handler\Error
63
 */
64
class ErrorHandler implements ErrorHandlerInterface
65
{
66
67
    /**
68
     * The content type
69
     * @var string
70
     */
71
    protected string $contentType = '';
72
73
    /**
74
     * The renderer to use to render error for the content
75
     * type
76
     * @var ErrorRenderInterface
77
     */
78
    protected ErrorRenderInterface $renderer;
79
80
    /**
81
     * List of well known content type with their renderer
82
     * @var array<string, ErrorRenderInterface>
83
     */
84
    protected array $renderes = [];
85
86
    /**
87
     * The request method that generate this error
88
     * @var string
89
     */
90
    protected string $method;
91
92
    /**
93
     * The request
94
     * @var ServerRequestInterface
95
     */
96
    protected ServerRequestInterface $request;
97
98
    /**
99
     * The exception that is thrown
100
     * @var Throwable
101
     */
102
    protected Throwable $exception;
103
104
    /**
105
     * The HTTP status code to use for this error
106
     * @var int
107
     */
108
    protected int $statusCode;
109
110
    /**
111
     * The logger instance to use to save error
112
     * @var LoggerInterface
113
     */
114
    protected LoggerInterface $logger;
115
116
    /**
117
     * Whether to show error details
118
     * @var bool
119
     */
120
    protected bool $detail = true;
121
122
    /**
123
     * Create new instance
124
     * @param LoggerInterface $logger
125
     */
126
    public function __construct(LoggerInterface $logger)
127
    {
128
        $this->logger = $logger;
129
130
        //Add default renderer
131
        $this->addDefaultsRenderer();
132
    }
133
134
    /**
135
     * Handle error and generate the response
136
     * @param ServerRequestInterface $request
137
     * @param Throwable $exception
138
     * @param bool $detail
139
     * @return ResponseInterface
140
     */
141
    public function handle(
142
        ServerRequestInterface $request,
143
        Throwable $exception,
144
        bool $detail
145
    ): ResponseInterface {
146
        $this->detail = $detail;
147
        $this->request = $request;
148
        $this->exception = $exception;
149
        $this->method = $request->getMethod();
150
        $this->statusCode = $this->determineStatusCode();
151
        if (empty($this->contentType)) {
152
            $this->contentType = $this->getRequestContentType($request);
153
        }
154
155
        $this->saveErrorToLog();
156
157
        return $this->errorResponse();
158
    }
159
160
    /**
161
     * Set the content type to be used
162
     * @param string $contentType
163
     * @return $this
164
     */
165
    public function setContentType(string $contentType): self
166
    {
167
        $this->contentType = $contentType;
168
169
        return $this;
170
    }
171
172
    /**
173
     * Set error renderer for the given content type
174
     * @param string $contentType
175
     * @param ErrorRenderInterface $renderer
176
     * @return $this
177
     */
178
    public function addErrorRenderer(
179
        string $contentType,
180
        ErrorRenderInterface $renderer
181
    ): self {
182
        $this->renderes[$contentType] = $renderer;
183
184
        return $this;
185
    }
186
187
    /**
188
     * Set default error renderer
189
     * @param string $contentType
190
     * @param ErrorRenderInterface $renderer
191
     * @return $this
192
     */
193
    public function setDefaultErrorRenderer(
194
        string $contentType,
195
        ErrorRenderInterface $renderer
196
    ): self {
197
        $this->contentType = $contentType;
198
        $this->renderer = $renderer;
199
200
        return $this;
201
    }
202
203
    /**
204
     * Determine the error status code to be used
205
     * @return int
206
     */
207
    protected function determineStatusCode(): int
208
    {
209
        if ($this->method === 'OPTIONS') {
210
            return 200;
211
        }
212
213
        if ($this->exception instanceof HttpException) {
214
            return $this->exception->getCode();
215
        }
216
217
        return 500;
218
    }
219
220
    /**
221
     * Determine the error renderer to be used
222
     * @return ErrorRenderInterface
223
     */
224
    protected function determineRenderer(): ErrorRenderInterface
225
    {
226
        if (
227
            !empty($this->contentType)
228
            && array_key_exists($this->contentType, $this->renderes)
229
        ) {
230
            $renderer = $this->renderes[$this->contentType];
231
        } else {
232
            $renderer = $this->renderer;
233
        }
234
235
        return $renderer;
236
    }
237
238
    /**
239
     * Determine the content type based on the request
240
     * @param ServerRequestInterface $request
241
     * @return string
242
     */
243
    protected function getRequestContentType(ServerRequestInterface $request): string
244
    {
245
        $header = $request->getHeaderLine('Accept');
246
        $selected = array_intersect(
247
            explode(', ', $header),
248
            array_keys($this->renderes)
249
        );
250
251
        $count = count($selected);
252
253
        if ($count > 0) {
254
            $current = current($selected);
255
256
            /**
257
             * Ensure other supported content types take precedence over text/plain
258
             * when multiple content types are provided via Accept header.
259
             */
260
            if ($current === 'text/plain' && $count > 1) {
261
                $next = next($selected);
262
                if (is_string($next)) {
263
                    return $next;
264
                }
265
            }
266
267
            if (is_string($current)) {
268
                return $current;
269
            }
270
        }
271
272
        $matches = [];
273
        if (preg_match('/\+(json|xml)/', $header, $matches)) {
274
            $contentType = 'application/' . $matches[1];
275
            if (array_key_exists($contentType, $this->renderes)) {
276
                return $contentType;
277
            }
278
        }
279
280
        return 'text/html';
281
    }
282
283
    /**
284
     * Log the error
285
     * @return void
286
     */
287
    protected function saveErrorToLog(): void
288
    {
289
        $renderer = $this->determineRenderer();
290
        $error = $renderer->render($this->exception, $this->detail, true);
291
292
        $this->logError($error);
293
    }
294
295
    /**
296
     * Log error message
297
     * @param string $error the error to log
298
     * @return void
299
     */
300
    protected function logError(string $error): void
301
    {
302
        $this->logger->error($error);
303
    }
304
305
    /**
306
     * Return the response generated for this error
307
     * @return ResponseInterface
308
     */
309
    protected function errorResponse(): ResponseInterface
310
    {
311
        $response = new Response($this->statusCode);
312
        if (
313
                !empty($this->contentType)
314
                && array_key_exists($this->contentType, $this->renderes)
315
        ) {
316
            $response = $response->withHeader('Content-Type', $this->contentType);
317
        }
318
319
        if ($this->exception instanceof HttpMethodNotAllowedException) {
320
            $allowMethods = implode(', ', $this->exception->getAllowedMethods());
0 ignored issues
show
Bug introduced by
The method getAllowedMethods() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as Platine\Framework\Http\E...thodNotAllowedException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

320
            $allowMethods = implode(', ', $this->exception->/** @scrutinizer ignore-call */ getAllowedMethods());
Loading history...
321
            $response = $response->withHeader('Allow', $allowMethods);
322
        }
323
324
        $renderer = $this->determineRenderer();
325
        $body = $renderer->render($this->exception, $this->detail, false);
326
327
        $response->getBody()->write($body);
328
329
        return $response;
330
    }
331
332
    /**
333
     * Add system defaults renderer
334
     * @return void
335
     */
336
    protected function addDefaultsRenderer(): void
337
    {
338
        $this->addErrorRenderer('text/html', new HtmlErrorRenderer());
339
        $this->addErrorRenderer('text/plain', new HtmlErrorRenderer());
340
        $this->addErrorRenderer('application/xml', new HtmlErrorRenderer());
341
        $this->addErrorRenderer('application/json', new JsonErrorRenderer());
342
    }
343
}
344