JsonErrorRenderer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 1
b 0
f 0
dl 0
loc 31
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A response() 0 7 1
A render() 0 16 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 JsonErrorRenderer implements Renderer
13
{
14 2
    public function __construct(protected Factory $factory)
15
    {
16 2
    }
17
18 1
    public function render(mixed $data, mixed ...$args): string
19
    {
20 1
        assert(is_array($data));
21 1
        assert(isset($data['error']));
22 1
        assert($data['error'] instanceof Error);
23
24 1
        $error = $data['error'];
25 1
        $json = [
26 1
            'error' => $error->error,
27 1
            'description' => $error->description,
28 1
            'traceback' => $error->traceback,
29 1
            'code' => $error->code,
30 1
            'payload' => $error->payload,
31 1
        ];
32
33 1
        return json_encode($json, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
34
    }
35
36 1
    public function response(mixed $data, mixed ...$args): Response
37
    {
38 1
        $response = Response::fromFactory($this->factory);
39 1
        $response->header('Content-Type', 'application/json');
40 1
        $response->body($this->render($data, ...$args));
41
42 1
        return $response;
43
    }
44
}
45