ArrayHydrator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 11
cp 0
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace FAPI\Localise\Hydrator;
11
12
use FAPI\Localise\Exception\HydrationException;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Hydrate an HTTP response to array.
17
 *
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
final class ArrayHydrator implements Hydrator
21
{
22
    /**
23
     * @param ResponseInterface $response
24
     * @param string            $class
25
     *
26
     * @return array
27
     */
28
    public function hydrate(ResponseInterface $response, string $class): array
29
    {
30
        $body = $response->getBody()->__toString();
31
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
32
            throw new HydrationException('The ArrayHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type'));
33
        }
34
35
        $content = json_decode($body, true);
36
        if (JSON_ERROR_NONE !== json_last_error()) {
37
            throw new HydrationException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
38
        }
39
40
        return $content;
41
    }
42
}
43