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