PrettyJsonRenderer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 24
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A render() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use Override;
8
9
use function array_key_exists;
10
use function error_log;
11
use function json_encode;
12
use function json_last_error;
13
use function json_last_error_msg;
14
15
use const JSON_PRETTY_PRINT;
16
use const JSON_UNESCAPED_SLASHES;
17
use const PHP_EOL;
18
19
final class PrettyJsonRenderer implements RenderInterface
20
{
21
    /**
22
     * {@inheritDoc}
23
     */
24
    #[Override]
25
    public function render(ResourceObject $ro)
26
    {
27
        if (! array_key_exists('Content-Type', $ro->headers)) {
28
            $ro->headers['Content-Type'] = 'application/json';
29
        }
30
31
        $ro->view = (string) json_encode($ro, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL;
32
        $e = json_last_error();
33
        if ($e) {
34
            // @codeCoverageIgnoreStart
35
            error_log('json_encode error: ' . json_last_error_msg() . ' in ' . __METHOD__);
36
37
            return '';
38
39
            // @codeCoverageIgnoreEnd
40
        }
41
42
        return $ro->view;
43
    }
44
}
45