1
|
|
|
<?php |
2
|
|
|
namespace Lyal\Checkr; |
3
|
|
|
|
4
|
|
|
use Guzzle\Http\Message\Response; |
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use Lyal\Checkr\Exceptions\Client\BadRequest; |
8
|
|
|
use Lyal\Checkr\Exceptions\Client\Conflict; |
9
|
|
|
use Lyal\Checkr\Exceptions\Client\Forbidden; |
10
|
|
|
use Lyal\Checkr\Exceptions\Client\NotFound; |
11
|
|
|
use Lyal\Checkr\Exceptions\Client\Unauthorized; |
12
|
|
|
use Lyal\Checkr\Exceptions\Server\InternalServerError; |
13
|
|
|
use Lyal\Checkr\Exceptions\UnhandledRequestError; |
14
|
|
|
use Lyal\Checkr\Exceptions\UnknownResourceException; |
15
|
|
|
|
16
|
|
|
class Client |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
private $key; |
20
|
|
|
private $guzzle; |
21
|
|
|
private $options = []; |
22
|
|
|
|
23
|
|
|
private $lastResponse; |
24
|
|
|
|
25
|
|
|
public $useCollections = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Client constructor. |
29
|
|
|
* @param string|null $key |
30
|
|
|
* @param array $options |
31
|
|
|
* @param GuzzleClient|NULL $guzzle |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
public function __construct($key = NULL, array $options = [], GuzzleClient $guzzle = NULL) |
35
|
|
|
{ |
36
|
|
|
$this->setHttpClient($guzzle ?? new GuzzleClient()); |
37
|
|
|
$this->setKey($key ?? env('checkr_api_key', NULL)); |
38
|
|
|
$this->setOptions($options); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param GuzzleClient $client |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public function setHttpClient(GuzzleClient $client) : void |
48
|
|
|
{ |
49
|
|
|
$this->guzzle = $client; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getHttpClient() : GuzzleClient |
53
|
|
|
{ |
54
|
|
|
return $this->guzzle; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Fetch an API resource to handle the client request |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* @param array $args |
62
|
|
|
* @param \Lyal\Checkr\Entities\AbstractEntity $previousObject |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws UnknownResourceException |
65
|
|
|
*/ |
66
|
|
|
public function api($name, $args, $previousObject = null) |
67
|
|
|
{ |
68
|
|
|
// Autoload resource name using studly case |
69
|
|
|
if (!$className = checkrEntityClassName($name)) { |
70
|
|
|
throw new UnknownResourceException($name); |
71
|
|
|
} |
72
|
|
|
$args = count($args) ? $args[0] : $args; |
73
|
|
|
$entity = new $className($args, $this); |
74
|
|
|
if ($previousObject) { |
75
|
|
|
$entity->setPreviousObject($previousObject); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $entity; |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Pass all unknown methods to api() |
85
|
|
|
* |
86
|
|
|
* @param $name |
87
|
|
|
* @param $args |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
|
91
|
|
|
public function __call($name, $args) |
92
|
|
|
{ |
93
|
|
|
return $this->api($name, $args); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param bool $status |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
|
101
|
|
|
public function setLastResponse($response) |
102
|
|
|
{ |
103
|
|
|
$this->lastResponse = $response; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Response |
108
|
|
|
*/ |
109
|
|
|
|
110
|
|
|
public function getLastResponse() |
111
|
|
|
{ |
112
|
|
|
return $this->lastResponse; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set a key for authentication with checkr |
118
|
|
|
* |
119
|
|
|
* @param $key |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
|
123
|
|
|
public function setKey($key) : void |
124
|
|
|
{ |
125
|
|
|
$this->key = $key; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the checkr api key |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
|
134
|
|
|
public function getKey() : string |
135
|
|
|
{ |
136
|
|
|
return $this->key ?: env('checkr_api_key', NULL); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set options for HttpClient |
141
|
|
|
* |
142
|
|
|
* @param array $options |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
|
146
|
|
|
public function setOptions(array $options) : array |
147
|
|
|
{ |
148
|
|
|
return $this->options = $options; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set individual option |
153
|
|
|
* |
154
|
|
|
* @param $key |
155
|
|
|
* @param $value |
156
|
|
|
* |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
|
|
|
160
|
|
|
public function setOption($key, $value) |
161
|
|
|
{ |
162
|
|
|
return $this->options[$key] = $value; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
|
169
|
|
|
public function getOptions() : array |
170
|
|
|
{ |
171
|
|
|
return $this->options; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param $key |
176
|
|
|
* @return bool|mixed |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
|
181
|
|
|
public function getOption($key) |
182
|
|
|
{ |
183
|
|
|
if (isset($this->options[$key])) { |
184
|
|
|
return $this->options[$key]; |
185
|
|
|
} |
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Make a request through Guzzle |
191
|
|
|
* |
192
|
|
|
* Note: $options in the parameters of this function is temporary; for Guzzle options |
193
|
|
|
* like debug, set them on the $client |
194
|
|
|
* |
195
|
|
|
* @param $method |
196
|
|
|
* @param $path |
197
|
|
|
* @param array $options |
198
|
|
|
* @param boolean $returnResponse |
199
|
|
|
* @return mixed |
200
|
|
|
* @throws \Lyal\Checkr\Exceptions\UnhandledRequestError |
201
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\Unauthorized |
202
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\NotFound |
203
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\InternalServerError |
204
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\Forbidden |
205
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\Conflict |
206
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\BadRequest |
207
|
|
|
*/ |
208
|
|
|
public function request($method, $path, array $options = []) |
209
|
|
|
{ |
210
|
|
|
$body = ''; |
211
|
|
|
$options = array_merge($this->getOptions(), $options); |
212
|
|
|
$options['auth'] = [$this->getKey() . ':', '']; |
213
|
|
|
try { |
214
|
|
|
$response = $this->getHttpClient()->request($method, $this->getApiEndPoint() . $path, $options); |
215
|
|
|
$this->setLastResponse($response); |
216
|
|
|
$body = json_decode((string)$response->getBody()); |
217
|
|
|
|
218
|
|
|
} catch (BadResponseException $exception) { |
219
|
|
|
$this->handleError($exception); |
220
|
|
|
} |
221
|
|
|
return $body; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
|
228
|
|
|
public function getApiEndPoint() : string |
229
|
|
|
{ |
230
|
|
|
return 'https://api.checkr.com/v1/'; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Throw our own custom handler for errors |
235
|
|
|
* |
236
|
|
|
* @param BadResponseException $exception |
237
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\BadRequest |
238
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\Unauthorized |
239
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\Forbidden |
240
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\NotFound |
241
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\Conflict |
242
|
|
|
* @throws \Lyal\Checkr\Exceptions\Client\InternalServerError |
243
|
|
|
* @throws \Lyal\Checkr\Exceptions\UnhandledRequestError |
244
|
|
|
*/ |
245
|
|
|
|
246
|
|
|
private function handleError(BadResponseException $exception) : void |
247
|
|
|
{ |
248
|
|
|
|
249
|
|
|
$body = $exception->getResponse()->getBody(); |
250
|
|
|
|
251
|
|
|
switch ($exception->getResponse()->getStatusCode()) { |
252
|
|
|
case 400: |
253
|
|
|
throw new BadRequest($body); |
254
|
|
|
break; |
255
|
|
|
case 401: |
256
|
|
|
throw new Unauthorized($body); |
257
|
|
|
break; |
258
|
|
|
case 403: |
259
|
|
|
throw new Forbidden($body); |
260
|
|
|
break; |
261
|
|
|
case 404: |
262
|
|
|
throw new NotFound($body); |
263
|
|
|
break; |
264
|
|
|
case 409: |
265
|
|
|
throw new Conflict($body); |
266
|
|
|
break; |
267
|
|
|
case 500: |
268
|
|
|
throw new InternalServerError($body); |
269
|
|
|
break; |
270
|
|
|
default: |
271
|
|
|
throw new UnhandledRequestError($exception->getResponse()->getStatusCode(), $body); |
272
|
|
|
|
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths