Total Complexity | 9 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Coverage | 85% |
Changes | 0 |
1 | <?php |
||
8 | class Response extends BaseResponse implements ResponseInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | protected $data; |
||
14 | |||
15 | 3 | public function __construct(string $content = '', int $status = 200, array $headers = array()) |
|
16 | { |
||
17 | 3 | parent::__construct($content, $status, $headers); |
|
18 | |||
19 | 3 | $this->data = $this->jsonToArray($content); |
|
20 | 3 | } |
|
21 | |||
22 | /** |
||
23 | * {@inheritDoc} |
||
24 | */ |
||
25 | 3 | public function getData() |
|
26 | { |
||
27 | 3 | return $this->data; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | 2 | public function setData($data) |
|
36 | 2 | } |
|
37 | |||
38 | 3 | protected function jsonToArray(string $content = null): ?array |
|
39 | { |
||
40 | 3 | if (empty($content)) { |
|
41 | return null; |
||
42 | } |
||
43 | |||
44 | 3 | $this->checkContentType(); |
|
45 | |||
46 | 3 | $data = @json_decode($content, true); |
|
47 | 3 | if ($data === null && json_last_error() !== JSON_ERROR_NONE) { |
|
48 | throw new \RuntimeException('It was not possible to convert the current content to JSON.'); |
||
49 | } |
||
50 | |||
51 | 3 | return $data; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Check if content type is json |
||
56 | */ |
||
57 | 3 | protected function checkContentType() |
|
61 | } |
||
62 | 3 | } |
|
64 |