1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kevintweber\Gauges; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use GuzzleHttp\Middleware; |
9
|
|
|
use GuzzleHttp\MessageFormatter; |
10
|
|
|
use GuzzleHttp\Psr7\Response; |
11
|
|
|
use Psr\Log\LoggerAwareInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Psr\Log\LogLevel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Used to make Gauges API calls. |
17
|
|
|
*/ |
18
|
|
|
class Request implements LoggerAwareInterface |
19
|
|
|
{ |
20
|
|
|
const URI = 'https://secure.gaug.es/'; |
21
|
|
|
|
22
|
|
|
/** @var null|ClientInterface */ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
/** @var HandlerStack */ |
26
|
|
|
private $handlerStack; |
27
|
|
|
|
28
|
|
|
/** @var null|LoggerInterface */ |
29
|
|
|
private $logger; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $logLevel; |
33
|
|
|
|
34
|
|
|
/** @var MessageFormatter */ |
35
|
|
|
private $messageFormatter; |
36
|
|
|
|
37
|
|
|
/** @var array */ |
38
|
|
|
private $options; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $token; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param string $token Your API token |
47
|
|
|
* @param array $options See Guzzle documentation (proxy, etc.) |
48
|
|
|
*/ |
49
|
27 |
|
public function __construct(string $token, array $options = array()) |
50
|
|
|
{ |
51
|
27 |
|
$this->client = null; |
52
|
27 |
|
$this->handlerStack = HandlerStack::create(); |
53
|
27 |
|
$this->logger = null; |
54
|
27 |
|
$this->logLevel = LogLevel::INFO; |
55
|
27 |
|
$this->messageFormatter = new MessageFormatter(); |
56
|
27 |
|
$this->options = array_merge( |
57
|
27 |
|
array('timeout' => 10), |
58
|
|
|
$options |
59
|
|
|
); |
60
|
27 |
|
$this->options['base_uri'] = self::URI; |
61
|
27 |
|
$this->token = $token; |
62
|
27 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Getter for the HTTP client. |
66
|
|
|
* |
67
|
|
|
* @return Client |
68
|
|
|
*/ |
69
|
23 |
|
protected function getHttpClient() : Client |
70
|
|
|
{ |
71
|
23 |
|
if ($this->client === null) { |
72
|
23 |
|
if ($this->logger instanceof LoggerInterface) { |
73
|
23 |
|
$this->handlerStack->push( |
74
|
23 |
|
Middleware::log( |
75
|
23 |
|
$this->logger, |
76
|
23 |
|
$this->messageFormatter, |
77
|
23 |
|
$this->logLevel |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
23 |
|
$this->options['handler'] = $this->handlerStack; |
83
|
23 |
|
$this->client = new Client($this->options); |
84
|
|
|
} |
85
|
|
|
|
86
|
23 |
|
return $this->client; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Setter for the Guzzle HandlerStack |
91
|
|
|
*/ |
92
|
26 |
|
public function setHandlerStack(HandlerStack $handlerStack) |
93
|
|
|
{ |
94
|
26 |
|
$this->handlerStack = $handlerStack; |
95
|
26 |
|
} |
96
|
|
|
|
97
|
27 |
|
public function setLogger(LoggerInterface $logger) |
98
|
|
|
{ |
99
|
27 |
|
$this->logger = $logger; |
100
|
27 |
|
} |
101
|
|
|
|
102
|
26 |
|
public function setLogLevel(string $logLevel) |
103
|
|
|
{ |
104
|
26 |
|
$logLevel = strtolower($logLevel); |
105
|
26 |
|
if ($logLevel !== LogLevel::ALERT && |
106
|
26 |
|
$logLevel !== LogLevel::CRITICAL && |
107
|
26 |
|
$logLevel !== LogLevel::DEBUG && |
108
|
26 |
|
$logLevel !== LogLevel::EMERGENCY && |
109
|
26 |
|
$logLevel !== LogLevel::ERROR && |
110
|
26 |
|
$logLevel !== LogLevel::INFO && |
111
|
26 |
|
$logLevel !== LogLevel::NOTICE && |
112
|
26 |
|
$logLevel !== LogLevel::WARNING) { |
113
|
1 |
|
throw new \InvalidArgumentException('Invalid log level: ' . $logLevel); |
114
|
|
|
} |
115
|
|
|
|
116
|
26 |
|
$this->logLevel = $logLevel; |
117
|
26 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Setter for the Guzzle MessageFormatter |
121
|
|
|
*/ |
122
|
27 |
|
public function setMessageFormatter(MessageFormatter $messageFormatter) |
123
|
|
|
{ |
124
|
27 |
|
$this->messageFormatter = $messageFormatter; |
125
|
27 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get Your Information |
129
|
|
|
* |
130
|
|
|
* Returns your information. |
131
|
|
|
* |
132
|
|
|
* @return Response |
133
|
|
|
*/ |
134
|
2 |
|
public function me() : Response |
135
|
|
|
{ |
136
|
2 |
|
return $this->makeApiCall('GET', 'me'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Update Your Information |
141
|
|
|
* |
142
|
|
|
* Updates and returns your information with the updates applied. |
143
|
|
|
* |
144
|
|
|
* @param string $first_name Your first name. (Optional) |
145
|
|
|
* @param string $last_name Your last name. (Optional) |
146
|
|
|
* |
147
|
|
|
* @return Response |
148
|
|
|
*/ |
149
|
1 |
|
public function updateMe(string $first_name = null, string $last_name = null) : Response |
150
|
|
|
{ |
151
|
1 |
|
$params = array(); |
152
|
1 |
|
if (isset($first_name)) { |
153
|
1 |
|
$params['first_name'] = $first_name; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
if (isset($last_name)) { |
157
|
1 |
|
$params['last_name'] = $last_name; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
return $this->makeApiCall('PUT', 'me', $params); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* API Client List |
165
|
|
|
* |
166
|
|
|
* Returns an array of your API clients. |
167
|
|
|
* |
168
|
|
|
* @return Response |
169
|
|
|
*/ |
170
|
1 |
|
public function listClients() : Response |
171
|
|
|
{ |
172
|
1 |
|
return $this->makeApiCall('GET', 'clients'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Creating an API Client |
177
|
|
|
* |
178
|
|
|
* Creates an API client, which can be used to authenticate against |
179
|
|
|
* the Gaug.es API. |
180
|
|
|
* |
181
|
|
|
* @param string $description Short description for the key (Optional) |
182
|
|
|
* |
183
|
|
|
* @return Response |
184
|
|
|
*/ |
185
|
1 |
|
public function createClient(string $description = null) : Response |
186
|
|
|
{ |
187
|
1 |
|
$params = array(); |
188
|
1 |
|
if (isset($description)) { |
189
|
1 |
|
$params['description'] = $description; |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
return $this->makeApiCall('POST', 'clients', $params); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Delete an API Client |
197
|
|
|
* |
198
|
|
|
* Permanently deletes an API client key. |
199
|
|
|
* |
200
|
|
|
* @param string $id |
201
|
|
|
* |
202
|
|
|
* @return Response |
203
|
|
|
*/ |
204
|
1 |
|
public function deleteClient(string $id) : Response |
205
|
|
|
{ |
206
|
1 |
|
return $this->makeApiCall('DELETE', 'clients/' . $id); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Gauges List |
211
|
|
|
* |
212
|
|
|
* Returns an array of your gauges, with recent traffic included. |
213
|
|
|
* |
214
|
|
|
* @param int $page Page number (Optional) |
215
|
|
|
* |
216
|
|
|
* @return Response |
217
|
|
|
*/ |
218
|
1 |
|
public function listGauges(int $page = null) : Response |
219
|
|
|
{ |
220
|
1 |
|
return $this->makeApiCall('GET', 'gauges', $this->formatCommonParameters(null, $page)); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Create a New Gauge |
225
|
|
|
* |
226
|
|
|
* Creates a gauge. |
227
|
|
|
* |
228
|
|
|
* @param string $title |
229
|
|
|
* @param string|\DateTimeZone $tz |
230
|
|
|
* @param string $allowedHosts (Optional) |
231
|
|
|
* |
232
|
|
|
* @return Response |
233
|
|
|
*/ |
234
|
2 |
|
public function createGauge(string $title, $tz, string $allowedHosts = null) : Response |
235
|
|
|
{ |
236
|
2 |
|
return $this->makeApiCall('POST', 'gauges', $this->formatGaugeParameters($title, $tz, $allowedHosts)); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Gauge Detail |
241
|
|
|
* |
242
|
|
|
* Gets details for a gauge. |
243
|
|
|
* |
244
|
|
|
* @param string $id |
245
|
|
|
* |
246
|
|
|
* @return Response |
247
|
|
|
*/ |
248
|
1 |
|
public function gaugeDetail(string $id) : Response |
249
|
|
|
{ |
250
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Update a Gauge |
255
|
|
|
* |
256
|
|
|
* Updates and returns a gauge with the updates applied. |
257
|
|
|
* |
258
|
|
|
* @param string $id |
259
|
|
|
* @param string $title |
260
|
|
|
* @param string|\DateTimeZone $tz |
261
|
|
|
* @param string $allowedHosts (Optional) |
262
|
|
|
* |
263
|
|
|
* @return Response |
264
|
|
|
*/ |
265
|
1 |
|
public function updateGauge(string $id, string $title, $tz, string $allowedHosts = null) : Response |
266
|
|
|
{ |
267
|
1 |
|
return $this->makeApiCall('PUT', 'gauges/' . $id, $this->formatGaugeParameters($title, $tz, $allowedHosts)); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Delete a Gauge |
272
|
|
|
* |
273
|
|
|
* Permanently deletes a gauge. |
274
|
|
|
* |
275
|
|
|
* @param string $id |
276
|
|
|
* |
277
|
|
|
* @return Response |
278
|
|
|
*/ |
279
|
1 |
|
public function deleteGauge(string $id) : Response |
280
|
|
|
{ |
281
|
1 |
|
return $this->makeApiCall('DELETE', 'gauges/' . $id); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* List Shares |
286
|
|
|
* |
287
|
|
|
* Lists the people that have access to a Gauge. |
288
|
|
|
* |
289
|
|
|
* @param string $id |
290
|
|
|
* |
291
|
|
|
* @return Response |
292
|
|
|
*/ |
293
|
1 |
|
public function listShares(string $id) : Response |
294
|
|
|
{ |
295
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/shares'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Share a Gauge |
300
|
|
|
* |
301
|
|
|
* Shares gauge with a person by their email. Any valid email will work |
302
|
|
|
* and will receive an invite even if there is no existing Gauges user |
303
|
|
|
* with that email. |
304
|
|
|
* |
305
|
|
|
* @param string $id |
306
|
|
|
* @param string $email |
307
|
|
|
* |
308
|
|
|
* @return Response |
309
|
|
|
*/ |
310
|
1 |
|
public function shareGauge(string $id, string $email) : Response |
311
|
|
|
{ |
312
|
|
|
$params = array( |
313
|
1 |
|
'email' => $email |
314
|
|
|
); |
315
|
|
|
|
316
|
1 |
|
return $this->makeApiCall('POST', 'gauges/' . $id . '/shares', $params); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Top Content |
321
|
|
|
* |
322
|
|
|
* Gets top content for a gauge, paginated. |
323
|
|
|
* |
324
|
|
|
* @param string $id |
325
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
326
|
|
|
* @param string $group (Optional) Either "day" or "month". Default is "day". |
327
|
|
|
* @param int $page (Optional) |
328
|
|
|
* |
329
|
|
|
* @return Response |
330
|
|
|
*/ |
331
|
2 |
|
public function topContent(string $id, $date = null, string $group = null, int $page = null) : Response |
332
|
|
|
{ |
333
|
2 |
|
$params = $this->formatCommonParameters($date, $page); |
334
|
2 |
|
if (isset($group)) { |
335
|
2 |
|
$group = strtolower($group); |
336
|
2 |
|
if ($group !== 'month' && $group !== 'day') { |
337
|
1 |
|
throw new \InvalidArgumentException( |
338
|
1 |
|
'Invalid group parameter for "topContent" call. Allowed values are "day" or "month". Actual value is : ' . $group |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
1 |
|
$params['group'] = $group; |
343
|
|
|
} |
344
|
|
|
|
345
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/content', $params); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Un-share Gauge |
350
|
|
|
* |
351
|
|
|
* @param string $id |
352
|
|
|
* @param string $user_id |
353
|
|
|
* |
354
|
|
|
* @return Response |
355
|
|
|
*/ |
356
|
1 |
|
public function unshareGauge(string $id, string $user_id) : Response |
357
|
|
|
{ |
358
|
1 |
|
return $this->makeApiCall('DELETE', 'gauges/' . $id . '/shares/' . $user_id); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Top Referrers |
363
|
|
|
* |
364
|
|
|
* Gets top referrers for a gauge, paginated. |
365
|
|
|
* |
366
|
|
|
* @param string $id |
367
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
368
|
|
|
* @param int $page (Optional) |
369
|
|
|
* |
370
|
|
|
* @return Response |
371
|
|
|
*/ |
372
|
1 |
|
public function topReferrers(string $id, $date = null, int $page = null) : Response |
373
|
|
|
{ |
374
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/referrers', $this->formatCommonParameters($date, $page)); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Traffic |
379
|
|
|
* |
380
|
|
|
* Gets traffic for a gauge. |
381
|
|
|
* |
382
|
|
|
* @param string $id |
383
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
384
|
|
|
* |
385
|
|
|
* @return Response |
386
|
|
|
*/ |
387
|
1 |
|
public function traffic(string $id, $date = null) : Response |
388
|
|
|
{ |
389
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/traffic', $this->formatCommonParameters($date)); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Browser Resolutions |
394
|
|
|
* |
395
|
|
|
* Gets browsers heights, browser widths, and screen widths for a gauge. |
396
|
|
|
* |
397
|
|
|
* @param string $id |
398
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
399
|
|
|
* |
400
|
|
|
* @return Response |
401
|
|
|
*/ |
402
|
1 |
|
public function browserResolutions(string $id, $date = null) : Response |
403
|
|
|
{ |
404
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/resolutions', $this->formatCommonParameters($date)); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Technology |
409
|
|
|
* |
410
|
|
|
* Gets browsers and platforms for a gauge. |
411
|
|
|
* |
412
|
|
|
* @param string $id |
413
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
414
|
|
|
* |
415
|
|
|
* @return Response |
416
|
|
|
*/ |
417
|
1 |
|
public function technology(string $id, $date = null) : Response |
418
|
|
|
{ |
419
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/technology', $this->formatCommonParameters($date)); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Search Terms |
424
|
|
|
* |
425
|
|
|
* Gets search terms for a gauge, paginated. |
426
|
|
|
* |
427
|
|
|
* @param string $id |
428
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
429
|
|
|
* @param int $page (Optional) |
430
|
|
|
* |
431
|
|
|
* @return Response |
432
|
|
|
*/ |
433
|
1 |
|
public function searchTerms(string $id, $date = null, int $page = null) : Response |
434
|
|
|
{ |
435
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/terms', $this->formatCommonParameters($date, $page)); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Search Engines |
440
|
|
|
* |
441
|
|
|
* Gets search engines for a gauge. |
442
|
|
|
* |
443
|
|
|
* @param string $id |
444
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
445
|
|
|
* |
446
|
|
|
* @return Response |
447
|
|
|
*/ |
448
|
1 |
|
public function searchEngines(string $id, $date = null) : Response |
449
|
|
|
{ |
450
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/engines', $this->formatCommonParameters($date)); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Locations |
455
|
|
|
* |
456
|
|
|
* Gets locations for a gauge. |
457
|
|
|
* |
458
|
|
|
* @param string $id |
459
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
460
|
|
|
* |
461
|
|
|
* @return Response |
462
|
|
|
*/ |
463
|
1 |
|
public function locations(string $id, $date = null) : Response |
464
|
|
|
{ |
465
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/locations', $this->formatCommonParameters($date)); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Browser stats |
470
|
|
|
* |
471
|
|
|
* Get the browser statistics in a format used with the browserlist module. |
472
|
|
|
* (See https://github.com/ai/browserslist) |
473
|
|
|
* |
474
|
|
|
* @param string $id |
475
|
|
|
* @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
476
|
|
|
* |
477
|
|
|
* @return Response |
478
|
|
|
*/ |
479
|
1 |
|
public function browserStats(string $id, $date = null) : Response |
480
|
|
|
{ |
481
|
1 |
|
return $this->makeApiCall('GET', 'gauges/' . $id . '/browserstats', $this->formatCommonParameters($date)); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Make the actual gauges API call. |
486
|
|
|
* |
487
|
|
|
* @param string $method [GET|POST|PUT|DELETE] |
488
|
|
|
* @param string $path |
489
|
|
|
* @param array $params |
490
|
|
|
* |
491
|
|
|
* @return Response |
492
|
|
|
*/ |
493
|
23 |
|
protected function makeApiCall(string $method, string $path, array $params = array()) : Response |
494
|
|
|
{ |
495
|
|
|
// Format method. |
496
|
23 |
|
$method = strtoupper($method); |
497
|
|
|
|
498
|
|
|
// Make API call. |
499
|
23 |
|
return $this->getHttpClient()->request( |
500
|
|
|
$method, |
501
|
|
|
$path, |
502
|
|
|
array( |
503
|
23 |
|
'headers' => array('X-Gauges-Token' => $this->token), |
504
|
23 |
|
'query' => $params |
505
|
|
|
) |
506
|
|
|
); |
507
|
|
|
} |
508
|
|
|
|
509
|
11 |
|
private function formatCommonParameters($date = null, int $page = null) : array |
510
|
|
|
{ |
511
|
11 |
|
$params = array(); |
512
|
11 |
|
if (isset($date)) { |
513
|
10 |
|
if (!$date instanceof \DateTime) { |
514
|
10 |
|
$date = new \DateTime($date); |
515
|
|
|
} |
516
|
|
|
|
517
|
10 |
|
$params['date'] = $date->format('Y-m-d'); |
518
|
|
|
} |
519
|
|
|
|
520
|
11 |
|
if (isset($page)) { |
521
|
4 |
|
$params['page'] = $page; |
522
|
|
|
} |
523
|
|
|
|
524
|
11 |
|
return $params; |
525
|
|
|
} |
526
|
|
|
|
527
|
3 |
|
private function formatGaugeParameters(string $title, $tz, string $allowedHosts = null) : array |
528
|
|
|
{ |
529
|
3 |
|
if (empty($title)) { |
530
|
1 |
|
throw new \InvalidArgumentException('Gauge title must not be empty.'); |
531
|
|
|
} |
532
|
|
|
|
533
|
2 |
|
if (!$tz instanceof \DateTimeZone) { |
534
|
2 |
|
$tz = new \DateTimeZone($tz); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
$params = array( |
538
|
2 |
|
'title' => $title, |
539
|
2 |
|
'tz' => $tz->getName() |
540
|
|
|
); |
541
|
2 |
|
if (isset($allowedHosts)) { |
542
|
2 |
|
$params['allowed_hosts'] = $allowedHosts; |
543
|
|
|
} |
544
|
|
|
|
545
|
2 |
|
return $params; |
546
|
|
|
} |
547
|
|
|
} |
548
|
|
|
|