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 ( 65564e...78887b )
by Jonny
03:37
created

AbstractRequest   C

Complexity

Total Complexity 39

Size/Duplication

Total Lines 532
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 1

Test Coverage

Coverage 98.02%

Importance

Changes 0
Metric Value
wmc 39
lcom 7
cbo 1
dl 0
loc 532
ccs 99
cts 101
cp 0.9802
rs 6.96
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 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 addCookie() 0 18 1
A deleteCookie() 0 6 1
A getCookies() 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
     * Cookies
41
     *
42
     * @var array
43
     * @access protected
44
     */
45
    protected $cookies;
46
47
    /**
48
     * Request data
49
     *
50
     * @var array
51
     * @access protected
52
     */
53
    protected $data;
54
55
    /**
56
     * Request URL
57
     *
58
     * @var string
59
     * @access protected
60
     */
61
    protected $url;
62
63
    /**
64
     * Request method
65
     *
66
     * @var string
67
     * @access protected
68
     */
69
    protected $method;
70
71
    /**
72
     * Timeout period
73
     *
74
     * @var int
75
     * @access protected
76
     */
77
    protected $timeout;
78
79
    /**
80
     * Page load delay time.
81
     *
82
     * @var int
83
     * @access protected
84
     */
85
    protected $delay;
86
87
    /**
88
     * Viewport width.
89
     *
90
     * @var int
91
     * @access protected
92
     */
93
    protected $viewportWidth;
94
95
    /**
96
     * Viewport height.
97
     *
98
     * @var int
99
     * @access protected
100
     */
101
    protected $viewportHeight;
102
103
    /**
104
     * Body styles.
105
     *
106
     * @var array
107
     * @access protected
108
     */
109
    protected $bodyStyles;
110
111 118
    /**
112
     * Internal constructor
113 118
     *
114 118
     * @access public
115 118
     * @param string $url     (default: null)
116 118
     * @param string $method  (default: RequestInterface::METHOD_GET)
117 118
     * @param int    $timeout (default: 5000)
118 118
     */
119 118
    public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
120
    {
121 118
        $this->headers         = array();
122 118
        $this->data            = array();
123
        $this->bodyStyles      = array();
124 118
        $this->settings        = array();
125 8
        $this->delay           = 0;
126 8
        $this->viewportWidth   = 0;
127 118
        $this->viewportHeight  = 0;
128
129
        $this->cookies = array(
130
            'add'    => array(),
131
            'delete' => array()
132
        );
133
134
        $this->setMethod($method);
135
        $this->setTimeout($timeout);
136
137 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...
138
            $this->setUrl($url);
139 118
        }
140 118
    }
141
142 118
    /**
143 3
     * Set request method
144
     *
145
     * @access public
146 118
     * @param  string                                             $method
147
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
148 118
     * @throws \JonnyW\PhantomJs\Exception\InvalidMethodException
149
     */
150
    public function setMethod($method)
151
    {
152
        $method     = strtoupper($method);
153
        $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface');
154
155
        if (!$reflection->hasConstant('METHOD_' . $method)) {
156
            throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
157 61
        }
158
159 61
        $this->method = $method;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Get request method
166
     *
167
     * @access public
168
     * @return string
169 118
     */
170
    public function getMethod()
171 118
    {
172
        return $this->method;
173 118
    }
174
175
    /**
176
     * Set timeout period
177
     *
178
     * @access public
179
     * @param  int                                    $timeout
180
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
181
     */
182 8
    public function setTimeout($timeout)
183
    {
184 8
        $this->settings['resourceTimeout'] = $timeout;
185 8
186
        return $this;
187
    }
188
189
    /**
190
     * Get timeout period
191
     *
192
     * @access public
193
     * @return int
194
     */
195
    public function getTimeout()
196
    {
197
        if (isset($this->settings['resourceTimeout'])) {
198 6
            return $this->settings['resourceTimeout'];
199
        }
200 6
201
        return null;
202 6
    }
203
204
    /**
205
     * Set page load delay time (seconds).
206
     *
207
     * @access public
208
     * @param  int                                    $delay
209
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
210
     */
211 28
    public function setDelay($delay)
212
    {
213 28
        $this->delay = (int) $delay;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Get page load delay time (seconds).
220
     *
221
     * @access public
222
     * @return int
223
     */
224 8
    public function getDelay()
225
    {
226 8
        return (int) $this->delay;
227 8
    }
228
229 8
    /**
230
     * Set viewport size.
231
     *
232
     * @access public
233
     * @param  int  $width
234
     * @param  int  $height
235
     * @return void
236
     */
237
    public function setViewportSize($width, $height)
238 33
    {
239
        $this->viewportWidth  = (int) $width;
240 33
        $this->viewportHeight = (int) $height;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Get viewport width.
247
     *
248
     * @access public
249 33
     * @return int
250
     */
251 33
    public function getViewportWidth()
252
    {
253
        return (int) $this->viewportWidth;
254
    }
255
256
    /**
257
     * Get viewport height.
258
     *
259
     * @access public
260
     * @return int
261 54
     */
262
    public function getViewportHeight()
263 54
    {
264
        return (int) $this->viewportHeight;
265 54
    }
266
267
    /**
268
     * Set request URL
269
     *
270
     * @access public
271
     * @param  string                                 $url
272
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
273
     */
274
    public function setUrl($url)
275
    {
276 47
        $this->url = $url;
277
278 47
        return $this;
279 4
    }
280
281
    /**
282 43
     * Get request URL
283
     *  - Assembles query string for GET
284 43
     *  and HEAD requests
285
     *
286 10
     * @access public
287 10
     * @return string
288 10
     */
289
    public function getUrl()
290 43
    {
291
        if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
292
            return $this->url;
293
        }
294
295
        $url = $this->url;
296
297
        if (count($this->data)) {
298
299
            $url .= false === strpos($url, '?') ? '?' : '&';
300 39
            $url .= http_build_query($this->data);
301
        }
302 39
303 35
        return $url;
304
    }
305
306 4
    /**
307
     * Get content body
308
     *  - Returns query string if not GET or HEAD
309
     *
310
     * @access public
311
     * @return string
312
     */
313
    public function getBody()
314
    {
315
        if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
316 29
            return '';
317
        }
318 29
319
        return http_build_query($this->getRequestData());
320 29
    }
321
322
    /**
323
     * Set request data
324
     *
325
     * @access public
326
     * @param  array                                  $data
327
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
328
     */
329
    public function setRequestData(array $data)
330 10
    {
331
        $this->data = $data;
332 10
333 7
        return $this;
334
    }
335
336 3
    /**
337
     * Get request data
338
     *
339
     * @access public
340
     * @param  boolean $flat
341
     * @return array
342
     */
343
    public function getRequestData($flat = true)
344
    {
345
        if ($flat) {
346 9
            return $this->flattenData($this->data);
347
        }
348 9
349 9
        return $this->data;
350
    }
351
352
    /**
353
     * Set headers
354
     *
355
     * @access public
356
     * @param  array                                  $headers
357
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
358
     */
359 1
    public function setHeaders(array $headers)
360
    {
361 1
        $this->headers = $headers;
362
    }
363 1
364
    /**
365
     * Add single header
366
     *
367
     * @access public
368
     * @param  string                                 $header
369
     * @param  string                                 $value
370
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
371
     */
372
    public function addHeader($header, $value)
373 3
    {
374
        $this->headers[$header] = $value;
375 3
376
        return $this;
377 3
    }
378
379
    /**
380
     * Merge headers with existing
381
     *
382
     * @access public
383
     * @param  array                                  $headers
384
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
385
     */
386
    public function addHeaders(array $headers)
387 40
    {
388
        $this->headers = array_merge($this->headers, $headers);
389 40
390 4
        return $this;
391
    }
392
393 36
    /**
394
     * Get request headers
395
     *
396
     * @access public
397
     * @param  string       $format
398
     * @return array|string
399
     */
400
    public function getHeaders($format = 'default')
401
    {
402
        if ($format === 'json') {
403
            return json_encode($this->headers);
404 2
        }
405
406 2
        return $this->headers;
407
    }
408 2
409
    /**
410
     * Add single setting
411
     *
412
     * @access public
413
     * @param  string                                 $setting
414
     * @param  string                                 $value
415
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
416
     */
417 32
    public function addSetting($setting, $value)
418
    {
419 32
        $this->settings[$setting] = $value;
420
421
        return $this;
422
    }
423
424
    /**
425
     * Get settings
426
     *
427
     * @access public
428
     * @return array
429 1
     */
430
    public function getSettings()
431 1
    {
432
        return $this->settings;
433 1
    }
434
435
    /**
436
     * Add cookie.
437
     *
438
     * @access public
439
     * @param  string                                 $name
440
     * @param  mixed                                  $value
441
     * @param  string                                 $path
442
     * @param  string                                 $domain
443 30
     * @param  bool                                   $httpOnly (default: true)
444
     * @param  bool                                   $secure   (default: false)
445 30
     * @param  int                                    $expires  (default: null)
446
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
447
     */
448
    public function addCookie($name, $value, $path, $domain, $httpOnly = true, $secure = false, $expires = null)
449 30
    {
450
        $filter = function ($value) {
451
            return !is_null($value);
452
        };
453
454
        $this->cookies['add'][] = array_filter(array(
455
            'name'     => $name,
456
            'value'    => $value,
457
            'path'     => $path,
458
            'domain'   => $domain,
459
            'httponly' => $httpOnly,
460
            'secure'   => $secure,
461
            'expires'  => $expires
462 7
        ), $filter);
463
464 7
        return $this;
465
    }
466 7
467
    /**
468 7
     * Delete cookie.
469
     *
470 7
     * @access public
471
     * @param  string                                 $name
472 3
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
473 3
     */
474
    public function deleteCookie($name)
475
    {
476 7
        $this->cookies['delete'][] = $name;
477 7
478
        return $this;
479 7
    }
480
481
    /**
482
     * Get cookies
483
     *
484
     * @access public
485
     * @return array
486
     */
487
    public function getCookies()
488
    {
489
        return $this->cookies;
490
    }
491
492
    /**
493
     * Set body styles
494
     *
495
     * @access public
496
     * @param  array                                  $styles
497
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
498
     */
499
    public function setBodyStyles(array $styles)
500
    {
501
        $this->bodyStyles = $styles;
502
503
        return $this;
504
    }
505
506
    /**
507
     * Get body styles
508
     *
509
     * @access public
510
     * @param  string       $format (default: 'default')
511
     * @return array|string
512
     */
513
    public function getBodyStyles($format = 'default')
514
    {
515
        if ($format === 'json') {
516
            return json_encode($this->bodyStyles);
517
        }
518
519
        return $this->bodyStyles;
520
    }
521
522
    /**
523
     * Flatten data into single
524
     * dimensional array
525
     *
526
     * @access protected
527
     * @param  array  $data
528
     * @param  string $prefix
529
     * @param  string $format
530
     * @return array
531
     */
532
    protected function flattenData(array $data, $prefix = '', $format = '%s')
533
    {
534
        $flat = array();
535
536
        foreach ($data as $name => $value) {
537
538
            $ref = $prefix . sprintf($format, $name);
539
540
            if (is_array($value)) {
541
542
                $flat += $this->flattenData($value, $ref, '[%s]');
543
                continue;
544
            }
545
546
            $flat[$ref] = $value;
547
        }
548
549
        return $flat;
550
    }
551
}
552