DevVndErrorPage::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
use function json_encode;
13
use function sprintf;
14
15
use const JSON_PRETTY_PRINT;
16
use const JSON_UNESCAPED_SLASHES;
17
use const PHP_EOL;
18
19
final class DevVndErrorPage extends ResourceObject
20
{
21
    public function __construct(Throwable $e, RouterMatch $request)
22
    {
23
        $status = new Status($e);
24
        $this->code = $status->code;
25
        $this->headers = $this->getHeader();
26
        $this->body = $this->getResponseBody($e, $request, $status);
27
    }
28
29
    public function toString(): string
30
    {
31
        $this->view = json_encode($this->body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
32
33
        return $this->view;
34
    }
35
36
    /**
37
     * @return array<string, string>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
38
     */
39
    private function getHeader(): array
40
    {
41
        return ['content-type' => 'application/vnd.error+json'];
42
    }
43
44
    /**
45
     * @return array<string, string>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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