GuzzleResponse   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 17
dl 0
loc 47
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 3 1
A getBody() 0 3 1
A getStatusCode() 0 3 1
A __construct() 0 3 1
A toString() 0 6 1
A parseJson() 0 13 2
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