DevVndErrorPage   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
eloc 16
dl 0
loc 35
rs 10
c 1
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseBody() 0 8 1
A __construct() 0 6 1
A toString() 0 8 1
A getHeader() 0 3 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 Override;
10
use Throwable;
11
12
use function assert;
13
use function json_encode;
14
use function sprintf;
15
16
use const JSON_PRETTY_PRINT;
17
use const JSON_UNESCAPED_SLASHES;
18
use const PHP_EOL;
19
20
final class DevVndErrorPage extends ResourceObject
21
{
22
    public function __construct(Throwable $e, RouterMatch $request)
23
    {
24
        $status = new Status($e);
25
        $this->code = $status->code;
26
        $this->headers = $this->getHeader();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getHeader() of type array<string,string> is incompatible with the declared type BEAR\Resource\Headers of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
        $this->body = $this->getResponseBody($e, $request, $status);
28
    }
29
30
    #[Override]
31
    public function toString(): string
32
    {
33
        $jsonEncoded = json_encode($this->body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
34
        assert($jsonEncoded !== false);
35
        $this->view = $jsonEncoded . PHP_EOL;
36
37
        return $this->view;
38
    }
39
40
    /** @return array<string, string> */
41
    private function getHeader(): array
42
    {
43
        return ['content-type' => 'application/vnd.error+json'];
44
    }
45
46
    /** @return array<string, string> */
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)', $e::class, $e->getMessage()),
54
            'file' => sprintf('%s(%s)', $e->getFile(), $e->getLine()),
55
        ];
56
    }
57
}
58