Conditions | 5 |
Paths | 4 |
Total Lines | 21 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 30 |
Changes | 0 |
1 | <?php |
||
29 | public function deserialize(ResponseInterface $response, $class) |
||
30 | { |
||
31 | $body = $response->getBody()->__toString(); |
||
32 | $contentType = $response->getHeaderLine('Content-Type'); |
||
33 | if (strpos($contentType, 'application/json') !== 0 && strpos($contentType, 'application/octet-stream') !== 0) { |
||
34 | throw new DeserializeException('The ModelHydrator cannot hydrate response with Content-Type: '.$contentType); |
||
35 | } |
||
36 | |||
37 | $data = json_decode($body, true); |
||
38 | if (JSON_ERROR_NONE !== json_last_error()) { |
||
39 | throw new DeserializeException(sprintf('Error (%d) when trying to json_decode response', json_last_error())); |
||
40 | } |
||
41 | |||
42 | if (is_subclass_of($class, ApiResponse::class)) { |
||
|
|||
43 | $object = call_user_func($class.'::create', $data); |
||
44 | } else { |
||
45 | $object = new $class($data); |
||
46 | } |
||
47 | |||
48 | return $object; |
||
49 | } |
||
50 | } |
||
51 |