Completed
Push — master ( ff5b0d...faa513 )
by Tobias
05:10 queued 02:50
created

ArrayHydrator::hydrate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 3
Ratio 21.43 %

Importance

Changes 0
Metric Value
dl 3
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
namespace Happyr\ApiClient\Hydrator;
4
5
use Happyr\ApiClient\Exception\HydrationException;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Hydrate an HTTP response to array.
10
 *
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13
final class ArrayHydrator implements Hydrator
14
{
15
    /**
16
     * @param ResponseInterface $response
17
     * @param string            $class
18
     *
19
     * @return array
20
     */
21
    public function hydrate(ResponseInterface $response, $class)
22
    {
23
        $body = $response->getBody()->__toString();
24 View Code Duplication
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
            throw new HydrationException('The ArrayHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type'));
26
        }
27
28
        $content = json_decode($body, true);
29
        if (JSON_ERROR_NONE !== json_last_error()) {
30
            throw new HydrationException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
31
        }
32
33
        return $content;
34
    }
35
}
36