Completed
Push — master ( c13142...c0435c )
by Tobias
02:34
created

src/Hydrator/ArrayHydrator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
0 ignored issues
show
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