GuzzleResponse::getBody()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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