Completed
Push — code201 ( 677523...68154a )
by Akihito
05:57
created

AbstractRequest::invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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 78
     * @param array                $query
99
     * @param array                $links
100
     * @param LinkerInterface|null $linker
101
     *
102
     * @throws MethodException
103
     */
104
    public function __construct(
105
        InvokerInterface $invoker,
106 78
        ResourceObject $ro = null,
107 78
        $method = Request::GET,
108 78
        array $query = [],
109 1
        array $links = [],
110
        LinkerInterface $linker = null
111 78
    ) {
112 78
        $this->invoker = $invoker;
113 78
        $this->resourceObject = $ro;
114 78
        if (! in_array(strtolower($method), [self::GET, self::POST, self::PUT, self::PATCH, self::DELETE, self::HEAD, self::OPTIONS], true)) {
115 78
            throw new MethodException($method, 400);
116
        }
117 6
        $this->method = $method;
118
        $this->query = $query;
119
        $this->links = $links;
120 6
        $this->linker = $linker;
121
    }
122 5
123 1
    public function __toString()
124 1
    {
125
        try {
126 1
            $this->invoke();
127
128
            return (string) $this->result;
129
        } catch (\Exception $e) {
130
            error_log($e);
131
132
            return '';
133 44
        }
134
    }
135 44
136 6
    /**
137
     * {@inheritdoc}
138 44
     */
139
    public function __invoke(array $query = null)
140
    {
141
        if ($query !== null) {
142 44
            $this->query = array_merge($this->query, $query);
143
        }
144
        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
            return $this->linker->invoke($this);
146
        }
147
148
        return $this->invoker->invoke($this);
149
    }
150 1
151
    /**
152 1
     * {@inheritdoc}
153
     */
154
    public function __get($name)
155
    {
156
        $this->result = $this->invoke();
157
158
        return $this->result->$name;
159
    }
160 1
161
    /**
162 1
     *{@inheritdoc}
163 1
     *
164
     * @throws OutOfBoundsException
165
     */
166
    public function offsetSet($offset, $value)
167
    {
168
        throw new OutOfBoundsException(__METHOD__ . ' is unavailable.', 400);
169 26
    }
170
171 26
    /**
172 19
     * {@inheritdoc}
173
     *
174 17
     * @throws OutOfBoundsException
175
     */
176
    public function offsetUnset($offset)
177 8
    {
178
        unset($offset);
179
        throw new OutOfBoundsException(__METHOD__ . ' is unavailable.', 400);
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 2
    public function request()
186
    {
187 2
        if ($this->in == 'eager') {
188 2
            $this->result = $this->invoke();
189 1
190
            return $this->result;
191
        }
192 1
193
        return $this;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198 2
     *
199
     * @throws OutOfBoundsException
200 2
     */
201
    public function offsetGet($offset)
202 2
    {
203
        $this->invoke();
204
        if (! isset($this->result->body[$offset])) {
205
            throw new OutOfBoundsException("[$offset] for object[" . get_class($this->result) . ']', 400);
206
        }
207
208 1
        return $this->result->body[$offset];
209
    }
210 1
211 1
    /**
212 1
     * {@inheritdoc}
213
     */
214 1
    public function offsetExists($offset)
215
    {
216
        $this->invoke();
217
218
        return isset($this->result->body[$offset]);
219
    }
220 3
221
    /**
222 3
     * {@inheritdoc}
223
     */
224
    public function getIterator()
225
    {
226
        $this->invoke();
227
        $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 1
    public function hash()
237
    {
238 1
        return md5(get_class($this->resourceObject) . $this->method . serialize($this->query) . serialize($this->links));
239
    }
240
241 31
    /**
242
     * {@inheritdoc}
243 31
     */
244
    public function serialize()
245 31
    {
246
        return serialize($this->invoke());
247
    }
248 28
249
    /**
250
     * {@inheritdoc}
251
     */
252
    public function unserialize($serialized)
253
    {
254
        return unserialize($serialized);
255
    }
256
257
    private function invoke() : ResourceObject
258
    {
259
        if ($this->result === null) {
260
            /* @noinspection ImplicitMagicMethodCallInspection */
261
            $this->result = $this->__invoke();
262
        }
263
264
        return $this->result;
265
    }
266
}
267