| Total Complexity | 9 |
| Total Lines | 53 |
| Duplicated Lines | 0 % |
| Coverage | 95% |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | class Response extends BaseResponse implements ResponseInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | protected $data; |
||
| 14 | |||
| 15 | 10 | public function __construct(string $content = '', int $status = 200, array $headers = array()) |
|
| 16 | { |
||
| 17 | 10 | parent::__construct($content, $status, $headers); |
|
| 18 | |||
| 19 | 10 | $this->data = $this->jsonToArray($content); |
|
| 20 | 9 | } |
|
| 21 | |||
| 22 | /** |
||
| 23 | * {@inheritDoc} |
||
| 24 | */ |
||
| 25 | 8 | public function getData() |
|
| 26 | { |
||
| 27 | 8 | return $this->data; |
|
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritdoc} |
||
| 32 | */ |
||
| 33 | 5 | public function setData($data) |
|
| 36 | 5 | } |
|
| 37 | |||
| 38 | 10 | protected function jsonToArray(string $content = null): ?array |
|
| 39 | { |
||
| 40 | 10 | if (empty($content)) { |
|
| 41 | 1 | return null; |
|
| 42 | } |
||
| 43 | |||
| 44 | 9 | $this->checkContentType(); |
|
| 45 | |||
| 46 | 8 | $data = @json_decode($content, true); |
|
| 47 | 8 | 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 | 8 | return $data; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Check if content type is json |
||
| 56 | */ |
||
| 57 | 9 | protected function checkContentType() |
|
| 61 | } |
||
| 62 | 8 | } |
|
| 64 |