-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnuLigaClient.php
374 lines (306 loc) · 11 KB
/
nuLigaClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Class for accessing the nuPortalRS REST interface.
* Available endpoints are listed under https://dartde-portal.liga.nu/rs/documentation/.
*/
class nuLigaClient
{
// nuLiga API access
protected $baseUrl = '';
protected $clientId = '';
protected $clientSecret = '';
// API scope for a "Verband"
const SCOPE = 'nuPortalRS_federation';
// Federation name
const FEDERATION = 'BDV';
// How many seconds is an access token valid?
const TOKEN_VALID = 300;
// Database connection.
protected $dbHost = '';
protected $dbUser = '';
protected $dbPassword = '';
protected $dbName = '';
private $db = null;
// The access token used for authenticating.
private $accessToken = null;
public function __construct($config)
{
$this->baseUrl = $config['api']['base_url'];
$this->clientId = $config['api']['client_id'];
$this->clientSecret = $config['api']['client_secret'];
$this->dbHost = $config['db']['host'];
$this->dbUser = $config['db']['user'];
$this->dbPassword = $config['db']['password'];
$this->dbName = $config['db']['name'];
}
/**
* Generic request method for API endpoints.
*
* @param string $path the API endpoint to call
* @param string $method which HTTP method (GET, POST, PUT, PATCH, DELETE,...)
* @param array $queryParams additional query parameters as array (the actual query will be built here)
* @param array $body optional data for POST or PUT requests as array
* @param array $customHeaders optional HTTP headers as array
* @param boolean $authenticated are we authenticated? If yes, pass Authentication header.
*
* @return array|mixed
* @throws Exception
*/
public function request(string $path, string $method = 'GET', array $queryParams = [],
array $body = [], array $customHeaders = [], $authenticated = true)
{
$url = $this->baseUrl . $path;
$queryParams['maxResults'] = 250;
if (count($queryParams) > 0) {
$url .= '?' . http_build_query($queryParams);
}
// Get a cURL handle...
$curl = curl_init();
// ... for the given endpoint.
curl_setopt($curl, CURLOPT_URL, $url);
// Handle POST and PUT requests.
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
} else if ($method == 'PUT') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
}
// Set body data for POST and PUT requests.
if (($method == 'POST' || $method == 'PUT') && count($body) > 0) {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($body));
}
// Needed HTTP headers.
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
'accept' => 'application/json'
];
if ($authenticated) {
$this->accessToken = $this->authenticate();
$headers['Authorization'] = sprintf('Bearer %s', $this->accessToken);
}
// Add custom header fields if necessary.
if (count($customHeaders) > 0) {
$headers = array_merge($headers, $customHeaders);
}
// Set headers to correct 'name: value' format and add them to request.
$headerFields = [];
foreach ($headers as $name => $value) {
$headerFields[] = sprintf('%s: %s', $name, $value);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerFields);
// Return call result.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
// Something went wrong.
if ($result === false) {
$http_error = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
throw new Exception(
sprintf('Received HTTP error code %d.', $http_error)
);
}
curl_close($curl);
// Get real data from result.
$data = json_decode($result, true);
// Received no JSON-data?
if (!is_array($data)) {
throw new Exception($result);
}
// Some API error was returned.
if (array_key_exists('error_description', $data)) {
throw new Exception($data['error_description']);
}
// Return data from API call.
return $data;
}
/**
* Provides an access token for authenticating against the API.
*
* @return string
* @throws Exception
*/
protected function authenticate()
{
// Try to find an access token which was last updated less than "validity time" ago.
$stmt = $this->getDatabase()->prepare("SELECT * FROM `token`
WHERE `federation` = ?
AND `type` = 'access'
AND `last_update` >= ?
ORDER BY `last_update` DESC LIMIT 1");
$stmt->execute([
nuLigaClient::FEDERATION,
date('Y-m-d H:i:s', time() - nuLigaClient::TOKEN_VALID)
]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
// Valid access token found, return it.
if ($data && $data['value']) {
return $data['value'];
// No valid access token found, try getting a new one via refresh token.
} else {
// Try to find a current refresh token.
$stmt = $this->getDatabase()->prepare("SELECT * FROM `token`
WHERE `federation` = ?
AND `type` = 'refresh'
ORDER BY `last_update` DESC LIMIT 1");
$stmt->execute([
nuLigaClient::FEDERATION
]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
// Refreshing access token.
if ($data && $data['value']) {
return $this->refreshTokensFromAPI($data['value']);
// Something went wrong, we need a completely new authentication.
} else {
return $this->refreshTokensFromAPI();
}
}
}
/**
* Refresh authentication tokens via API, either by giving a refresh token
* or by generating a new set.
*
* @param null|string $refreshToken
* @return string
* @throws Exception
*/
protected function refreshTokensFromAPI($refreshToken = null)
{
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded'
];
if ($refreshToken == null) {
$body = [
'grant_type' => 'client_credentials',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'scope' => nuLigaClient::SCOPE
];
$auth = $this->request('/rs/auth/token', 'POST', [], $body, $headers, false);
} else {
$body = [
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'scope' => nuLigaClient::SCOPE
];
try {
$auth = $this->request('/rs/auth/token', 'POST', [], $body, $headers, false);
// Available refresh token has failed, generate new tokens.
} catch (Exception $e) {
return $this->refreshTokensFromAPI();
}
}
// Obtained a new token set.
if ($auth['access_token'] && $auth['refresh_token']) {
// Write tokens to database.
$stored = $this->storeTokens($auth['access_token'], $auth['refresh_token']);
return $auth['access_token'];
// Something is seriously wrong here.
} else {
throw new Exception('Could not authenticate!');
}
}
/**
* Store access and refresh tokens to databaswe.
*
* @param string $accessToken
* @param string $refreshToken
* @return bool
*/
protected function storeTokens($accessToken, $refreshToken)
{
$stmt = $this->getDatabase()->prepare("INSERT INTO `token`
(`federation`, `value`, `last_update`, `type`)
VALUES
(?, ?, ?, ?)
ON DUPLICATE KEY UPDATE `value` = VALUES(value), `last_update` = VALUES(last_update)");
return $stmt->execute([nuLigaClient::FEDERATION, $accessToken, date('Y-m-d H:i:s', time()), 'access']) &&
$stmt->execute([nuLigaClient::FEDERATION, $refreshToken, date('Y-m-d H:i:s', time()), 'refresh']);
}
/**
* Get a database connection (instance singleton).
*
* @return PDO|null
*/
private function getDatabase()
{
if ($this->db) {
return $this->db;
} else {
return new PDO(
sprintf('mysql:host=%s;dbname=%s', $this->dbHost, $this->dbName),
$this->dbUser, $this->dbPassword
);
}
}
/**
* Fetches all federations.
*
* @return array|mixed
* @throws Exception
*/
public function getFederations()
{
$data = $this->request('/rs/2014/federations');
return $data['federationAbbr'] ?: [];
}
/**
* Fetches data for a single federation.
*
* @param string $federation a federation nickname.
* @return array|mixed
* @throws Exception
*/
public function getFederation($federation)
{
return $this->request(sprintf('/rs/2014/federations/%s', $federation));
}
/**
* Fetches all players for a given federation.
*
* @param string $federation a federation nickname.
* @return array|mixed
* @throws Exception
*/
public function getPlayers($federation)
{
$data = $this->request(sprintf('/rs/2014/federations/%s/players', $federation));
return $data['playerAbbr'] ?: [];
}
/**
* Fetches all clubs for a given federation.
*
* @param string $federation a federation nickname.
* @return array|mixed
* @throws Exception
*/
public function getClubs($federation)
{
$data = $this->request(sprintf('/rs/2014/federations/%s/clubs', $federation));
return $data['clubAbbr'] ?: [];
}
/**
* Fetches all seasons for a given federation.
*
* @param string $federation a federation nickname.
* @return array|mixed
* @throws Exception
*/
public function getSeasons($federation)
{
$data = $this->request(sprintf('/rs/2014/federations/%s/seasons', $federation));
return $data['seasonAbbr'] ?: [];
}
/**
* Fetches data for a single season in the given federation.
*
* @param string $federation a federation nickname.
* @param string $season a season nickname.
* @return array|mixed
* @throws Exception
*/
public function getSeason($federation, $season)
{
return $this->request(sprintf('/rs/2014/federations/%s/seasons/%s', $federation, $season));
}
}