|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ianrizky\MoslemPray\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use Ianrizky\MoslemPray\Contracts\Driverable; |
|
6
|
|
|
use Illuminate\Http\Client\Factory; |
|
7
|
|
|
use Illuminate\Http\Client\PendingRequest; |
|
8
|
|
|
use Illuminate\Http\Client\Response; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractDriver implements Driverable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* List of configuration value. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $config = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* List of basic configuration value. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $basicConfig = [ |
|
26
|
|
|
'timeout' => 2000, // in milliseconds |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Illuminate\Http\Client\PendingRequest instance. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Illuminate\Http\Client\PendingRequest |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $request; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new instance class. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $config |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
12 |
|
public function __construct(array $config = []) |
|
43
|
|
|
{ |
|
44
|
12 |
|
$this->mergeConfig($config); |
|
45
|
12 |
|
$this->checkConfig('url', 'timeout'); |
|
46
|
|
|
|
|
47
|
12 |
|
$this->createHttpInstance(); |
|
48
|
12 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Merge given configuration value with existing default configuration. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $config |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
12 |
|
protected function mergeConfig(array $config = []) |
|
57
|
|
|
{ |
|
58
|
12 |
|
$this->config = array_merge($this->basicConfig, $this->config, $config); |
|
59
|
12 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create Illuminate\Http\Client\PendingRequest instance. |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
12 |
|
protected function createHttpInstance() |
|
67
|
|
|
{ |
|
68
|
12 |
|
$this->request = tap(new PendingRequest(new Factory), function (PendingRequest $request) { |
|
69
|
12 |
|
$request->baseUrl($this->config['url']); |
|
70
|
|
|
|
|
71
|
12 |
|
transform($this->config['timeout'], function ($timeout) use ($request) { |
|
72
|
12 |
|
$request->timeout($timeout, true); |
|
|
|
|
|
|
73
|
12 |
|
}); |
|
74
|
12 |
|
}); |
|
75
|
12 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Run checking for specified configuration key. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string|array $key |
|
81
|
|
|
* @return void |
|
82
|
|
|
* |
|
83
|
|
|
* @throws \InvalidArgumentException |
|
84
|
|
|
*/ |
|
85
|
12 |
|
protected function checkConfig($keys) |
|
86
|
|
|
{ |
|
87
|
12 |
|
$keys = is_array($keys) ? $keys : func_get_args(); |
|
88
|
|
|
|
|
89
|
12 |
|
foreach ($keys as $key) { |
|
90
|
12 |
|
if (!array_key_exists($key, $this->config)) { |
|
91
|
|
|
throw new InvalidArgumentException($key . ' is not available.'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
12 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Throw a Exception exception if the given json status is error. |
|
98
|
|
|
* |
|
99
|
|
|
* @param \Illuminate\Http\Client\Response $response |
|
100
|
|
|
* @return \Illuminate\Http\Client\Response |
|
101
|
|
|
* |
|
102
|
|
|
* @throws \Exception |
|
103
|
|
|
*/ |
|
104
|
|
|
abstract protected function throwJsonError(Response $response): Response; |
|
105
|
|
|
} |
|
106
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.