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 ( bd17d5...23c909 )
by Jonny
03:33
created

AbstractRequest::getDelay()   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
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
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
     * Request data
33
     *
34
     * @var array
35
     * @access protected
36
     */
37
    protected $data;
38
39
    /**
40
     * Request URL
41
     *
42
     * @var string
43
     * @access protected
44
     */
45
    protected $url;
46
47
    /**
48
     * Request method
49
     *
50
     * @var string
51
     * @access protected
52
     */
53
    protected $method;
54
55
    /**
56
     * Timeout period
57
     *
58
     * @var int
59
     * @access protected
60
     */
61
    protected $timeout;
62
63
    /**
64
     * Page load delay time.
65
     *
66
     * @var int
67
     * @access protected
68
     */
69
    protected $delay;
70
71
    /**
72
     * Viewport width.
73
     *
74
     * @var int
75
     * @access protected
76
     */
77
    protected $viewportWidth;
78
79
    /**
80
     * Viewport height.
81
     *
82
     * @var int
83
     * @access protected
84
     */
85
    protected $viewportHeight;
86
87
    /**
88
     * Body styles.
89
     *
90
     * @var array
91
     * @access protected
92
     */
93
    protected $bodyStyles;
94
95
    /**
96
     * Internal constructor
97
     *
98
     * @access public
99
     * @param string $url     (default: null)
100
     * @param string $method  (default: RequestInterface::METHOD_GET)
101
     * @param int    $timeout (default: 5000)
102
     */
103 115
    public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
104
    {
105 115
        $this->headers         = array();
106 115
        $this->data            = array();
107 115
        $this->bodyStyles      = array();
108 115
        $this->delay           = 0;
109 115
        $this->viewportWidth   = 0;
110 115
        $this->viewportHeight  = 0;
111
112 115
        $this->setMethod($method);
113 115
        $this->setTimeout($timeout);
114
115 115
        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...
116 8
            $this->setUrl($url);
117 8
        }
118 115
    }
119
120
    /**
121
     * Set request method
122
     *
123
     * @access public
124
     * @param  string                                             $method
125
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
126
     * @throws \JonnyW\PhantomJs\Exception\InvalidMethodException
127
     */
128 115
    public function setMethod($method)
129
    {
130 115
        $method     = strtoupper($method);
131 115
        $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface');
132
133 115
        if (!$reflection->hasConstant('METHOD_' . $method)) {
134 3
            throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
135
        }
136
137 115
        $this->method = $method;
138
139 115
        return $this;
140
    }
141
142
    /**
143
     * Get request method
144
     *
145
     * @access public
146
     * @return string
147
     */
148 60
    public function getMethod()
149
    {
150 60
        return $this->method;
151
    }
152
153
    /**
154
     * Set timeout period
155
     *
156
     * @access public
157
     * @param  int                                    $timeout
158
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
159
     */
160 115
    public function setTimeout($timeout)
161
    {
162 115
        $this->timeout = $timeout;
163
164 115
        return $this;
165
    }
166
167
    /**
168
     * Get timeout period
169
     *
170
     * @access public
171
     * @return int
172
     */
173 35
    public function getTimeout()
174
    {
175 35
        return $this->timeout;
176
    }
177
178
    /**
179
     * Set page load delay time (seconds).
180
     *
181
     * @access public
182
     * @param  int                                    $delay
183
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
184
     */
185 6
    public function setDelay($delay)
186
    {
187 6
        $this->delay = (int) $delay;
188
189 6
        return $this;
190
    }
191
192
    /**
193
     * Get page load delay time (seconds).
194
     *
195
     * @access public
196
     * @return int
197
     */
198 27
    public function getDelay()
199
    {
200 27
        return (int) $this->delay;
201
    }
202
203
    /**
204
     * Set viewport size.
205
     *
206
     * @access public
207
     * @param  int  $width
208
     * @param  int  $height
209
     * @return void
210
     */
211 8
    public function setViewportSize($width, $height)
212
    {
213 8
        $this->viewportWidth  = (int) $width;
214 8
        $this->viewportHeight = (int) $height;
215
216 8
        return $this;
217
    }
218
219
    /**
220
     * Get viewport width.
221
     *
222
     * @access public
223
     * @return int
224
     */
225 32
    public function getViewportWidth()
226
    {
227 32
        return (int) $this->viewportWidth;
228
    }
229
230
    /**
231
     * Get viewport height.
232
     *
233
     * @access public
234
     * @return int
235
     */
236 32
    public function getViewportHeight()
237
    {
238 32
        return (int) $this->viewportHeight;
239
    }
240
241
    /**
242
     * Set request URL
243
     *
244
     * @access public
245
     * @param  string                                 $url
246
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
247
     */
248 53
    public function setUrl($url)
249
    {
250 53
        $this->url = $url;
251
252 53
        return $this;
253
    }
254
255
    /**
256
     * Get request URL
257
     *  - Assembles query string for GET
258
     *  and HEAD requests
259
     *
260
     * @access public
261
     * @return string
262
     */
263 46
    public function getUrl()
264
    {
265 46
        if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
266 4
            return $this->url;
267
        }
268
269 42
        $url = $this->url;
270
271 42
        if (count($this->data)) {
272
273 10
            $url .= false === strpos($url, '?') ? '?' : '&';
274 10
            $url .= http_build_query($this->data);
275 10
        }
276
277 42
        return $url;
278
    }
279
280
    /**
281
     * Get content body
282
     *  - Returns query string if not GET or HEAD
283
     *
284
     * @access public
285
     * @return string
286
     */
287 38
    public function getBody()
288
    {
289 38
        if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
290 34
            return '';
291
        }
292
293 4
        return http_build_query($this->getRequestData());
294
    }
295
296
    /**
297
     * Set request data
298
     *
299
     * @access public
300
     * @param  array                                  $data
301
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
302
     */
303 29
    public function setRequestData(array $data)
304
    {
305 29
        $this->data = $data;
306
307 29
        return $this;
308
    }
309
310
    /**
311
     * Get request data
312
     *
313
     * @access public
314
     * @param  boolean $flat
315
     * @return array
316
     */
317 10
    public function getRequestData($flat = true)
318
    {
319 10
        if ($flat) {
320 7
            return $this->flattenData($this->data);
321
        }
322
323 3
        return $this->data;
324
    }
325
326
    /**
327
     * Set headers
328
     *
329
     * @access public
330
     * @param  array                                  $headers
331
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
332
     */
333 9
    public function setHeaders(array $headers)
334
    {
335 9
        $this->headers = $headers;
336 9
    }
337
338
    /**
339
     * Add single header
340
     *
341
     * @access public
342
     * @param  string                                 $header
343
     * @param  string                                 $value
344
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
345
     */
346 1
    public function addHeader($header, $value)
347
    {
348 1
        $this->headers[$header] = $value;
349
350 1
        return $this;
351
    }
352
353
    /**
354
     * Merge headers with existing
355
     *
356
     * @access public
357
     * @param  array                                  $headers
358
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
359
     */
360 3
    public function addHeaders(array $headers)
361
    {
362 3
        $this->headers = array_merge($this->headers, $headers);
363
364 3
        return $this;
365
    }
366
367
    /**
368
     * Get request headers
369
     *
370
     * @access public
371
     * @param  string       $format
372
     * @return array|string
373
     */
374 39
    public function getHeaders($format = 'default')
375
    {
376 39
        if ($format === 'json') {
377 4
            return json_encode($this->headers);
378
        }
379
380 35
        return $this->headers;
381
    }
382
383
    /**
384
     * Set body styles
385
     *
386
     * @access public
387
     * @param  array                                  $styles
388
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
389
     */
390 1
    public function setBodyStyles(array $styles)
391
    {
392 1
        $this->bodyStyles = $styles;
393
394 1
        return $this;
395
    }
396
397
    /**
398
     * Get body styles
399
     *
400
     * @access public
401
     * @param  string       $format (default: 'default')
402
     * @return array|string
403
     */
404 29
    public function getBodyStyles($format = 'default')
405
    {
406 29
        if ($format === 'json') {
407
            return json_encode($this->bodyStyles);
408
        }
409
410 29
        return $this->bodyStyles;
411
    }
412
413
    /**
414
     * Flatten data into single
415
     * dimensional array
416
     *
417
     * @access protected
418
     * @param  array  $data
419
     * @param  string $prefix
420
     * @param  string $format
421
     * @return array
422
     */
423 7
    protected function flattenData(array $data, $prefix = '', $format = '%s')
424
    {
425 7
        $flat = array();
426
427 7
        foreach ($data as $name => $value) {
428
429 7
            $ref = $prefix . sprintf($format, $name);
430
431 7
            if (is_array($value)) {
432
433 3
                $flat += $this->flattenData($value, $ref, '[%s]');
434 3
                continue;
435
            }
436
437 7
            $flat[$ref] = $value;
438 7
        }
439
440 7
        return $flat;
441
    }
442
}
443