JsonRenderer::render()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 20
rs 9.9332
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
final class JsonRenderer implements RenderInterface
16
{
17
    /**
18
     * {@inheritDoc}
19
     *
20
     * @psalm-taint-escape html
21
     */
22
    #[Override]
23
    public function render(ResourceObject $ro)
24
    {
25
        if (! array_key_exists('Content-Type', $ro->headers)) {
26
            $ro->headers['Content-Type'] = 'application/json';
27
        }
28
29
        $ro->view = (string) json_encode($ro);
30
        $e = json_last_error();
31
        if ($e) {
32
            // @codeCoverageIgnoreStart
33
            $msg = json_last_error_msg();
34
            error_log('json_encode error: ' . $msg . ' in ' . __METHOD__);
35
36
            return '';
37
38
            // @codeCoverageIgnoreEnd
39
        }
40
41
        return $ro->view;
42
    }
43
}
44