ArrayHydrator::hydrate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 3
Ratio 21.43 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 3
loc 14
ccs 0
cts 12
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
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\PhraseApp\Hydrator;
11
12
use FAPI\PhraseApp\Exception\HydrationException;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Hydrate an HTTP response to array.
17
 *
18
 * @author Sascha-Oliver Prolic <[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 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...
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