AbstractHttpException::response()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Controlabs\Http\Exception;
4
5
use Controlabs\Http\ResponseMessage;
6
7
/**
8
 * Abstract HTTP exception class.
9
 */
10
abstract class AbstractHttpException extends \Exception implements HttpExceptionInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $data = [];
16
17
    /**
18
     * @var array
19
     */
20
    protected $headers = [];
21
22
    /**
23
     * @var string
24
     */
25
    protected $message = 'Internal Server Error';
26
27
    /**
28
     * @var integer
29
     */
30
    protected $statusCode = 500;
31
32
    /**
33
     * Constructor
34
     *
35
     * @param string        $message
36
     * @param array         $data
37
     * @param \Throwable    $previous
38
     * @param array         $headers
39
     */
40 6
    public function __construct(
41
        string $message = '',
42
        array $data = [],
43
        \Throwable $previous = null,
44
        array $headers = []
45
    ) {
46 6
        ($message) && $this->message = $message;
47 6
        parent::__construct($this->message, 0, $previous);
48 6
        $this->data = $data;
49 6
        $this->headers = $headers;
50 6
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 3
    public function statusCode() : int
56
    {
57 3
        return $this->statusCode;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function data() : array
64
    {
65 2
        return $this->data;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function headers() : array
72
    {
73 1
        return $this->headers;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 3
    public function message() : ?string
80
    {
81 3
        return $this->getMessage();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function response() : ResponseMessage
88
    {
89 1
        return new ResponseMessage($this->statusCode(), $this->message(), $this->data());
90
    }
91
}
92