Completed
Push — 1.x ( ce34cb...ebc2f4 )
by Akihito
02:21
created

ProdVndErrorPage::toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package\Provide\Error;
8
9
use BEAR\Resource\ResourceObject;
10
use BEAR\Sunday\Extension\Router\RouterMatch;
11
12
final class ProdVndErrorPage extends ResourceObject
13
{
14
    public function __construct(\Exception $e, RouterMatch $request)
15
    {
16
        $status = new Status($e);
17
        $this->code = $status->code;
18
        $this->headers = $this->getHeader($status->code);
19
        $this->body = $this->getResponseBody($e, $request, $status);
20
    }
21
22
    public function toString()
23
    {
24
        $this->view = json_encode($this->body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    private function getHeader($code)
31
    {
32
        return ['content-type' => ($code >= 500) ? 'application/vnd.error+json' : 'application/json'];
33
    }
34
35
    /**
36
     * @param \Exception  $e
37
     * @param RouterMatch $request
38
     * @param Status      $status
39
     *
40
     * @return array
41
     */
42
    private function getResponseBody(\Exception $e, RouterMatch $request, Status $status)
43
    {
44
        unset($request);
45
        $body = ['message' => $status->text];
46
        if ($status->code >= 500) {
47
            $body['logref'] = (string) new LogRef($e);
48
        }
49
50
        return $body;
51
    }
52
}
53