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