Completed
Push — develop ( 68a985...a1f82e )
by Adam
10s
created

ArrayHydrator::hydrate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 5
Ratio 26.32 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 5
loc 19
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
crap 3
1
<?php
2
3
namespace IBM\Watson\Common\Hydrator;
4
5
use IBM\Watson\Common\Exception\HydrationException;
6
use Psr\Http\Message\ResponseInterface;
7
8
final class ArrayHydrator implements HydratorInterface
9
{
10
    /**
11
     * Hydrate API response to array
12
     *
13
     * @param \Psr\Http\Message\ResponseInterface $response
14
     *
15
     * @return mixed
16
     * @throws \IBM\Watson\Common\Exception\HydrationException
17
     */
18 9
    public function hydrate(ResponseInterface $response, $class = null)
19
    {
20 9
        $body = $response->getBody()->__toString();
21 9 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...
22 3
            $message = 'The ArrayHydrator cannot hydrate response with Content-Type: ';
23
24 3
            throw new HydrationException($message . $response->getHeaderLine('Content-Type'));
25
        }
26
27 6
        $content = \json_decode($body, true);
28 6
        if (JSON_ERROR_NONE !== \json_last_error()) {
29 3
            throw new HydrationException(sprintf(
30 3
                'Error (%d) when trying to json_decode response',
31 3
                \json_last_error()
32 2
            ));
33
        }
34
35 3
        return $content;
36
    }
37
}
38