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