Response::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Raidros\Storer;
4
5
use GuzzleHttp\Psr7\Response as Psr7Response;
6
use Raidros\Storer\Exception\ResponseWithoutBody;
7
8
class Response
9
{
10
    protected $response;
11
    protected $transformer;
12
13
    /**
14
     * Api response.
15
     *
16
     * @param Psr7Response     $response
17
     * @param Transformer|null $transformer
18
     */
19 15
    public function __construct(Psr7Response $response, Transformer $transformer = null)
20
    {
21 15
        $this->response = $response;
22 15
        $this->transformer = $transformer;
23 15
    }
24
25
    /**
26
     * get the guzzle promisse.
27
     *
28
     * @return GuzzleHttp\Psr7\Response
29
     */
30 3
    public function promise()
31
    {
32 3
        return $this->response;
33
    }
34
35
    /**
36
     * Return a transformed response.
37
     *
38
     * @return array
39
     */
40 12
    public function getBody()
41
    {
42 12
        $body = json_decode($this->response->getBody(), true);
43
44 12
        if (!$body) {
45 3
            throw new ResponseWithoutBody('Response without body');
46
        }
47
48 9
        if (!$this->transformer) {
49 6
            return $body;
50
        }
51
52 3
        return $this->transformer->transformData($body);
53
    }
54
}
55