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

GuzzleResponse::getBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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