ProdVndErrorPage   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
eloc 12
dl 0
loc 33
rs 10
c 1
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 5 1
A __construct() 0 7 1
A getResponseBody() 0 8 2
A getHeader() 0 3 2
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getHeader($status->code) 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...
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
    /** @return array<string, string> */
36
    private function getHeader(int $code): array
37
    {
38
        return ['content-type' => $code >= 500 ? 'application/vnd.error+json' : 'application/json'];
39
    }
40
41
    /** @return array<string, string> */
42
    private function getResponseBody(Throwable $e, Status $status): array
43
    {
44
        $body = ['message' => $status->text];
45
        if ($status->code >= 500) {
46
            $body['logref'] = (string) new LogRef($e);
47
        }
48
49
        return $body;
50
    }
51
}
52