AbstractClient::setUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the alphaz Framework.
5
 *
6
 * @author Muhammad Umer Farooq <[email protected]>
7
 *
8
 * @author-profile https://www.facebook.com/Muhammadumerfarooq01/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 *
13
 * @since 1.0.0
14
 *
15
 * @license MIT
16
 */
17
18
namespace alphaz\http\Clients;
19
20
abstract class AbstractClient
21
{
22
    /**
23
     * Client resource object.
24
     *
25
     * @since 1.0.0
26
     *
27
     * @var object
28
     */
29
    protected $resource = '';
30
31
    /**
32
     * URL.
33
     *
34
     * @since 1.0.0
35
     *
36
     * @var string
37
     */
38
    protected $url = '';
39
40
    /**
41
     * Method.
42
     *
43
     * @since 1.0.0
44
     *
45
     * @var string
46
     */
47
    protected $method = '';
48
49
    /**
50
     * Fields.
51
     *
52
     * @since 1.0.0
53
     *
54
     * @var array
55
     */
56
    protected $fields = [];
57
58
    /**
59
     * Query.
60
     *
61
     * @since 1.0.0
62
     *
63
     * @var string
64
     */
65
    protected $query = '';
66
67
    /**
68
     * Request headers.
69
     *
70
     * @since 1.0.0
71
     *
72
     * @var array
73
     */
74
    protected $requestHeaders = [];
75
76
    /**
77
     * HTTP version from response.
78
     *
79
     * @since 1.0.0
80
     *
81
     * @var string
82
     */
83
    protected $version = '';
84
85
    /**
86
     * Response code.
87
     *
88
     * @since 1.0.0
89
     *
90
     * @var int
91
     */
92
    protected $code;
93
94
    /**
95
     * Response message.
96
     *
97
     * @since 1.0.0
98
     *
99
     * @var string
100
     */
101
    protected $message = '';
102
103
    /**
104
     * Raw response string.
105
     *
106
     * @since 1.0.0
107
     *
108
     * @var string
109
     */
110
    protected $response = '';
111
112
    /**
113
     * Raw response header.
114
     *
115
     * @since 1.0.0
116
     *
117
     * @var string
118
     */
119
    protected $responseHeader = '';
120
121
    /**
122
     * Response headers.
123
     *
124
     * @since 1.0.0
125
     *
126
     * @var array
127
     */
128
    protected $responseHeaders = [];
129
130
    /**
131
     * Response body.
132
     *
133
     * @since 1.0.0
134
     *
135
     * @var string
136
     */
137
    protected $body = '';
138
139
    /**
140
     * CURL options.
141
     *
142
     * @since 1.0.0
143
     *
144
     * @var array
145
     */
146
    protected $options = [];
147
148
    /**
149
     * Stream context.
150
     *
151
     * @since 1.0.0
152
     *
153
     * @var resource
154
     */
155
    protected $context = null;
156
157
    /**
158
     * Stream context options.
159
     *
160
     * @since 1.0.0
161
     *
162
     * @var array
163
     */
164
    protected $contextOptions = [];
165
166
    /**
167
     * Stream context parameters.
168
     *
169
     * @since 1.0.0
170
     *
171
     * @var array
172
     */
173
    protected $contextParams = [];
174
175
    /**
176
     * HTTP Response Headers.
177
     *
178
     * @since 1.0.0
179
     *
180
     * @var string
181
     */
182
    protected $httpResponseHeaders = null;
183
184
    /**
185
     * Stream mode.
186
     *
187
     * @since 1.0.0
188
     *
189
     * @var string
190
     */
191
    protected $mode = 'r';
192
193
    /**
194
     * Set the URL.
195
     *
196
     * @param (string) $url
197
     *
198
     * @since 1.0.0
199
     *
200
     * @return object
201
     */
202
    public function setUrl($url)
203
    {
204
        $this->url = $url;
205
206
        return $this;
207
    }
208
209
    /**
210
     * Get the URL.
211
     *
212
     * @since 1.0.0
213
     *
214
     * @return string
215
     */
216
    public function getUrl()
217
    {
218
        return $this->url;
219
    }
220
221
    /**
222
     * Set the method.
223
     *
224
     * @param (string) $method
225
     *
226
     * @since 1.0.0
227
     *
228
     * @return object
229
     */
230
    public function setMethod($method)
231
    {
232
        $valid = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'];
233
        $method = strtoupper($method);
234
235
        if (!in_array($method, $valid)) {
236
            throw new \Exception('Error: That request method is not valid.');
237
        }
238
        $this->method = $method;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Get the method.
245
     *
246
     * @since 1.0.0
247
     *
248
     * @return string
249
     */
250
    public function getMethod()
251
    {
252
        return $this->method;
253
    }
254
255
    /**
256
     * Set the fields.
257
     *
258
     * @param (string) $name
259
     *                       (string) $value
260
     *
261
     * @since 1.0.0
262
     *
263
     * @return object
264
     */
265
    public function setField($name, $value)
266
    {
267
        $this->fields[$name] = $value;
268
269
        return $this;
270
    }
271
272
    /**
273
     * Set fields.
274
     *
275
     * @param (array) $fields
276
     *
277
     * @since 1.0.0
278
     *
279
     * @return object
280
     */
281
    public function setFields(array $fields)
282
    {
283
        foreach ($fields as $name => $value) {
284
            $this->setField($name, $value);
285
        }
286
287
        $this->prepareQuery();
288
289
        return $this;
290
    }
291
292
    /**
293
     * Get a field.
294
     *
295
     * @param (string) $name name of field
296
     *
297
     * @since 1.0.0
298
     *
299
     * @return string
300
     */
301
    public function getField($name)
302
    {
303
        return (isset($this->fields[$name])) ? $this->fields[$name] : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? $this->fields[$name] : false could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
304
    }
305
306
    /**
307
     * Remove a field.
308
     *
309
     * @param (string) $name name of field
310
     *
311
     * @since 1.0.0
312
     *
313
     * @return object
314
     */
315
    public function removeField($name)
316
    {
317
        if (isset($this->fields[$name])) {
318
            unset($this->fields[$name]);
319
        }
320
321
        $this->prepareQuery();
322
323
        return $this;
324
    }
325
326
    /**
327
     * Prepare the HTTP query.
328
     *
329
     * @since 1.0.0
330
     *
331
     * @return object
332
     */
333
    public function prepareQuery()
334
    {
335
        $this->query = http_build_query($this->fields);
336
337
        return $this;
338
    }
339
340
    /**
341
     * Get the HTTP query.
342
     *
343
     * @since 1.0.0
344
     *
345
     * @return string
346
     */
347
    public function getQuery()
348
    {
349
        return $this->query;
350
    }
351
352
    /**
353
     * Get HTTP query length.
354
     *
355
     * @param (bool) $mb
356
     *
357
     * @since 1.0.0
358
     *
359
     * @return int
360
     */
361
    public function getQueryLength($mb = true)
362
    {
363
        return ($mb) ? mb_strlen($this->query) : strlen($this->query);
364
    }
365
366
    /**
367
     * Set a request header.
368
     *
369
     * @param (string) $name
370
     * @param (string) $value
371
     *
372
     * @since 1.0.0
373
     *
374
     * @return object
375
     */
376
    public function setRequestHeader($name, $value)
377
    {
378
        $this->requestHeaders[$name] = $value;
379
380
        return $this;
381
    }
382
383
    /**
384
     * Set all request headers.
385
     *
386
     * @param (arryay) $headers
0 ignored issues
show
Bug introduced by
The type alphaz\http\Clients\arryay was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
387
     *
388
     * @since 1.0.0
389
     *
390
     * @return object
391
     */
392
    public function setRequestHeaders(array $headers)
393
    {
394
        $this->requestHeaders = $headers;
395
396
        return $this;
397
    }
398
399
    /**
400
     * Get a request header.
401
     *
402
     * @param (string) $name
403
     *
404
     * @since 1.0.0
405
     *
406
     * @return mixed
407
     */
408
    public function getRequestHeader($name)
409
    {
410
        return (isset($this->requestHeaders[$name])) ? $this->requestHeaders[$name] : null;
411
    }
412
413
    /**
414
     * Get all request headers.
415
     *
416
     * @since 1.0.0
417
     *
418
     * @return array
419
     */
420
    public function getRequestHeaders()
421
    {
422
        return $this->requestHeaders;
423
    }
424
425
    /**
426
     * Determine if there are request headers.
427
     *
428
     * @since 1.0.0
429
     *
430
     * @return bool
431
     */
432
    public function hasRequestHeaders()
433
    {
434
        return count($this->requestHeaders) > 0;
435
    }
436
437
    /**
438
     * Get a response header.
439
     *
440
     * @param (string) $name
441
     *
442
     * @since 1.0.0
443
     *
444
     * @return mixed
445
     */
446
    public function getResponseHeader($name)
447
    {
448
        return (isset($this->responseHeaders[$name])) ? $this->responseHeaders[$name] : null;
449
    }
450
451
    /**
452
     * Get all response headers.
453
     *
454
     * @since 1.0.0
455
     *
456
     * @return array
457
     */
458
    public function getResponseHeaders()
459
    {
460
        return $this->responseHeaders;
461
    }
462
463
    /**
464
     * Determine if there are response headers.
465
     *
466
     * @since 1.0.0
467
     *
468
     * @return bool
469
     */
470
    public function hasResponseHeaders()
471
    {
472
        return count($this->responseHeaders) > 0;
473
    }
474
475
    /**
476
     * Get raw response header.
477
     *
478
     * @since 1.0.0
479
     *
480
     * @return string
481
     */
482
    public function getRawResponseHeader()
483
    {
484
        return $this->responseHeader;
485
    }
486
487
    /**
488
     * Get the response body.
489
     *
490
     * @since 1.0.0
491
     *
492
     * @return string
493
     */
494
    public function getBody()
495
    {
496
        return $this->body;
497
    }
498
499
    /**
500
     * Get the response code.
501
     *
502
     * @since 1.0.0
503
     *
504
     * @return string
505
     */
506
    public function getCode()
507
    {
508
        return $this->code;
509
    }
510
511
    /**
512
     * Get the response HTTP version.
513
     *
514
     * @since 1.0.0
515
     *
516
     * @return string
517
     */
518
    public function getHttpVersion()
519
    {
520
        return $this->version;
521
    }
522
523
    /**
524
     * Get the response HTTP message.
525
     *
526
     * @since 1.0.0
527
     *
528
     * @return string
529
     */
530
    public function getMessage()
531
    {
532
        return $this->message;
533
    }
534
535
    /**
536
     * Get the raw response.
537
     *
538
     * @since 1.0.0
539
     *
540
     * @return string
541
     */
542
    public function getResponse()
543
    {
544
        return $this->response;
545
    }
546
547
    /**
548
     * Determine whether or not resource is available.
549
     *
550
     * @since 1.0.0
551
     *
552
     * @return bool
553
     */
554
    public function hasResource()
555
    {
556
        return is_resource($this->resource);
557
    }
558
559
    /**
560
     * Get the resource.
561
     *
562
     * @since 1.0.0
563
     *
564
     * @return resource
565
     */
566
    public function getResource()
567
    {
568
        return $this->resource;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->resource returns the type object which is incompatible with the documented return type resource.
Loading history...
569
    }
570
571
    /**
572
     * Decode the body.
573
     *
574
     * @since 1.0.0
575
     *
576
     * @return void
577
     */
578
    public function decodeBody()
579
    {
580
        $this->body = Response::decodeBody($this->body, $this->responseHeaders['Content-Encoding']);
0 ignored issues
show
Bug introduced by
The type alphaz\http\Clients\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
581
    }
582
583
    /**
584
     * Create and open the client resource.
585
     *
586
     * @since 1.0.0
587
     *
588
     * @return AbstractClient
589
     */
590
    abstract public function open();
591
592
    /**
593
     * Method to send the request and get the response.
594
     *
595
     * @since 1.0.0
596
     *
597
     * @return void
598
     */
599
    abstract public function send();
600
601
    /**
602
     * Close the client resource.
603
     *
604
     * @since 1.0.0
605
     *
606
     * @return void
607
     */
608
    abstract public function close();
609
}
610