ProdVndErrorPage::getHeader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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 json_encode;
12
13
use const JSON_PRETTY_PRINT;
14
use const JSON_UNESCAPED_SLASHES;
15
use const PHP_EOL;
16
17
final class ProdVndErrorPage extends ResourceObject
18
{
19
    public function __construct(Throwable $e, RouterMatch $request)
20
    {
21
        unset($request);
22
        $status = new Status($e);
23
        $this->code = $status->code;
24
        $this->headers = $this->getHeader($status->code);
25
        $this->body = $this->getResponseBody($e, $status);
26
    }
27
28
    public function toString(): string
29
    {
30
        $this->view = json_encode($this->body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
31
32
        return $this->view;
33
    }
34
35
    /**
36
     * @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...
37
     */
38
    private function getHeader(int $code): array
39
    {
40
        return ['content-type' => $code >= 500 ? 'application/vnd.error+json' : 'application/json'];
41
    }
42
43
    /**
44
     * @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...
45
     */
46
    private function getResponseBody(Throwable $e, Status $status): array
47
    {
48
        $body = ['message' => $status->text];
49
        if ($status->code >= 500) {
50
            $body['logref'] = (string) new LogRef($e);
51
        }
52
53
        return $body;
54
    }
55
}
56