Completed
Push — master ( 1c8f88...698168 )
by Joachim
01:33
created

Response::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
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 getErrors() : array
49
    {
50
        if(!isset($this->data['ErrorMessages'])) {
51
            return [];
52
        }
53
54
        return $this->data['ErrorMessages'];
55
    }
56
57
    public function offsetExists($offset)
58
    {
59
        return isset($this->data[$offset]);
60
    }
61
62 1
    public function offsetGet($offset)
63
    {
64 1
        return $this->data[$offset] ?? null;
65
    }
66
67
    public function offsetSet($offset, $value)
68
    {
69
        throw new \BadMethodCallException('The response data can not be manipulated');
70
    }
71
72
    public function offsetUnset($offset)
73
    {
74
        throw new \BadMethodCallException('The response data can not be manipulated');
75
    }
76
}
77