Completed
Push — code201 ( 677523...68154a )
by Akihito
08:44 queued 05:59
created

AbstractRequest::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the BEAR.Resource package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Resource;
8
9
use BEAR\Resource\Exception\MethodException;
10
use BEAR\Resource\Exception\OutOfBoundsException;
11
12
/**
13
 * @property string code
14
 * @property array headers
15
 * @property mixed body
16
 * @property string view
17
 */
18
abstract class AbstractRequest implements RequestInterface, \ArrayAccess, \IteratorAggregate, \Serializable
19
{
20
    const GET = 'get';
21
    const POST = 'post';
22
    const PUT = 'put';
23
    const PATCH = 'patch';
24
    const DELETE = 'delete';
25
    const HEAD = 'head';
26
    const OPTIONS = 'options';
27
28
    /**
29
     * URI
30
     *
31
     * @var string
32
     */
33
    public $uri;
34
35
    /**
36
     * Resource object
37
     *
38
     * @var \BEAR\Resource\ResourceObject
39
     */
40
    public $resourceObject;
41
42
    /**
43
     * Method
44
     *
45
     * @var string
46
     */
47
    public $method = '';
48
49
    /**
50
     * Query
51
     *
52
     * @var array
53
     */
54
    public $query = [];
55
56
    /**
57
     * Options
58
     *
59
     * @var array
60
     */
61
    public $options = [];
62
63
    /**
64
     * Request option (eager or lazy)
65
     *
66
     * @var string
67
     */
68
    public $in;
69
70
    /**
71
     * Links
72
     *
73
     * @var \BEAR\Resource\LinkType[]
74
     */
75
    public $links = [];
76
77
    /**
78
     * Request Result
79
     *
80
     * @var ResourceObject
81
     */
82
    protected $result;
83
84
    /**
85
     * @var InvokerInterface
86
     */
87
    protected $invoker;
88
89
    /**
90
     * @var LinkerInterface
91
     */
92
    private $linker;
93
94
    /**
95
     * @param InvokerInterface     $invoker
96
     * @param ResourceObject|null  $ro
97
     * @param string               $method
98
     * @param array                $query
99
     * @param array                $links
100
     * @param LinkerInterface|null $linker
101
     *
102
     * @throws MethodException
103
     */
104 83
    public function __construct(
105
        InvokerInterface $invoker,
106
        ResourceObject $ro = null,
107
        $method = Request::GET,
108
        array $query = [],
109
        array $links = [],
110
        LinkerInterface $linker = null
111
    ) {
112 83
        $this->invoker = $invoker;
113 83
        $this->resourceObject = $ro;
114 83
        if (! in_array(strtolower($method), [self::GET, self::POST, self::PUT, self::PATCH, self::DELETE, self::HEAD, self::OPTIONS], true)) {
115 1
            throw new MethodException($method, 400);
116
        }
117 83
        $this->method = $method;
118 83
        $this->query = $query;
119 83
        $this->links = $links;
120 83
        $this->linker = $linker;
121 83
    }
122
123 6
    public function __toString()
124
    {
125
        try {
126 6
            $this->invoke();
127
128 5
            return (string) $this->result;
129 1
        } catch (\Exception $e) {
130 1
            error_log($e);
131
132 1
            return '';
133
        }
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 49
    public function __invoke(array $query = null)
140
    {
141 49
        if ($query !== null) {
142 6
            $this->query = array_merge($this->query, $query);
143
        }
144 49
        if ($this->links) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->links of type BEAR\Resource\LinkType[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145 3
            return $this->linker->invoke($this);
146
        }
147
148 46
        return $this->invoker->invoke($this);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 3
    public function __get($name)
155
    {
156 3
        $this->result = $this->invoke();
157
158 3
        return $this->result->$name;
159
    }
160
161
    /**
162
     *{@inheritdoc}
163
     *
164
     * @throws OutOfBoundsException
165
     */
166 1
    public function offsetSet($offset, $value)
167
    {
168 1
        throw new OutOfBoundsException(__METHOD__ . ' is unavailable.', 400);
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     *
174
     * @throws OutOfBoundsException
175
     */
176 1
    public function offsetUnset($offset)
177
    {
178 1
        unset($offset);
179 1
        throw new OutOfBoundsException(__METHOD__ . ' is unavailable.', 400);
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 26
    public function request()
186
    {
187 26
        if ($this->in == 'eager') {
188 19
            $this->result = $this->invoke();
189
190 17
            return $this->result;
191
        }
192
193 8
        return $this;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     *
199
     * @throws OutOfBoundsException
200
     */
201 2
    public function offsetGet($offset)
202
    {
203 2
        $this->invoke();
204 2
        if (! isset($this->result->body[$offset])) {
205 1
            throw new OutOfBoundsException("[$offset] for object[" . get_class($this->result) . ']', 400);
206
        }
207
208 1
        return $this->result->body[$offset];
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214 2
    public function offsetExists($offset)
215
    {
216 2
        $this->invoke();
217
218 2
        return isset($this->result->body[$offset]);
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224 1
    public function getIterator()
225
    {
226 1
        $this->invoke();
227 1
        $isArray = (is_array($this->result->body) || $this->result->body instanceof \Traversable);
228 1
        $iterator = $isArray ? new \ArrayIterator($this->result->body) : new \ArrayIterator([]);
229
230 1
        return $iterator;
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236 4
    public function hash()
237
    {
238 4
        return md5(get_class($this->resourceObject) . $this->method . serialize($this->query) . serialize($this->links));
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244 1
    public function serialize()
245
    {
246 1
        return serialize($this->invoke());
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252 1
    public function unserialize($serialized)
253
    {
254 1
        return unserialize($serialized);
255
    }
256
257 34
    private function invoke() : ResourceObject
258
    {
259 34
        if ($this->result === null) {
260
            /* @noinspection ImplicitMagicMethodCallInspection */
261 33
            $this->result = $this->__invoke();
262
        }
263
264 31
        return $this->result;
265
    }
266
}
267