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

ProdVndErrorPage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
eloc 12
c 2
b 1
f 0
dl 0
loc 37
ccs 13
cts 13
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getResponseBody() 0 8 2
A toString() 0 5 1
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 2
13
use const JSON_PRETTY_PRINT;
14 2
use const JSON_UNESCAPED_SLASHES;
15 2
use const PHP_EOL;
16 2
17 2
final class ProdVndErrorPage extends ResourceObject
18 2
{
19
    public function __construct(Throwable $e, RouterMatch $request)
20 2
    {
21
        unset($request);
22 2
        $status = new Status($e);
23
        $this->code = $status->code;
24 2
        $this->headers = $this->getHeader($status->code);
25
        $this->body = $this->getResponseBody($e, $status);
26
    }
27 2
28
    public function toString(): string
29 2
    {
30
        $this->view = json_encode($this->body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
31
32 2
        return $this->view;
33
    }
34 2
35 2
    /**
36 2
     * @return array<string, string>
37 2
     */
38
    private function getHeader(int $code): array
39
    {
40 2
        return ['content-type' => $code >= 500 ? 'application/vnd.error+json' : 'application/json'];
41
    }
42
43
    /**
44
     * @return array<string, string>
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