Completed
Push — master ( c44990...b4ec9a )
by Mikhail
01:07
created

ResponseTransformer::transform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace R3bers\BittrexApi\Message;
6
7
use Psr\Http\Message\ResponseInterface;
8
use R3bers\BittrexApi\Exception\TransformResponseException;
9
10
/**
11
 * Class ResponseTransformer
12
 * @package R3bers\BittrexApi\Message
13
 */
14
class ResponseTransformer
15
{
16
    /**
17
     * @param ResponseInterface $response
18
     * @return array
19
     * @throws TransformResponseException
20
     */
21
    public function transform(ResponseInterface $response): array
22
    {
23
        $body = (string) $response->getBody();
24
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) {
25
            $content = json_decode($body, true);
26
            if (JSON_ERROR_NONE === json_last_error()) {
27
                return $content;
28
            }
29
30
            throw new TransformResponseException('Error transforming response to array. JSON_ERROR: '
31
                . json_last_error());
32
        }
33
34
        throw new TransformResponseException('Error transforming response to array. Content-Type 
35
            is not application/json');
36
    }
37
}
38