Passed
Push — main ( a1a461...2097df )
by Thomas
12:50
created

HtmlErrorRenderer::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck\Renderer;
6
7
use Conia\Chuck\Error\Error;
8
use Conia\Chuck\Response;
9
use Conia\Chuck\ResponseFactory;
10
11
class HtmlErrorRenderer implements Renderer
12
{
13 8
    public function __construct(protected ResponseFactory $response)
14
    {
15 8
    }
16
17 7
    public function render(mixed $data, mixed ...$args): string
18
    {
19 7
        assert($data instanceof Error);
20
21 7
        $error = htmlspecialchars($data->error);
22 7
        $body = "<h1>{$error}</h1>";
23
24 7
        if ($data->debug) {
25 4
            $description = htmlspecialchars($data->description);
26 4
            $body .= "<h2>{$description}</h2>";
27
28 4
            $traceback = str_replace(
29 4
                ['<', '>', '"'],
30 4
                ['&lt;', '&gt', '&quot;'],
31 4
                $data->traceback
32 4
            );
33 4
            $traceback = implode('<br>#', explode('#', $traceback));
34 4
            $body .= preg_replace('/^<br>/', '', $traceback);
35
        }
36
37 7
        return $body;
38
    }
39
40 7
    public function response(mixed $data, mixed ...$args): Response
41
    {
42 7
        return $this->response->html($this->render($data));
43
    }
44
}
45