Passed
Branch test-coverage (8dc675)
by Brad
12:43
created

JsonFixture   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A offsetExists() 0 4 1
A only() 0 4 1
A has() 0 4 1
A withBody() 0 5 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A get() 0 4 1
A asArray() 0 4 1
A offsetUnset() 0 4 1
A getBody() 0 4 1
A fromResponse() 0 4 1
A forget() 0 4 1
A set() 0 6 1
1
<?php
2
3
namespace Muzzle\Messages;
4
5
use ArrayAccess;
6
use GuzzleHttp\Psr7\Response;
7
use Illuminate\Support\Arr;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\StreamInterface;
10
use function GuzzleHttp\Psr7\stream_for;
11
12
class JsonFixture implements ResponseInterface, ArrayAccess
13
{
14
15
    use ResponseDecorator {
16
        __construct as initialize;
17
    }
18
19
    /**
20
     * @var array
21
     */
22
    private $body = [];
23
24
    /**
25
     * @param int $status                                Status code
26
     * @param array $headers                             Response headers
27
     * @param string|null|resource|StreamInterface $body Response body
28
     * @param string $version                            Protocol version
29
     * @param string|null $reason                        Reason phrase
30
     */
31
    public function __construct(
32
        $status = 200,
33
        array $headers = [],
34
        $body = null,
35
        $version = '1.1',
36
        $reason = null
37
    ) {
38
39
        $this->initialize(new Response($status, $headers, $body, $version, $reason));
40
        $this->withBody($this->response->getBody());
41
    }
42
43
    public static function fromResponse(ResponseInterface $response) : JsonFixture
44
    {
45
46
        return new static($response->getStatusCode(), $response->getHeaders(), $response->getBody());
47
    }
48
49
    public function getBody()
50
    {
51
52
        return stream_for(json_encode($this->body));
53
    }
54
55
    public function withBody(StreamInterface $body)
56
    {
57
58
        $this->body = json_decode($body, true);
59
        return $this;
60
    }
61
62
    public function asArray() : array
63
    {
64
65
        return $this->body;
66
    }
67
68
    public function has(string $key) : bool
69
    {
70
71
        return Arr::has($this->body, $key);
72
    }
73
74
    /**
75
     * @param string $key
76
     * @param mixed|callable $default
77
     * @return mixed
78
     */
79
    public function get(string $key, $default = null)
80
    {
81
82
        return Arr::get($this->body, $key, $default);
83
    }
84
85
    public function set(string $key, $value) : JsonFixture
86
    {
87
88
        Arr::set($this->body, $key, $value);
89
90
        return $this;
91
    }
92
93
    public function forget(string $key) : void
94
    {
95
96
        Arr::forget($this->body, $key);
97
    }
98
99
    public function only(array $keys) : array
100
    {
101
102
        return Arr::only($this->body, $keys);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function offsetExists($offset) : bool
109
    {
110
111
        return $this->has($offset);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function offsetGet($offset)
118
    {
119
120
        return $this->get($offset);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function offsetSet($offset, $value) : void
127
    {
128
129
        $this->set($offset, $value);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function offsetUnset($offset)
136
    {
137
138
        $this->forget($offset);
139
    }
140
}
141