HtmlErrorRenderer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 18
c 1
b 0
f 0
dl 0
loc 32
ccs 22
cts 22
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A response() 0 3 1
A render() 0 20 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\Factory;
9
use Conia\Chuck\Response;
10
11
/** @psalm-api */
12
class HtmlErrorRenderer implements Renderer
13
{
14 5
    public function __construct(protected Factory $factory)
15
    {
16 5
    }
17
18 4
    public function render(mixed $data, mixed ...$args): string
19
    {
20 4
        assert(is_array($data));
21 4
        assert(isset($data['error']));
22 4
        assert($data['error'] instanceof Error);
23 4
        $error = $data['error'];
24
25 4
        $title = htmlspecialchars($error->error);
26
27 4
        return sprintf(
28 4
            '<!doctype html>' .
29 4
            '<html lang="en">' .
30 4
            '<head>' .
31 4
            '<meta charset="utf-8">' .
32 4
            '<meta name="viewport" content="width=device-width, initial-scale=1">' .
33 4
            '<title>%s</title>' .
34 4
            '</head>' .
35 4
            '<body><h1>%s</h1></body></html>',
36 4
            $title,
37 4
            $title
38 4
        );
39
    }
40
41 4
    public function response(mixed $data, mixed ...$args): Response
42
    {
43 4
        return Response::fromFactory($this->factory)->html($this->render($data));
44
    }
45
}
46