ResponseTransformer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 24
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

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