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