Completed
Push — master ( 391ffc...8971ff )
by Kevin
04:09
created

Request::makeApiCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 3
crap 1
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 Psr\Log\LoggerAwareInterface;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\LogLevel;
13
14
/**
15
 * Used to make Gauges API calls.
16
 */
17
class Request implements LoggerAwareInterface
18
{
19
    const URI = 'https://secure.gaug.es/';
20
21
    /** @var null|ClientInterface */
22
    private $client;
23
24
    /** @var HandlerStack */
25
    private $handlerStack;
26
27
    /** @var LoggerInterface */
28
    private $logger;
29
30
    /** @var string */
31
    private $logLevel;
32
33
    /** @var MessageFormatter */
34
    private $messageFormatter;
35
36
    /** @var array */
37
    private $options;
38
39
    /** @var string */
40
    protected $token;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param string $token     Your API token
46
     * @param array  $options   See Guzzle documentation (proxy, etc.)
47
     */
48 24
    public function __construct($token, array $options = array())
49
    {
50 24
        $this->client = null;
51 24
        $this->handlerStack = HandlerStack::create();
52 24
        $this->logger = null;
53 24
        $this->logLevel = LogLevel::INFO;
54 24
        $this->messageFormatter = new MessageFormatter();
55 24
        $this->options = array_merge(
56 24
            array('timeout' => 10),
57
            $options
58 24
        );
59 24
        $this->options['base_uri'] = self::URI;
60 24
        $this->token = $token;
61 24
    }
62
63
    /**
64
     * Getter for the HTTP client.
65
     *
66
     * @return Client
67
     */
68 23
    protected function getHttpClient()
69
    {
70 23
        if ($this->client === null) {
71 23
            if ($this->logger instanceof LoggerInterface) {
72 23
                $this->handlerStack->push(
73 23
                    Middleware::log(
74 23
                        $this->logger,
75 23
                        $this->messageFormatter,
76 23
                        $this->logLevel
77 23
                    )
78 23
                );
79 23
            }
80
81 23
            $this->options['handler'] = $this->handlerStack;
82 23
            $this->client = new Client($this->options);
83 23
        }
84
85 23
        return $this->client;
86
    }
87
88
    /**
89
     * Setter for the Guzzle HandlerStack
90
     */
91 23
    public function setHandlerStack(HandlerStack $handlerStack)
92
    {
93 23
        $this->handlerStack = $handlerStack;
94
95 23
        return $this;
96
    }
97
98 24
    public function setLogger(LoggerInterface $logger)
99
    {
100 24
        $this->logger = $logger;
101
102 24
        return $this;
103
    }
104
105 23
    public function setLogLevel($logLevel)
106
    {
107 23
        $this->logLevel = $logLevel;
108
109 23
        return $this;
110
    }
111
112
    /**
113
     * Setter for the Guzzle MessageFormatter
114
     */
115 24
    public function setMessageFormatter(MessageFormatter $messageFormatter)
116
    {
117 24
        $this->messageFormatter = $messageFormatter;
118
119 24
        return $this;
120
    }
121
122
    /**
123
     * Get Your Information
124
     *
125
     * Returns your information.
126
     *
127
     * @return GuzzleHttp\Psr7\Response
128
     */
129 2
    public function me()
130
    {
131 2
        return $this->makeApiCall('GET', 'me');
132
    }
133
134
    /**
135
     * Update Your Information
136
     *
137
     * Updates and returns your information with the updates applied.
138
     *
139
     * @param string $first_name Your first name.
140
     * @param string $last_name  Your last name.
141
     *
142
     * @return GuzzleHttp\Psr7\Response
143
     */
144 1 View Code Duplication
    public function update_me($first_name = null, $last_name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146 1
        $params = array();
147
148 1
        if (isset($first_name)) {
149 1
            $params['first_name'] = (string) $first_name;
150 1
        }
151
152 1
        if (isset($last_name)) {
153 1
            $params['last_name'] = (string) $last_name;
154 1
        }
155
156 1
        return $this->makeApiCall('PUT', 'me', $params);
157
    }
158
159
    /**
160
     * API Client List
161
     *
162
     * Returns an array of your API clients.
163
     *
164
     * @return GuzzleHttp\Psr7\Response
165
     */
166 1
    public function list_clients()
167
    {
168 1
        return $this->makeApiCall('GET', 'clients');
169
    }
170
171
    /**
172
     * Creating an API Client
173
     *
174
     * Creates an API client, which can be used to authenticate against
175
     * the Gaug.es API.
176
     *
177
     * @param string $description Short description for the key
178
     *
179
     * @return GuzzleHttp\Psr7\Response
180
     */
181 1
    public function create_client($description = null)
182
    {
183 1
        $params = array();
184
185 1
        if (isset($description)) {
186 1
            $params['description'] = (string) $description;
187 1
        }
188
189 1
        return $this->makeApiCall('POST', 'clients', $params);
190
    }
191
192
    /**
193
     * Delete an API Client
194
     *
195
     * Permanently deletes an API client key.
196
     *
197
     * @param string $id
198
     *
199
     * @return GuzzleHttp\Psr7\Response
200
     */
201 1
    public function delete_client($id)
202
    {
203 1
        return $this->makeApiCall('DELETE', 'clients/' . $id);
204
    }
205
206
    /**
207
     * Gauges List
208
     *
209
     * Returns an array of your gauges, with recent traffic included.
210
     *
211
     * @return GuzzleHttp\Psr7\Response
212
     */
213 1 View Code Duplication
    public function list_gauges($page = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
    {
215 1
        $params = array();
216
217 1
        if (isset($page)) {
218 1
            $params['page'] = (int) $page;
219 1
        }
220
221 1
        return $this->makeApiCall('GET', 'gauges', $params);
222
    }
223
224
    /**
225
     * Create a New Gauge
226
     *
227
     * Creates a gauge.
228
     *
229
     * @param string $title
230
     * @param string $tz
231
     * @param string $allowedHosts (Optional)
232
     *
233
     * @return GuzzleHttp\Psr7\Response
234
     */
235 1 View Code Duplication
    public function create_gauge($title, $tz, $allowedHosts = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
    {
237
        $params = array(
238 1
            'title' => $title,
239
            'tz' => $tz
240 1
        );
241
242 1
        if (isset($allowedHosts)) {
243 1
            $params['allowed_hosts'] = (string) $allowedHosts;
244 1
        }
245
246 1
        return $this->makeApiCall('POST', 'gauges', $params);
247
    }
248
249
    /**
250
     * Gauge Detail
251
     *
252
     * Gets details for a gauge.
253
     *
254
     * @param string $id
255
     *
256
     * @return GuzzleHttp\Psr7\Response
257
     */
258 1
    public function gauge_detail($id)
259
    {
260 1
        return $this->makeApiCall('GET', 'gauges/' . $id);
261
    }
262
263
    /**
264
     * Update a Gauge
265
     *
266
     * Updates and returns a gauge with the updates applied.
267
     *
268
     * @param string $id
269
     * @param string $title
270
     * @param string $tz
271
     * @param string $allowedHosts (Optional)
272
     *
273
     * @return GuzzleHttp\Psr7\Response
274
     */
275 1 View Code Duplication
    public function update_gauge($id, $title, $tz, $allowedHosts = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
276
    {
277
        $params = array(
278 1
            'title' => $title,
279
            'tz' => $tz
280 1
        );
281
282 1
        if (isset($allowedHosts)) {
283 1
            $params['allowed_hosts'] = (string) $allowedHosts;
284 1
        }
285
286 1
        return $this->makeApiCall('PUT', 'gauges/' . $id, $params);
287
    }
288
289
    /**
290
     * Delete a Gauge
291
     *
292
     * Permanently deletes a gauge.
293
     *
294
     * @param string $id
295
     *
296
     * @return GuzzleHttp\Psr7\Response
297
     */
298 1
    public function delete_gauge($id)
299
    {
300 1
        return $this->makeApiCall('DELETE', 'gauges/' . $id);
301
    }
302
303
    /**
304
     * List Shares
305
     *
306
     * Lists the people that have access to a Gauge.
307
     *
308
     * @param string $id
309
     *
310
     * @return GuzzleHttp\Psr7\Response
311
     */
312 1
    public function list_shares($id)
313
    {
314 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/shares');
315
    }
316
317
    /**
318
     * Share a Gauge
319
     *
320
     * Shares gauge with a person by their email. Any valid email will work
321
     * and will receive an invite even if there is no existing Gauges user
322
     * with that email.
323
     *
324
     * @param string $id
325
     * @param string $email
326
     *
327
     * @return GuzzleHttp\Psr7\Response
328
     */
329 1
    public function share_gauge($id, $email)
330
    {
331
        $params = array(
332
            'email' => $email
333 1
        );
334
335 1
        return $this->makeApiCall('POST', 'gauges/' . $id . '/shares', $params);
336
    }
337
338
    /**
339
     * Un-share Gauge
340
     *
341
     * @param string $id
342
     * @param string $user_id
343
     *
344
     * @return GuzzleHttp\Psr7\Response
345
     */
346 1
    public function unshare_gauge($id, $user_id)
347
    {
348 1
        return $this->makeApiCall('DELETE', 'gauges/' . $id . '/shares/' . $user_id);
349
    }
350
351
    /**
352
     * Top Content
353
     *
354
     * Gets top content for a gauge, paginated.
355
     *
356
     * @param string $id
357
     * @param string $date  (Optional) Date in format YYYY-MM-DD
358
     * @param int    $page  (Optional)
359
     * @param string $group (Optional) Either "day" or "month".  Default is "day".
360
     *
361
     * @return GuzzleHttp\Psr7\Response
362
     */
363 1
    public function top_content($id, $date = null, $page = null, $group = null)
364
    {
365 1
        $params = array();
366
367 1
        if (isset($date)) {
368 1
            $params['date'] = (string) $date;
369 1
        }
370
371 1
        if (isset($group)) {
372 1
            $params['group'] = (string) $group;
373 1
        }
374
375 1
        if (isset($page)) {
376 1
            $params['page'] = (int) $page;
377 1
        }
378
379 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/content', $params);
380
    }
381
382
    /**
383
     * Top Referrers
384
     *
385
     * Gets top referrers for a gauge, paginated.
386
     *
387
     * @param string $id
388
     * @param string $date (Optional) Date in format YYYY-MM-DD
389
     * @param int    $page (Optional)
390
     *
391
     * @return GuzzleHttp\Psr7\Response
392
     */
393 1 View Code Duplication
    public function top_referrers($id, $date = null, $page = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
394
    {
395 1
        $params = array();
396
397 1
        if (isset($date)) {
398 1
            $params['date'] = (string) $date;
399 1
        }
400
401 1
        if (isset($page)) {
402 1
            $params['page'] = (int) $page;
403 1
        }
404
405 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/referrers', $params);
406
    }
407
408
    /**
409
     * Traffic
410
     *
411
     * Gets traffic for a gauge.
412
     *
413
     * @param string $id
414
     * @param string $date (Optional) Date in format YYYY-MM-DD
415
     *
416
     * @return GuzzleHttp\Psr7\Response
417
     */
418 1 View Code Duplication
    public function traffic($id, $date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
419
    {
420 1
        $params = array();
421
422 1
        if (isset($date)) {
423 1
            $params['date'] = (string) $date;
424 1
        }
425
426 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/traffic', $params);
427
    }
428
429
    /**
430
     * Browser Resolutions
431
     *
432
     * Gets browsers heights, browser widths, and screen widths for a gauge.
433
     *
434
     * @param string $id
435
     * @param string $date (Optional) Date in format YYYY-MM-DD
436
     *
437
     * @return GuzzleHttp\Psr7\Response
438
     */
439 1 View Code Duplication
    public function browser_resolutions($id, $date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
440
    {
441 1
        $params = array();
442
443 1
        if (isset($date)) {
444 1
            $params['date'] = (string) $date;
445 1
        }
446
447 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/resolutions', $params);
448
    }
449
450
    /**
451
     * Technology
452
     *
453
     * Gets browsers and platforms for a gauge.
454
     *
455
     * @param string $id
456
     * @param string $date (Optional) Date in format YYYY-MM-DD
457
     *
458
     * @return GuzzleHttp\Psr7\Response
459
     */
460 1 View Code Duplication
    public function technology($id, $date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
461
    {
462 1
        $params = array();
463
464 1
        if (isset($date)) {
465 1
            $params['date'] = (string) $date;
466 1
        }
467
468 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/technology', $params);
469
    }
470
471
    /**
472
     * Search Terms
473
     *
474
     * Gets search terms for a gauge, paginated.
475
     *
476
     * @param string $id
477
     * @param string $date (Optional) Date in format YYYY-MM-DD
478
     * @param int    $page (Optional)
479
     *
480
     * @return GuzzleHttp\Psr7\Response
481
     */
482 1 View Code Duplication
    public function search_terms($id, $date = null, $page = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
483
    {
484 1
        $params = array();
485
486 1
        if (isset($date)) {
487 1
            $params['date'] = (string) $date;
488 1
        }
489
490 1
        if (isset($page)) {
491 1
            $params['page'] = (int) $page;
492 1
        }
493
494 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/terms', $params);
495
    }
496
497
    /**
498
     * Search Engines
499
     *
500
     * Gets search engines for a gauge.
501
     *
502
     * @param string $id
503
     * @param string $date (Optional) Date in format YYYY-MM-DD
504
     *
505
     * @return GuzzleHttp\Psr7\Response
506
     */
507 1 View Code Duplication
    public function search_engines($id, $date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
508
    {
509 1
        $params = array();
510
511 1
        if (isset($date)) {
512 1
            $params['date'] = (string) $date;
513 1
        }
514
515 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/engines', $params);
516
    }
517
518
    /**
519
     * Locations
520
     *
521
     * Gets locations for a gauge.
522
     *
523
     * @param string $id
524
     * @param string $date (Optional) Date in format YYYY-MM-DD
525
     *
526
     * @return GuzzleHttp\Psr7\Response
527
     */
528 1 View Code Duplication
    public function locations($id, $date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
529
    {
530 1
        $params = array();
531
532 1
        if (isset($date)) {
533 1
            $params['date'] = (string) $date;
534 1
        }
535
536 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/locations', $params);
537
    }
538
539
    /**
540
     * Browser stats
541
     *
542
     * Get the browser statistics in a format used with the browserlist module.
543
     * (See https://github.com/ai/browserslist)
544
     *
545
     * @param string $id
546
     * @param string $date (Optional) Date in format YYYY-MM-DD
547
     *
548
     * @return GuzzleHttp\Psr7\Response
549
     */
550 1 View Code Duplication
    public function browser_stats($id, $date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
551
    {
552 1
        $params = array();
553
554 1
        if (isset($date)) {
555 1
            $params['date'] = (string) $date;
556 1
        }
557
558 1
        return $this->makeApiCall('GET', 'gauges/' . $id . '/browserstats', $params);
559
    }
560
561
    /**
562
     * Make the actual gauges API call.
563
     *
564
     * @param string $method       [GET|POST|PUT|DELETE]
565
     * @param string $path
566
     * @param array  $params
567
     *
568
     * @return GuzzleHttp\Psr7\Response
569
     */
570 23
    protected function makeApiCall($method, $path, array $params = array())
571
    {
572
        // Format method.
573 23
        $method = strtoupper($method);
574
575
        // Make API call.
576 23
        return $this->getHttpClient()->request(
577 23
            $method,
578 23
            $path,
579
            array(
580 23
                'headers' => array('X-Gauges-Token' => $this->token),
581
                'query' => $params
582 23
            )
583 23
        );
584
    }
585
}
586