/
Connector.php
430 lines (377 loc) · 9.64 KB
/
Connector.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
namespace EventBriteConnector;
use EventBriteConnector\Entity\Entity;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
/**
* Class Connector.
*
* @package EventBriteConnector
*/
class Connector {
/**
* The Api endpoint for access token generation.
*/
const API_ENDPOINT = 'https://www.eventbriteapi.com/v3';
/**
* The OAuth endpoint for access token generation.
*/
const OAUTH_ENDPOINT = 'https://www.eventbrite.com/oauth';
/**
* Api endpoint.
*
* @var string $endpoint
*/
protected $endpoint;
/**
* Your App client secret.
*
* @var string $clientSecret
*/
protected $clientSecret;
/**
* Your App client id.
*
* @var string $clientId
*/
protected $clientId;
/**
* Your App client access token.
*
* @var string $accessToken
*/
protected $accessToken;
/**
* Eventbrite loaded entities.
*
* @var array $entities
*/
protected $entities;
/**
* @var Client $httpClient;
*/
protected $httpClient;
/**
* EventBriteConnector constructor.
*
* @param string $client_secret
* Your App client secret.
* @param string $client_id
* Your App client secret.
* @param string $access_token
* Your App access token.
*/
public function __construct($client_secret, $client_id, $access_token = NULL) {
$this->setEndpoint(self::API_ENDPOINT);
$this->setClientSecret($client_secret);
$this->setClientId($client_id);
$this->setAccessToken($access_token);
$this->httpClient = new Client();
}
/**
* Set endpoint.
*
* @param string $endpoint
* Api endpoint.
*/
public function setEndpoint($endpoint) {
$this->endpoint = $endpoint;
}
/**
* Get endpoint.
*
* @return string
* Api endpoint.
*/
public function getEndpoint() {
return $this->endpoint;
}
/**
* Set client secret.
*
* @param $client_secret
* Your App client secret.
*
* @return Connector
* The Eventbrite connector instance.
*/
public function setClientSecret($client_secret) {
$this->clientSecret = $client_secret;
return $this;
}
/**
* Get client secret.
*
* @return string
* Your App client secret.
*/
public function getClientSecret() {
return $this->clientSecret;
}
/**
* Set client id.
*
* @param $client_id
* Your App client id.
*
* @return $this
* The Eventbrite connector instance.
*/
public function setClientId($client_id) {
$this->clientId = $client_id;
return $this;
}
/**
* Get client id.
*
* @return string
* Your App client id.
*/
public function getClientId() {
return $this->clientId;
}
/**
* Set access token.
*
* @param string|NULL $access_token
* Your App access token.
*
* @return $this
* The Eventbrite connector instance.
*/
public function setAccessToken($access_token = NULL) {
if (empty($access_token) && !empty($_SESSION['eb_access_token'])) {
$access_token = $_SESSION['eb_access_token'];
}
$this->accessToken = $access_token;
$_SESSION['eb_access_token'] = $access_token;
return $this;
}
/**
* Get access token.
*
* @return string
* Your App access token.
*
* @throws \RuntimeException
*/
public function getAccessToken() {
if (!empty($_SESSION['eb_access_token'])) {
$this->setAccessToken($_SESSION['eb_access_token']);
}
if (empty($this->accessToken)) {
$_SESSION['eb_last_request'] = array(
'server' => $_SERVER,
'get' => $_GET,
'post' => $_POST,
);
throw new \RuntimeException('Missing Access Token');
}
return $this->accessToken;
}
/**
* Delete access token.
*
* @return $this
* The Eventbrite connector instance.
*/
public function deleteAccessToken() {
unset($this->accessToken);
unset($_SESSION['eb_access_token']);
return $this;
}
/**
* Get entities.
*
* @param string $entity_api_type
* A string representing the type of the entity to be extracted.
*
* @return array
* An array of entities.
*/
public function getEntities($entity_api_type = '') {
return (!empty($entity_api_type)) ? $this->entities[$entity_api_type] : $this->entities;
}
/**
* Get entity.
*
* @param string $entity_api_type
* The entity type name.
* @param string $entity_id
* The entity id.
*
* @return Entity
* An Eventbrite entity instance.
*
* @throws \InvalidArgumentException
*/
public function getEntity($entity_api_type, $entity_id) {
if (!isset($this->entities[$entity_api_type][$entity_id])) {
$message = sprintf('Undefined entity %s with id %s', $entity_api_type, $entity_id);
throw new \InvalidArgumentException($message);
}
return $this->entities[$entity_api_type][$entity_id];
}
/**
* Set entities.
*
* @param array $entities
* An array of entities.
*/
protected function setEntities(array $entities) {
$this->entities = $entities;
}
/**
* Fetch entity.
*
* @param string $entity_api_type
* The entity type name.
* @param string|NULL $entity_id
* (optional) The entity id.
*
* @return \EventBriteConnector\Entity\Entity
* The added Eventbrite entity instance.
*/
public function fetch($entity_api_type, $entity_id = NULL) {
$entities = $this->getEntities();
if (empty($entities[$entity_api_type][$entity_id])) {
$entity = Entity::getInstance($entity_api_type, $entity_id);
$entity->setConnector($this);
$entity->load();
$entities[$entity->getEntityTypeName()][$entity->getEntityId()] = $entity;
$this->setEntities($entities);
$entity_id = $entity->getEntityId();
}
return $entities[$entity_api_type][$entity_id];
}
/**
* Remove entity.
*
* @param \EventBriteConnector\Entity\Entity $entity
* An Eventbrite entity instance.
*
* @return $this
* The Eventbrite connector instance.
*/
public function removeEntity(Entity $entity) {
$entities = $this->getEntities();
if (!empty($entities[$entity->getEntityApiType()][$entity->getEntityId()])) {
unset($entities[$entity->getEntityApiType()][$entity->getEntityId()]);
}
$this->setEntities($entities);
return $this;
}
/**
* Generate an URL for redirection to Eventbrite Authorization page.
*
* @return string
* The redirect URL.
*/
public function getAuthorizationURL() {
$params = array(
'response_type' => 'code',
'client_id' => $this->getClientId()
);
return self::OAUTH_ENDPOINT . '/authorize?' . http_build_query($params);
}
/**
* Invokes the Eventbrite API to get and set a valid Access Token.
*
* @param $auth_code
* The auth code.
*
* @throws \RuntimeException
*/
public function getOAuthToken($auth_code) {
$response = $this->request($this->buildOauthConnectionParams($auth_code));
if (empty($response)) {
throw new \RuntimeException('Empty response');
}
if (!empty($response->error)) {
throw new \RuntimeException($response->error);
}
$this->setAccessToken($response->access_token);
}
/**
* Build OAuth connection params.
*
* @param string $auth_code
* The code sent via GET from the Eventbrite Authorization page to your
* redirect URI.
*
* @return array
* Options to be used for the OAuth connection request.
*/
protected function buildOauthConnectionParams($auth_code) {
return array(
'url' => self::OAUTH_ENDPOINT . '/token',
'method' => 'POST',
'headers' => array('Content-type' => 'application/x-www-form-urlencoded'),
'data' => array(
'code' => $auth_code,
'client_secret' => $this->getClientSecret(),
'client_id' => $this->getClientId(),
'grant_type' => 'authorization_code'
),
'access_token_required' => FALSE,
);
}
/**
* Request.
*
* @param array $params
* An array of request parameters.
*
* @code
* $params = array(
* 'url' => self::OAUTH_ENDPOINT . '/token',
* 'method' => 'POST',
* 'headers' => array('Content-type' => 'application/x-www-form-urlencoded'),
* 'data' => array(
* 'code' => $auth_code,
* 'client_secret' => $this->getClientSecret(),
* 'client_id' => $this->getClientId(),
* 'grant_type' => 'authorization_code'
* ),
* 'access_token_required' => FALSE,
* );
* @endcode
*
* @return mixed
* The request response.
*/
public function request($params) {
$default = array(
'url' => '',
'method' => 'GET',
'headers' => array(),
'data' => array(),
'access_token_required' => TRUE,
);
$params = array_merge($default, $params);
if ($params['access_token_required']) {
$access_token = $this->getAccessToken();
$params['headers']['Authorization'] = 'Bearer ' . $access_token;
}
$data = is_array($params['data']) ? http_build_query($params['data']) : $params['data'];
if ($params['method'] == 'GET') {
$params['url'] .= !empty($data) ? '?' . $data : '';
$data = '';
}
elseif (empty($params['headers']['Content-type']) && !empty($data)) {
$params['headers']['Content-type'] = 'application/x-www-form-urlencoded';
}
$request = new Request($params['method'], $params['url'], $params['headers'], $data);
$response = $this->httpClient->send($request);
return json_decode($response->getBody()->getContents(), TRUE);
}
/**
* Get Http Client.
*
* @return \GuzzleHttp\Client
* The Http Client instance.
*/
public function getHttpClient() {
return $this->httpClient;
}
}