ArrayHydrator::hydrate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 3
Ratio 23.08 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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