Exception   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 77
ccs 19
cts 19
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 declare(strict_types=1);
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 54
    public function __construct(
35
        int $status,
36
        string $message = null,
37
        \Exception $previous = null,
38
        array      $headers = [],
39
        int        $code = 0
40
    ) {
41 54
        $this->headers = $headers;
42 54
        $this->message = $message;
43 54
        $this->status  = $status;
44
45 54
        parent::__construct($message, $code, $previous);
46 54
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 36
    public function getStatusCode() : int
52
    {
53 36
        return $this->status;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 2
    public function getHeaders() : array
60
    {
61 2
        return $this->headers;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 42
    public function buildJsonResponse(ResponseInterface $response) : ResponseInterface
68
    {
69 42
        $this->headers['content-type'] = 'application/json';
70
71 42
        foreach ($this->headers as $key => $value) {
72 42
            $response = $response->withAddedHeader($key, $value);
73
        }
74
75 42
        if ($response->getBody()->isWritable()) {
76 42
            $response->getBody()->write(json_encode([
77 42
                'status_code'   => $this->status,
78 42
                'reason_phrase' => $this->message
79
            ]));
80
        }
81
82 42
        return $response->withStatus($this->status, $this->message);
83
    }
84
}
85