Completed
Pull Request — master (#97)
by
unknown
03:44
created

DropboxRequest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 98.51%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 2
dl 0
loc 360
ccs 66
cts 67
cp 0.9851
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getMethod() 0 4 1
A setMethod() 0 6 1
A getAccessToken() 0 4 1
A setAccessToken() 0 6 1
A getEndpoint() 0 4 1
A setEndpoint() 0 6 1
A getEndpointType() 0 4 1
A setEndpointType() 0 6 1
A getContentType() 0 4 1
A getHeaders() 0 4 1
A setHeaders() 0 6 1
A getJsonBody() 0 4 1
A getParams() 0 4 1
A setParams() 0 11 1
A validateResponse() 0 4 1
A setContentType() 0 6 1
A getStreamBody() 0 4 1
A getFile() 0 4 1
A setFile() 0 6 1
A hasFile() 0 4 2
A processParams() 0 21 4
1
<?php
2
3
namespace Kunnu\Dropbox;
4
5
use Kunnu\Dropbox\Http\RequestBodyStream;
6
use Kunnu\Dropbox\Http\RequestBodyJsonEncoded;
7
8
/**
9
 * DropboxRequest
10
 */
11
class DropboxRequest
12
{
13
14
    /**
15
     * Access Token to use for this request
16
     *
17
     * @var string
18
     */
19
    protected $accessToken = null;
20
21
    /**
22
     * The HTTP method for this request
23
     *
24
     * @var string
25
     */
26
    protected $method = "GET";
27
28
    /**
29
     * The params for this request
30
     *
31
     * @var array
32
     */
33
    protected $params = null;
34
35
    /**
36
     * The Endpoint for this request
37
     *
38
     * @var string
39
     */
40
    protected $endpoint = null;
41
42
    /**
43
     * The Endpoint Type for this request
44
     *
45
     * @var string
46
     */
47
    protected $endpointType = null;
48
49
    /**
50
     * The headers to send with this request
51
     *
52
     * @var array
53
     */
54
    protected $headers = [];
55
56
    /**
57
     * File to upload
58
     *
59
     * @var \Kunnu\Dropbox\DropboxFile
60
     */
61
    protected $file = null;
62
63
    /**
64
     * Content Type for the Request
65
     *
66
     * @var string
67
     */
68
    protected $contentType = 'application/json';
69
70
    /**
71
     * If the Response needs to be validated
72
     * against being a valid JSON response.
73
     * Set this to false when an endpoint or
74
     * request has no return values.
75
     *
76
     * @var boolean
77
     */
78
    protected $validateResponse = true;
79
80
81
    /**
82
     * Create a new DropboxRequest instance
83
     *
84
     * @param string $method       HTTP Method of the Request
85
     * @param string $endpoint     API endpoint of the Request
86
     * @param string $accessToken  Access Token for the Request
87
     * @param string $endpointType Endpoint type ['api'|'content']
88
     * @param mixed  $params       Request Params
89
     * @param array  $headers      Headers to send along with the Request
90
     */
91 49
    public function __construct($method, $endpoint, $accessToken, $endpointType = "api", array $params = [], array $headers = [], $contentType = null)
92
    {
93 49
        $this->setMethod($method);
94 49
        $this->setEndpoint($endpoint);
95 49
        $this->setAccessToken($accessToken);
96 49
        $this->setEndpointType($endpointType);
97 49
        $this->setParams($params);
98 49
        $this->setHeaders($headers);
99
100 49
        if ($contentType) {
101
            $this->setContentType($contentType);
102
        }
103 49
    }
104
105
    /**
106
     * Get the Request Method
107
     *
108
     * @return string
109
     */
110 49
    public function getMethod()
111
    {
112 49
        return $this->method;
113
    }
114
115
    /**
116
     * Set the Request Method
117
     *
118
     * @param string
119
     *
120
     * @return \Kunnu\Dropbox\DropboxRequest
121
     */
122 49
    public function setMethod($method)
123
    {
124 49
        $this->method = $method;
125
126 49
        return $this;
127
    }
128
129
    /**
130
     * Get Access Token for the Request
131
     *
132
     * @return string
133
     */
134 49
    public function getAccessToken()
135
    {
136 49
        return $this->accessToken;
137
    }
138
139
    /**
140
     * Set Access Token for the Request
141
     *
142
     * @param string
143
     *
144
     * @return \Kunnu\Dropbox\DropboxRequest
145
     */
146 49
    public function setAccessToken($accessToken)
147
    {
148 49
        $this->accessToken = $accessToken;
149
150 49
        return $this;
151
    }
152
153
    /**
154
     * Get the Endpoint of the Request
155
     *
156
     * @return string
157
     */
158 49
    public function getEndpoint()
159
    {
160 49
        return $this->endpoint;
161
    }
162
163
    /**
164
     * Set the Endpoint of the Request
165
     *
166
     * @param string
167
     *
168
     * @return \Kunnu\Dropbox\DropboxRequest
169
     */
170 49
    public function setEndpoint($endpoint)
171
    {
172 49
        $this->endpoint = $endpoint;
173
174 49
        return $this;
175
    }
176
177
    /**
178
     * Get the Endpoint Type of the Request
179
     *
180
     * @return string
181
     */
182 49
    public function getEndpointType()
183
    {
184 49
        return $this->endpointType;
185
    }
186
187
    /**
188
     * Set the Endpoint Type of the Request
189
     *
190
     * @param string
191
     *
192
     * @return \Kunnu\Dropbox\DropboxRequest
193
     */
194 49
    public function setEndpointType($endpointType)
195
    {
196 49
        $this->endpointType = $endpointType;
197
198 49
        return $this;
199
    }
200
201
    /**
202
     * Get the Content Type of the Request
203
     *
204
     * @return string
205
     */
206 49
    public function getContentType()
207
    {
208 49
        return $this->contentType;
209
    }
210
211
    /**
212
     * Set the Content Type of the Request
213
     *
214
     * @param string
215
     *
216
     * @return \Kunnu\Dropbox\DropboxRequest
217
     */
218 12
    public function setContentType($contentType)
219
    {
220 12
        $this->contentType = $contentType;
221
222 12
        return $this;
223
    }
224
225
    /**
226
     * Get Request Headers
227
     *
228
     * @return array
229
     */
230 49
    public function getHeaders()
231
    {
232 49
        return $this->headers;
233
    }
234
235
    /**
236
     * Set Request Headers
237
     *
238
     * @param array
239
     *
240
     * @return \Kunnu\Dropbox\DropboxRequest
241
     */
242 49
    public function setHeaders(array $headers)
243
    {
244 49
        $this->headers = array_merge($this->headers, $headers);
245
246 49
        return $this;
247
    }
248
249
    /**
250
     * Get JSON Encoded Request Body
251
     *
252
     * @return \Kunnu\Dropbox\Http\RequestBodyJsonEncoded
253
     */
254 39
    public function getJsonBody()
255
    {
256 39
        return new RequestBodyJsonEncoded($this->getParams());
257
    }
258
259
    /**
260
     * Get the Request Params
261
     *
262
     * @return array
263
     */
264 49
    public function getParams()
265
    {
266 49
        return $this->params;
267
    }
268
269
    /**
270
     * Set the Request Params
271
     *
272
     * @param array
273
     *
274
     * @return \Kunnu\Dropbox\DropboxRequest
275
     */
276 49
    public function setParams(array $params = [])
277
    {
278
279
        //Process Params
280 49
        $params = $this->processParams($params);
281
282
        //Set the params
283 49
        $this->params = $params;
284
285 49
        return $this;
286
    }
287
288
    /**
289
     * Get Stream Request Body
290
     *
291
     * @return \Kunnu\Dropbox\Http\RequestBodyStream
292
     */
293 7
    public function getStreamBody()
294
    {
295 7
        return new RequestBodyStream($this->getFile());
296
    }
297
298
    /**
299
     * Get the File to be sent with the Request
300
     *
301
     * @return \Kunnu\Dropbox\DropboxFile
302
     */
303 7
    public function getFile()
304
    {
305 7
        return $this->file;
306
    }
307
308
    /**
309
     * Set the File to be sent with the Request
310
     *
311
     * @param \Kunnu\Dropbox\DropboxFile
312
     *
313
     * @return \Kunnu\Dropbox\DropboxRequest
314
     */
315 7
    public function setFile(DropboxFile $file)
316
    {
317 7
        $this->file = $file;
318
319 7
        return $this;
320
    }
321
322
    /**
323
     * Returns true if Request has file to be uploaded
324
     *
325
     * @return boolean
326
     */
327 10
    public function hasFile()
328
    {
329 10
        return !is_null($this->file) ? true : false;
330
    }
331
332
    /**
333
     * Whether to validate response or not
334
     *
335
     * @return boolean
336
     */
337 44
    public function validateResponse()
338
    {
339 44
        return $this->validateResponse;
340
    }
341
342
    /**
343
     * Process Params for the File parameter
344
     *
345
     * @param  array $params Request Params
346
     *
347
     * @return array
348
     */
349 49
    protected function processParams(array $params)
350
    {
351
        //If a file needs to be uploaded
352 49
        if (isset($params['file']) && $params['file'] instanceof DropboxFile) {
353
            //Set the file property
354 7
            $this->setFile($params['file']);
355
            //Remove the file item from the params array
356 7
            unset($params['file']);
357
        }
358
359
        //Whether the response needs to be validated
360
        //against being a valid JSON response
361 49
        if (isset($params['validateResponse'])) {
362
            //Set the validateResponse
363 2
            $this->validateResponse = $params['validateResponse'];
364
            //Remove the validateResponse from the params array
365 2
            unset($params['validateResponse']);
366
        }
367
368 49
        return $params;
369
    }
370
}
371