Completed
Push — master ( a904dc...40c607 )
by Joachim
01:50
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 Psr\Http\Message\ResponseInterface as PsrResponseInterface;
6
use function Loevgaard\Consignor\ShipmentServer\decodeJson;
7
8
class Response implements ResponseInterface, \ArrayAccess
9
{
10
    /**
11
     * @var PsrResponseInterface
12
     */
13
    protected $response;
14
15
    /**
16
     * @var array
17
     */
18
    protected $data;
19
20
    /**
21
     * @param PsrResponseInterface $response
22
     * @throws InvalidJsonException
23
     */
24 1
    public function __construct(PsrResponseInterface $response)
25
    {
26 1
        $this->response = $response;
27 1
        $this->data = decodeJson((string)$this->response->getBody());
28 1
    }
29
30
    public function __toString()
31
    {
32
        return (string)$this->response->getBody();
33
    }
34
35
    public function wasSuccessful() : bool
36
    {
37
        return $this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300;
38
    }
39
40
    public function offsetExists($offset)
41
    {
42
        return isset($this->data[$offset]);
43
    }
44
45 1
    public function offsetGet($offset)
46
    {
47 1
        return $this->data[$offset] ?? null;
48
    }
49
50
    public function offsetSet($offset, $value)
51
    {
52
        throw new \BadMethodCallException('The response data can not be manipulated');
53
    }
54
55
    public function offsetUnset($offset)
56
    {
57
        throw new \BadMethodCallException('The response data can not be manipulated');
58
    }
59
}
60