| Conditions | 4 |
| Paths | 4 |
| Total Lines | 21 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 20 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 17 | public function hydrate(ResponseInterface $response, string $class) |
||
| 18 | { |
||
| 19 | $body = (string) $response->getBody(); |
||
| 20 | |||
| 21 | if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) { |
||
| 22 | throw new HydrationException('The ModelHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type')); |
||
| 23 | } |
||
| 24 | |||
| 25 | try { |
||
| 26 | $data = Json::decode($body, true); |
||
| 27 | } catch (\JsonException $exception) { |
||
| 28 | throw new HydrationException(sprintf('Error when trying to decode the JSON response: %s', $exception->getMessage()), 0, $exception); |
||
| 29 | } |
||
| 30 | |||
| 31 | if (is_subclass_of($class, CreatableFromArray::class)) { |
||
| 32 | $object = $class::createFromArray($data); |
||
| 33 | } else { |
||
| 34 | $object = new $class($data); |
||
| 35 | } |
||
| 36 | |||
| 37 | return $object; |
||
| 38 | } |
||
| 40 |