XmlRenderer::renderVerbose()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 12
ccs 11
cts 11
cp 1
rs 9.9332
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ErrorHandler\Renderer;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Throwable;
9
use Yiisoft\ErrorHandler\ErrorData;
10
use Yiisoft\ErrorHandler\ThrowableRendererInterface;
11
12
use function str_replace;
13
14
/**
15
 * Formats throwable into XML string.
16
 */
17
final class XmlRenderer implements ThrowableRendererInterface
18
{
19 1
    public function render(Throwable $t, ServerRequestInterface $request = null): ErrorData
20
    {
21 1
        $content = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
22 1
        $content .= "\n<error>\n";
23 1
        $content .= $this->tag('message', self::DEFAULT_ERROR_MESSAGE);
24 1
        $content .= '</error>';
25 1
        return new ErrorData($content);
26
    }
27
28 1
    public function renderVerbose(Throwable $t, ServerRequestInterface $request = null): ErrorData
29
    {
30 1
        $content = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
31 1
        $content .= "\n<error>\n";
32 1
        $content .= $this->tag('type', $t::class);
33 1
        $content .= $this->tag('message', $this->cdata($t->getMessage()));
34 1
        $content .= $this->tag('code', $this->cdata((string) $t->getCode()));
35 1
        $content .= $this->tag('file', $t->getFile());
36 1
        $content .= $this->tag('line', (string) $t->getLine());
37 1
        $content .= $this->tag('trace', $t->getTraceAsString());
38 1
        $content .= '</error>';
39 1
        return new ErrorData($content);
40
    }
41
42 2
    private function tag(string $name, string $value): string
43
    {
44 2
        return "<$name>" . $value . "</$name>\n";
45
    }
46
47 1
    private function cdata(string $value): string
48
    {
49 1
        return '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $value) . ']]>';
50
    }
51
}
52