GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0baec8...65564e )
by Jonny
04:04
created

AbstractRequest   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 462
Duplicated Lines 0 %

Coupling/Cohesion

Components 6
Dependencies 1

Test Coverage

Coverage 98.02%

Importance

Changes 0
Metric Value
wmc 36
lcom 6
cbo 1
dl 0
loc 462
ccs 99
cts 101
cp 0.9802
rs 8
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A setMethod() 0 13 2
A getMethod() 0 4 1
A setTimeout() 0 6 1
A getTimeout() 0 8 2
A setDelay() 0 6 1
A getDelay() 0 4 1
A setViewportSize() 0 7 1
A getViewportWidth() 0 4 1
A getViewportHeight() 0 4 1
A setUrl() 0 6 1
A getUrl() 0 16 4
A getBody() 0 8 2
A setRequestData() 0 6 1
A getRequestData() 0 8 2
A setHeaders() 0 4 1
A addHeader() 0 6 1
A addHeaders() 0 6 1
A getHeaders() 0 8 2
A addSetting() 0 6 1
A getSettings() 0 4 1
A setBodyStyles() 0 6 1
A getBodyStyles() 0 8 2
A flattenData() 0 19 3
1
<?php
2
3
/*
4
 * This file is part of the php-phantomjs.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JonnyW\PhantomJs\Http;
11
12
use JonnyW\PhantomJs\Exception\InvalidMethodException;
13
use JonnyW\PhantomJs\Procedure\InputInterface;
14
15
/**
16
 * PHP PhantomJs
17
 *
18
 * @author Jon Wenmoth <[email protected]>
19
 */
20
abstract class AbstractRequest
21
    implements RequestInterface, InputInterface
22
{
23
    /**
24
     * Headers
25
     *
26
     * @var array
27
     * @access protected
28
     */
29
    protected $headers;
30
31
    /**
32
     * Settings
33
     *
34
     * @var array
35
     * @access protected
36
     */
37
    protected $settings;
38
39
    /**
40
     * Request data
41
     *
42
     * @var array
43
     * @access protected
44
     */
45
    protected $data;
46
47
    /**
48
     * Request URL
49
     *
50
     * @var string
51
     * @access protected
52
     */
53
    protected $url;
54
55
    /**
56
     * Request method
57
     *
58
     * @var string
59
     * @access protected
60
     */
61
    protected $method;
62
63
    /**
64
     * Timeout period
65
     *
66
     * @var int
67
     * @access protected
68
     */
69
    protected $timeout;
70
71
    /**
72
     * Page load delay time.
73
     *
74
     * @var int
75
     * @access protected
76
     */
77
    protected $delay;
78
79
    /**
80
     * Viewport width.
81
     *
82
     * @var int
83
     * @access protected
84
     */
85
    protected $viewportWidth;
86
87
    /**
88
     * Viewport height.
89
     *
90
     * @var int
91
     * @access protected
92
     */
93
    protected $viewportHeight;
94
95
    /**
96
     * Body styles.
97
     *
98
     * @var array
99
     * @access protected
100
     */
101
    protected $bodyStyles;
102
103
    /**
104
     * Internal constructor
105
     *
106
     * @access public
107
     * @param string $url     (default: null)
108
     * @param string $method  (default: RequestInterface::METHOD_GET)
109
     * @param int    $timeout (default: 5000)
110
     */
111 118
    public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
112
    {
113 118
        $this->headers         = array();
114 118
        $this->data            = array();
115 118
        $this->bodyStyles      = array();
116 118
        $this->settings        = array();
117 118
        $this->delay           = 0;
118 118
        $this->viewportWidth   = 0;
119 118
        $this->viewportHeight  = 0;
120
121 118
        $this->setMethod($method);
122 118
        $this->setTimeout($timeout);
123
124 118
        if ($url) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
125 8
            $this->setUrl($url);
126 8
        }
127 118
    }
128
129
    /**
130
     * Set request method
131
     *
132
     * @access public
133
     * @param  string                                             $method
134
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
135
     * @throws \JonnyW\PhantomJs\Exception\InvalidMethodException
136
     */
137 118
    public function setMethod($method)
138
    {
139 118
        $method     = strtoupper($method);
140 118
        $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface');
141
142 118
        if (!$reflection->hasConstant('METHOD_' . $method)) {
143 3
            throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
144
        }
145
146 118
        $this->method = $method;
147
148 118
        return $this;
149
    }
150
151
    /**
152
     * Get request method
153
     *
154
     * @access public
155
     * @return string
156
     */
157 61
    public function getMethod()
158
    {
159 61
        return $this->method;
160
    }
161
162
    /**
163
     * Set timeout period
164
     *
165
     * @access public
166
     * @param  int                                    $timeout
167
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
168
     */
169 118
    public function setTimeout($timeout)
170
    {
171 118
        $this->settings['resourceTimeout'] = $timeout;
172
173 118
        return $this;
174
    }
175
176
    /**
177
     * Get timeout period
178
     *
179
     * @access public
180
     * @return int
181
     */
182 8
    public function getTimeout()
183
    {
184 8
        if (isset($this->settings['resourceTimeout'])) {
185 8
            return $this->settings['resourceTimeout'];
186
        }
187
188
        return null;
189
    }
190
191
    /**
192
     * Set page load delay time (seconds).
193
     *
194
     * @access public
195
     * @param  int                                    $delay
196
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
197
     */
198 6
    public function setDelay($delay)
199
    {
200 6
        $this->delay = (int) $delay;
201
202 6
        return $this;
203
    }
204
205
    /**
206
     * Get page load delay time (seconds).
207
     *
208
     * @access public
209
     * @return int
210
     */
211 28
    public function getDelay()
212
    {
213 28
        return (int) $this->delay;
214
    }
215
216
    /**
217
     * Set viewport size.
218
     *
219
     * @access public
220
     * @param  int  $width
221
     * @param  int  $height
222
     * @return void
223
     */
224 8
    public function setViewportSize($width, $height)
225
    {
226 8
        $this->viewportWidth  = (int) $width;
227 8
        $this->viewportHeight = (int) $height;
228
229 8
        return $this;
230
    }
231
232
    /**
233
     * Get viewport width.
234
     *
235
     * @access public
236
     * @return int
237
     */
238 33
    public function getViewportWidth()
239
    {
240 33
        return (int) $this->viewportWidth;
241
    }
242
243
    /**
244
     * Get viewport height.
245
     *
246
     * @access public
247
     * @return int
248
     */
249 33
    public function getViewportHeight()
250
    {
251 33
        return (int) $this->viewportHeight;
252
    }
253
254
    /**
255
     * Set request URL
256
     *
257
     * @access public
258
     * @param  string                                 $url
259
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
260
     */
261 54
    public function setUrl($url)
262
    {
263 54
        $this->url = $url;
264
265 54
        return $this;
266
    }
267
268
    /**
269
     * Get request URL
270
     *  - Assembles query string for GET
271
     *  and HEAD requests
272
     *
273
     * @access public
274
     * @return string
275
     */
276 47
    public function getUrl()
277
    {
278 47
        if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
279 4
            return $this->url;
280
        }
281
282 43
        $url = $this->url;
283
284 43
        if (count($this->data)) {
285
286 10
            $url .= false === strpos($url, '?') ? '?' : '&';
287 10
            $url .= http_build_query($this->data);
288 10
        }
289
290 43
        return $url;
291
    }
292
293
    /**
294
     * Get content body
295
     *  - Returns query string if not GET or HEAD
296
     *
297
     * @access public
298
     * @return string
299
     */
300 39
    public function getBody()
301
    {
302 39
        if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
303 35
            return '';
304
        }
305
306 4
        return http_build_query($this->getRequestData());
307
    }
308
309
    /**
310
     * Set request data
311
     *
312
     * @access public
313
     * @param  array                                  $data
314
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
315
     */
316 29
    public function setRequestData(array $data)
317
    {
318 29
        $this->data = $data;
319
320 29
        return $this;
321
    }
322
323
    /**
324
     * Get request data
325
     *
326
     * @access public
327
     * @param  boolean $flat
328
     * @return array
329
     */
330 10
    public function getRequestData($flat = true)
331
    {
332 10
        if ($flat) {
333 7
            return $this->flattenData($this->data);
334
        }
335
336 3
        return $this->data;
337
    }
338
339
    /**
340
     * Set headers
341
     *
342
     * @access public
343
     * @param  array                                  $headers
344
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
345
     */
346 9
    public function setHeaders(array $headers)
347
    {
348 9
        $this->headers = $headers;
349 9
    }
350
351
    /**
352
     * Add single header
353
     *
354
     * @access public
355
     * @param  string                                 $header
356
     * @param  string                                 $value
357
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
358
     */
359 1
    public function addHeader($header, $value)
360
    {
361 1
        $this->headers[$header] = $value;
362
363 1
        return $this;
364
    }
365
366
    /**
367
     * Merge headers with existing
368
     *
369
     * @access public
370
     * @param  array                                  $headers
371
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
372
     */
373 3
    public function addHeaders(array $headers)
374
    {
375 3
        $this->headers = array_merge($this->headers, $headers);
376
377 3
        return $this;
378
    }
379
380
    /**
381
     * Get request headers
382
     *
383
     * @access public
384
     * @param  string       $format
385
     * @return array|string
386
     */
387 40
    public function getHeaders($format = 'default')
388
    {
389 40
        if ($format === 'json') {
390 4
            return json_encode($this->headers);
391
        }
392
393 36
        return $this->headers;
394
    }
395
396
    /**
397
     * Add single setting
398
     *
399
     * @access public
400
     * @param  string                                 $setting
401
     * @param  string                                 $value
402
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
403
     */
404 2
    public function addSetting($setting, $value)
405
    {
406 2
        $this->settings[$setting] = $value;
407
408 2
        return $this;
409
    }
410
411
    /**
412
     * Get settings
413
     *
414
     * @access public
415
     * @return array
416
     */
417 32
    public function getSettings()
418
    {
419 32
        return $this->settings;
420
    }
421
422
    /**
423
     * Set body styles
424
     *
425
     * @access public
426
     * @param  array                                  $styles
427
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
428
     */
429 1
    public function setBodyStyles(array $styles)
430
    {
431 1
        $this->bodyStyles = $styles;
432
433 1
        return $this;
434
    }
435
436
    /**
437
     * Get body styles
438
     *
439
     * @access public
440
     * @param  string       $format (default: 'default')
441
     * @return array|string
442
     */
443 30
    public function getBodyStyles($format = 'default')
444
    {
445 30
        if ($format === 'json') {
446
            return json_encode($this->bodyStyles);
447
        }
448
449 30
        return $this->bodyStyles;
450
    }
451
452
    /**
453
     * Flatten data into single
454
     * dimensional array
455
     *
456
     * @access protected
457
     * @param  array  $data
458
     * @param  string $prefix
459
     * @param  string $format
460
     * @return array
461
     */
462 7
    protected function flattenData(array $data, $prefix = '', $format = '%s')
463
    {
464 7
        $flat = array();
465
466 7
        foreach ($data as $name => $value) {
467
468 7
            $ref = $prefix . sprintf($format, $name);
469
470 7
            if (is_array($value)) {
471
472 3
                $flat += $this->flattenData($value, $ref, '[%s]');
473 3
                continue;
474
            }
475
476 7
            $flat[$ref] = $value;
477 7
        }
478
479 7
        return $flat;
480
    }
481
}
482