Passed
Pull Request — develop (#30)
by Darío
04:39 queued 02:18
created

GuzzleResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 45
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 3 1
A getStatusCode() 0 3 1
A __construct() 0 3 1
A parseJson() 0 13 2
A getBody() 0 3 1
A toString() 0 6 1
1
<?php
2
3
namespace EasyHttp\GuzzleLayer;
4
5
use EasyHttp\LayerContracts\Contracts\HttpClientResponse;
6
use EasyHttp\LayerContracts\Exceptions\ImpossibleToParseJsonException;
7
use Psr\Http\Message\ResponseInterface;
8
9
class GuzzleResponse implements HttpClientResponse
10
{
11
    protected ResponseInterface $response;
12
13 11
    public function __construct(ResponseInterface $response)
14
    {
15 11
        $this->response = $response;
16 11
    }
17
18 5
    public function getStatusCode(): int
19
    {
20 5
        return $this->response->getStatusCode();
21
    }
22
23
    public function getHeaders(): array
24
    {
25
        return $this->response->getHeaders();
26
    }
27
28
    public function getBody(): string
29
    {
30
        return $this->toString();
31
    }
32
33 11
    public function parseJson(): array
34
    {
35 11
        $response = $this->toString();
36 11
        $data     = json_decode($response, true);
37
38 11
        if (! $data) {
39 2
            throw new ImpossibleToParseJsonException(
40
                'Service response could not be parsed to JSON, Response: ' .
41 2
                $response . ', Reason: ' . json_last_error()
42
            );
43
        }
44
45 9
        return $data;
46
    }
47
48 11
    private function toString(): string
49
    {
50 11
        $stream = $this->response->getBody();
51 11
        $stream->rewind();
52
53 11
        return $stream->getContents();
54
    }
55
}
56