Passed
Push — feature/layer-contracts ( 1680b9 )
by Darío
03:04
created

SymfonyResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

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

6 Methods

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