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
Pull Request — master (#194)
by
unknown
02:20
created

AbstractRequest::getSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    /**
112
     * Internal constructor
113
     *
114
     * @access public
115
     * @param string $url     (default: null)
116
     * @param string $method  (default: RequestInterface::METHOD_GET)
117
     * @param int    $timeout (default: 5000)
118
     */
119 12
    public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
120
    {
121 12
        $this->headers         = array();
122 12
        $this->data            = array();
123 12
        $this->bodyStyles      = array();
124 12
        $this->settings        = array();
125 12
        $this->delay           = 0;
126 12
        $this->viewportWidth   = 0;
127 12
        $this->viewportHeight  = 0;
128
129 12
        $this->cookies = array(
130
            'add'    => array(),
131
            'delete' => array()
132
        );
133
134 12
        $this->setMethod($method);
135 12
        $this->setTimeout($timeout);
136
137 12
        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
        }
140 12
    }
141
142
    /**
143
     * Set request method
144
     *
145
     * @access public
146
     * @param  string                                             $method
147
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
148
     * @throws \JonnyW\PhantomJs\Exception\InvalidMethodException
149
     */
150 12
    public function setMethod($method)
151
    {
152 12
        $method     = strtoupper($method);
153 12
        $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface');
154
155 12
        if (!$reflection->hasConstant('METHOD_' . $method)) {
156
            throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
157
        }
158
159 12
        $this->method = $method;
160
161 12
        return $this;
162
    }
163
164
    /**
165
     * Get request method
166
     *
167
     * @access public
168
     * @return string
169
     */
170 9
    public function getMethod()
171
    {
172 9
        return $this->method;
173
    }
174
175
    /**
176
     * Set timeout period
177
     *
178
     * @access public
179
     * @param  int                                    $timeout
180
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
181
     */
182 12
    public function setTimeout($timeout)
183
    {
184 12
        $this->settings['resourceTimeout'] = $timeout;
185
186 12
        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
            return $this->settings['resourceTimeout'];
199
        }
200
201
        return null;
202
    }
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
    public function setDelay($delay)
212
    {
213
        $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 9
    public function getDelay()
225
    {
226 9
        return (int) $this->delay;
227
    }
228
229
    /**
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
    {
239
        $this->viewportWidth  = (int) $width;
240
        $this->viewportHeight = (int) $height;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Get viewport width.
247
     *
248
     * @access public
249
     * @return int
250
     */
251 9
    public function getViewportWidth()
252
    {
253 9
        return (int) $this->viewportWidth;
254
    }
255
256
    /**
257
     * Get viewport height.
258
     *
259
     * @access public
260
     * @return int
261
     */
262 9
    public function getViewportHeight()
263
    {
264 9
        return (int) $this->viewportHeight;
265
    }
266
267
    /**
268
     * Set request URL
269
     *
270
     * @access public
271
     * @param  string                                 $url
272
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
273
     */
274 9
    public function setUrl($url)
275
    {
276 9
        $this->url = $url;
277
278 9
        return $this;
279
    }
280
281
    /**
282
     * Get request URL
283
     *  - Assembles query string for GET
284
     *  and HEAD requests
285
     *
286
     * @access public
287
     * @return string
288
     */
289 9
    public function getUrl()
290
    {
291 9
        if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
292
            return $this->url;
293
        }
294
295 9
        $url = $this->url;
296
297 9
        if (count($this->data)) {
298
299 1
            $url .= false === strpos($url, '?') ? '?' : '&';
300 1
            $url .= http_build_query($this->data);
301
        }
302
303 9
        return $url;
304
    }
305
306
    /**
307
     * Get content body
308
     *  - Returns query string if not GET or HEAD
309
     *
310
     * @access public
311
     * @return string
312
     */
313 9
    public function getBody()
314
    {
315 9
        if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
316 9
            return '';
317
        }
318
319
        return http_build_query($this->getRequestData());
320
    }
321
322
    /**
323
     * Set request data
324
     *
325
     * @access public
326
     * @param  array                                  $data
327
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
328
     */
329 1
    public function setRequestData(array $data)
330
    {
331 1
        $this->data = $data;
332
333 1
        return $this;
334
    }
335
336
    /**
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
            return $this->flattenData($this->data);
347
        }
348
349
        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
    public function setHeaders(array $headers)
360
    {
361
        $this->headers = $headers;
362
    }
363
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
    {
374
        $this->headers[$header] = $value;
375
376
        return $this;
377
    }
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
    {
388
        $this->headers = array_merge($this->headers, $headers);
389
390
        return $this;
391
    }
392
393
    /**
394
     * Get request headers
395
     *
396
     * @access public
397
     * @param  string       $format
398
     * @return array|string
399
     */
400 9
    public function getHeaders($format = 'default')
401
    {
402 9
        if ($format === 'json') {
403
            return json_encode($this->headers);
404
        }
405
406 9
        return $this->headers;
407
    }
408
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 1
    public function addSetting($setting, $value)
418
    {
419 1
        $this->settings[$setting] = $value;
420
421 1
        return $this;
422
    }
423
424
    /**
425
     * Get settings
426
     *
427
     * @access public
428
     * @return array
429
     */
430 9
    public function getSettings()
431
    {
432 9
        return $this->settings;
433
    }
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
     * @param  bool                                   $httpOnly (default: true)
444
     * @param  bool                                   $secure   (default: false)
445
     * @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
    {
450 5
        $filter = function ($value) {
451 5
            return !is_null($value);
452 5
        };
453
454 5
        $this->cookies['add'][] = array_filter(array(
455 5
            'name'     => $name,
456 5
            'value'    => $value,
457 5
            'path'     => $path,
458 5
            'domain'   => $domain,
459 5
            'httponly' => $httpOnly,
460 5
            'secure'   => $secure,
461 5
            'expires'  => $expires
462 5
        ), $filter);
463
464 5
        return $this;
465
    }
466
467
    /**
468
     * Delete cookie.
469
     *
470
     * @access public
471
     * @param  string                                 $name
472
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
473
     */
474 2
    public function deleteCookie($name)
475
    {
476 2
        $this->cookies['delete'][] = $name;
477
478 2
        return $this;
479
    }
480
481
    /**
482
     * Get cookies
483
     *
484
     * @access public
485
     * @return array
486
     */
487 9
    public function getCookies()
488
    {
489 9
        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 9
    public function getBodyStyles($format = 'default')
514
    {
515 9
        if ($format === 'json') {
516
            return json_encode($this->bodyStyles);
517
        }
518
519 9
        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