ArrayHydrator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 29
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 19 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace AdamPaterson\ApiClient\Foundation\Hydrator;
6
7
use AdamPaterson\ApiClient\Foundation\Contract\HydratorInterface as HydratorContract;
8
use AdamPaterson\ApiClient\Foundation\Exception\HydrationException;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Class ArrayHydrator
13
 *
14
 * @package AdamPaterson\ApiClient\Foundation\Hydrator
15
 */
16
final class ArrayHydrator implements HydratorContract
17
{
18
    /**
19
     * Hydrate ResponseInterface to Array
20
     *
21
     * @param \Psr\Http\Message\ResponseInterface $response
22
     *
23
     * @return mixed
24
     */
25 9
    public function hydrate(ResponseInterface $response)
26
    {
27 9
        $body = $response->getBody()->__toString();
28
29 9
        $contentType = $response->getHeaderLine('Content-Type');
30 9
        if (strpos($contentType, 'application/json') !== 0) {
31 3
            throw new HydrationException('The ArrayHydrator cannot hydrate response with Content-Type:'.$contentType);
32
        }
33
34 6
        $content = json_decode($body, true);
35
36 6
        if (JSON_ERROR_NONE !== json_last_error()) {
37 3
            throw new HydrationException(
38 3
                sprintf('Error (%d) when trying to json_decode response', json_last_error())
39
            );
40
        }
41
42 3
        return $content;
43
    }
44
}
45