Completed
Push — 1.x ( c86fd3...4b8bd5 )
by Akihito
14s queued 12s
created

DevVndErrorPage::getHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Error;
6
7
use BEAR\Resource\ResourceObject;
8
use BEAR\Sunday\Extension\Router\RouterMatch;
9
use Throwable;
10
11
use function get_class;
12 1
use function json_encode;
13
use function sprintf;
14 1
15 1
use const JSON_PRETTY_PRINT;
16 1
use const JSON_UNESCAPED_SLASHES;
17 1
use const PHP_EOL;
18 1
19
final class DevVndErrorPage extends ResourceObject
20 1
{
21
    public function __construct(Throwable $e, RouterMatch $request)
22 1
    {
23
        $status = new Status($e);
24 1
        $this->code = $status->code;
25
        $this->headers = $this->getHeader();
26
        $this->body = $this->getResponseBody($e, $request, $status);
27 1
    }
28
29 1
    public function toString(): string
30
    {
31
        $this->view = json_encode($this->body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
32 1
33
        return $this->view;
34
    }
35 1
36 1
    /**
37 1
     * @return array<string, string>
38 1
     */
39 1
    private function getHeader(): array
40
    {
41
        return ['content-type' => 'application/vnd.error+json'];
42
    }
43
44
    /**
45
     * @return array<string, string>
46
     */
47
    private function getResponseBody(Throwable $e, RouterMatch $request, Status $status): array
48
    {
49
        return [
50
            'message' => $status->text,
51
            'logref' => (string) new LogRef($e),
52
            'request' => (string) $request,
53
            'exceptions' => sprintf('%s(%s)', get_class($e), $e->getMessage()),
54
            'file' => sprintf('%s(%s)', $e->getFile(), $e->getLine()),
55
        ];
56
    }
57
}
58