Passed
Push — master ( 8b7547...d71e56 )
by Alexander
02:06
created

XmlRenderer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
c 0
b 0
f 0
dl 0
loc 33
ccs 21
cts 21
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cdata() 0 3 1
A tag() 0 3 1
A renderVerbose() 0 12 1
A render() 0 7 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 get_class;
13
use function str_replace;
14
15
/**
16
 * Formats throwable into XML string.
17
 */
18
final class XmlRenderer implements ThrowableRendererInterface
19
{
20 1
    public function render(Throwable $t, ServerRequestInterface $request = null): ErrorData
21
    {
22 1
        $content = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
23 1
        $content .= "\n<error>\n";
24 1
        $content .= $this->tag('message', self::DEFAULT_ERROR_MESSAGE);
25 1
        $content .= '</error>';
26 1
        return new ErrorData($content);
27
    }
28
29 1
    public function renderVerbose(Throwable $t, ServerRequestInterface $request = null): ErrorData
30
    {
31 1
        $content = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
32 1
        $content .= "\n<error>\n";
33 1
        $content .= $this->tag('type', get_class($t));
34 1
        $content .= $this->tag('message', $this->cdata($t->getMessage()));
35 1
        $content .= $this->tag('code', $this->cdata((string) $t->getCode()));
36 1
        $content .= $this->tag('file', $t->getFile());
37 1
        $content .= $this->tag('line', (string) $t->getLine());
38 1
        $content .= $this->tag('trace', $t->getTraceAsString());
39 1
        $content .= '</error>';
40 1
        return new ErrorData($content);
41
    }
42
43 2
    private function tag(string $name, string $value): string
44
    {
45 2
        return "<$name>" . $value . "</$name>\n";
46
    }
47
48 1
    private function cdata(string $value): string
49
    {
50 1
        return '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $value) . ']]>';
51
    }
52
}
53