Exception   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 2

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 77
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getStatusCode() 0 4 1
A getHeaders() 0 4 1
A buildJsonResponse() 0 17 3
1
<?php
2
3
namespace League\Route\Http;
4
5
use League\Route\Http\Exception\HttpExceptionInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
class Exception extends \Exception implements HttpExceptionInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $headers = [];
14
15
    /**
16
     * @var string
17
     */
18
    protected $message;
19
20
    /**
21
     * @var integer
22
     */
23
    protected $status;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param integer    $status
29
     * @param string     $message
30
     * @param \Exception $previous
31
     * @param array      $headers
32
     * @param integer    $code
33
     */
34 69
    public function __construct(
35
        $status,
36
        $message             = null,
37
        \Exception $previous = null,
38
        array $headers       = [],
39
        $code                = 0
40
    ) {
41 69
        $this->headers = $headers;
42 69
        $this->message = $message;
43 69
        $this->status  = $status;
44
45 69
        parent::__construct($message, $code, $previous);
46 69
    }
47
48
    /**
49
     * [email protected]}
50
     */
51 54
    public function getStatusCode()
52
    {
53 54
        return $this->status;
54
    }
55
56
    /**
57
     * [email protected]}
58
     */
59 3
    public function getHeaders()
60
    {
61 3
        return $this->headers;
62
    }
63
64
    /**
65
     * [email protected]}
66
     */
67 63
    public function buildJsonResponse(ResponseInterface $response)
68
    {
69 63
        $this->headers['content-type'] = 'application/json';
70
71 63
        foreach ($this->headers as $key => $value) {
72 63
            $response = $response->withAddedHeader($key, $value);
73 63
        }
74
75 63
        if ($response->getBody()->isWritable()) {
76 63
            $response->getBody()->write(json_encode([
77 63
                'status_code'   => $this->status,
78 63
                'reason_phrase' => $this->message
79 63
            ]));
80 63
        }
81
82 63
        return $response->withStatus($this->status, $this->message);
83
    }
84
}
85