Completed
Push — master ( 40c607...1c8f88 )
by Joachim
04:58
created

Response::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Loevgaard\Consignor\ShipmentServer\Response;
3
4
use Loevgaard\Consignor\ShipmentServer\Exception\InvalidJsonException;
5
use Loevgaard\Consignor\ShipmentServer\Request\RequestInterface;
6
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
7
use function Loevgaard\Consignor\ShipmentServer\decodeJson;
8
9
class Response implements ResponseInterface, \ArrayAccess
10
{
11
    /**
12
     * @var PsrResponseInterface
13
     */
14
    protected $response;
15
16
    /**
17
     * @var RequestInterface
18
     */
19
    protected $request;
20
21
    /**
22
     * @var array
23
     */
24
    protected $data;
25
26
    /**
27
     * @param PsrResponseInterface $response
28
     * @param RequestInterface $request
29
     * @throws InvalidJsonException
30
     */
31 1
    public function __construct(PsrResponseInterface $response, RequestInterface $request)
32
    {
33 1
        $this->response = $response;
34 1
        $this->request = $request;
35 1
        $this->data = decodeJson((string)$this->response->getBody());
36 1
    }
37
38
    public function __toString()
39
    {
40
        return (string)$this->response->getBody();
41
    }
42
43
    public function wasSuccessful() : bool
44
    {
45
        return !isset($this->data['ErrorMessages']) && $this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300;
46
    }
47
48
    public function offsetExists($offset)
49
    {
50
        return isset($this->data[$offset]);
51
    }
52
53 1
    public function offsetGet($offset)
54
    {
55 1
        return $this->data[$offset] ?? null;
56
    }
57
58
    public function offsetSet($offset, $value)
59
    {
60
        throw new \BadMethodCallException('The response data can not be manipulated');
61
    }
62
63
    public function offsetUnset($offset)
64
    {
65
        throw new \BadMethodCallException('The response data can not be manipulated');
66
    }
67
}
68