GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 2.0 ( 3a9506...a8b622 )
by Nico
07:22 queued 05:45
created

Client::GetApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Datatrics\API;
3
4
use GuzzleHttp\Client as HttpClient;
5
use Datatrics\API\Modules\Apikey;
6
use Datatrics\API\Modules\Behavior;
7
use Datatrics\API\Modules\Billing;
8
use Datatrics\API\Modules\Box;
9
use Datatrics\API\Modules\Bucket;
10
use Datatrics\API\Modules\Campaign;
11
use Datatrics\API\Modules\Card;
12
use Datatrics\API\Modules\Channel;
13
use Datatrics\API\Modules\Content;
14
use Datatrics\API\Modules\Geo;
15
use Datatrics\API\Modules\Goal;
16
use Datatrics\API\Modules\Interaction;
17
use Datatrics\API\Modules\Journey;
18
use Datatrics\API\Modules\Link;
19
use Datatrics\API\Modules\NextBestAction;
20
use Datatrics\API\Modules\Profile;
21
use Datatrics\API\Modules\Project;
22
use Datatrics\API\Modules\Sale;
23
use Datatrics\API\Modules\Scorecard;
24
use Datatrics\API\Modules\Segment;
25
use Datatrics\API\Modules\Subscription;
26
use Datatrics\API\Modules\Template;
27
use Datatrics\API\Modules\Theme;
28
use Datatrics\API\Modules\Touchpoint;
29
use Datatrics\API\Modules\Tracker;
30
use Datatrics\API\Modules\Tric;
31
use Datatrics\API\Modules\Trigger;
32
use Datatrics\API\Modules\User;
33
use Datatrics\API\Modules\Webhook;
34
use GuzzleHttp\Psr7\Request;
35
36
class Client
37
{
38
    /**
39
     * @const Version of our client.
40
     */
41
    const CLIENT_VERSION = '2.0';
42
43
    /**
44
     * @const HTTP Method GET
45
     */
46
    const HTTP_GET = 'GET';
47
48
    /**
49
     * @const HTTP Method POST
50
     */
51
    const HTTP_POST = 'POST';
52
53
    /**
54
     * @const HTTP Method PUT
55
     */
56
    const HTTP_PUT = 'PUT';
57
58
    /**
59
     * @const HTTP Method DELETE
60
     */
61
    const HTTP_DELETE = 'DELETE';
62
63
    /**
64
     * Version of the remote API.
65
     *
66
     * @var string
67
     */
68
    private $_api_version = '2.0';
69
70
    /**
71
     * @var string
72
     */
73
    private $_api_endpoint = 'https://api.datatrics.com';
74
75
    /**
76
     * @var string
77
     */
78
    private $_api_key;
79
80
    /**
81
     * @var HttpClient
82
     */
83
    private $_http_client;
84
85
    /**
86
     * @var string
87
     */
88
    private $_projectId;
89
90
    /**
91
     * @var Apikey
92
     */
93
    public $Apikey;
94
95
    /**
96
     * @var Behavior
97
     */
98
    public $Behavior;
99
100
    /**
101
     * @var Billing
102
     */
103
    public $Billing;
104
105
    /**
106
     * @var Box
107
     */
108
    public $Box;
109
110
    /**
111
     * @var Bucket
112
     */
113
    public $Bucket;
114
115
    /**
116
     * @var Campaign
117
     */
118
    public $Campaign;
119
120
    /**
121
     * @var Card
122
     */
123
    public $Card;
124
125
    /**
126
     * @var Channel
127
     */
128
    public $Channel;
129
130
    /**
131
     * @var Content
132
     */
133
    public $Content;
134
135
    /**
136
     * @var Geo
137
     */
138
    public $Geo;
139
140
    /**
141
     * @var Goal
142
     */
143
    public $Goal;
144
145
    /**
146
     * @var Interaction
147
     */
148
    public $Interaction;
149
150
    /**
151
     * @var Journey
152
     */
153
    public $Journey;
154
155
    /**
156
     * @var Link
157
     */
158
    public $Link;
159
160
    /**
161
     * @var NextBestAction
162
     */
163
    public $NextBestAction;
164
165
    /**
166
     * @var Profile
167
     */
168
    public $Profile;
169
170
    /**
171
     * @var Project
172
     */
173
    public $Project;
174
175
    /**
176
     * @var Sale
177
     */
178
    public $Sale;
179
180
    /**
181
     * @var Scorecard
182
     */
183
    public $Scorecard;
184
185
    /**
186
     * @var Segment
187
     */
188
    public $Segment;
189
190
    /**
191
     * @var Subscription
192
     */
193
    public $Subscription;
194
195
    /**
196
     * @var Template
197
     */
198
    public $Template;
199
200
    /**
201
     * @var Theme
202
     */
203
    public $Theme;
204
205
    /**
206
     * @var Touchpoint
207
     */
208
    public $Touchpoint;
209
210
    /**
211
     * @var Tracker
212
     */
213
    public $Tracker;
214
215
    /**
216
     * @var Tric
217
     */
218
    public $Tric;
219
220
    /**
221
     * @var Trigger
222
     */
223
    public $Trigger;
224
225
    /**
226
     * @var User
227
     */
228
    public $User;
229
230
    /**
231
     * @var Webhook
232
     */
233
    public $Webhook;
234
235
    /**
236
     * Create a new API instance
237
     *
238
     * @param string $apiKey    The API key
239
     * @param string $projectId The Project id
240
     */
241 16
    public function __construct($apiKey, $projectId = null)
242
    {
243 16
        $this->SetApiKey($apiKey);
244 16
        $this->SetProjectId($projectId);
245 16
        $this->SetHttpClient();
246 16
        $this->_RegisterModules();
247 16
    }
248
249
    /**
250
     * Get the current API version
251
     *
252
     * @return string $api_version
253
     */
254 8
    public function GetApiVersion()
255
    {
256 8
        return $this->_api_version;
257
    }
258
259
    /**
260
     * Get the API endpoint
261
     *
262
     * @return string
263
     */
264 16
    public function GetApiEndpoint()
265
    {
266 16
        return $this->_api_endpoint;
267
    }
268
269
    /**
270
     * Set the API endpoint
271
     *
272
     * @param string $api_endpoint
273
     * @return  Client
274
     */
275 1
    public function SetApiEndpoint($api_endpoint)
276
    {
277 1
        $this->_api_endpoint = $api_endpoint;
278 1
        $this->SetHttpClient();
279 1
        $this->_RegisterModules();
280 1
        return $this;
281
    }
282
283
    /**
284
     * Get the API key
285
     *
286
     * @return string $api_key
287
     */
288 16
    public function GetApiKey()
289
    {
290 16
        return $this->_api_key;
291
    }
292
293
    /**
294
     * Get the API key
295
     *
296
     * @param string $api_key
297
     * @return Client
298
     */
299 16
    public function SetApiKey($api_key)
300
    {
301 16
        $this->_api_key = $api_key;
302 16
        $this->SetHttpClient();
303 16
        $this->_RegisterModules();
304 16
        return $this;
305
    }
306
307
    /**
308
     * Get the API project id
309
     *
310
     * @return string $projectId
311
     */
312 16
    public function GetProjectId()
313
    {
314 16
        return $this->_projectId;
315
    }
316
317
    /**
318
     * Set the API project id
319
     *
320
     * @param string $projectId
321
     * @return Client
322
     */
323 16
    public function SetProjectId($projectId)
324
    {
325 16
        $this->_projectId = $projectId;
326 16
        $this->SetHttpClient();
327 16
        $this->_RegisterModules();
328 16
        return $this;
329
    }
330
331
    /**
332
     * Register Modules
333
     *
334
     * @return void
335
     */
336 16
    private function _RegisterModules()
337
    {
338 16
        $this->Apikey = new Apikey($this);
339 16
        $this->Behavior = new Behavior($this);
340 16
        $this->Billing = new Billing($this);
341 16
        $this->Box = new Box($this);
342 16
        $this->Bucket = new Bucket($this);
343 16
        $this->Campaign = new Campaign($this);
344 16
        $this->Card = new Card($this);
345 16
        $this->Channel = new Channel($this);
346 16
        $this->Content = new Content($this);
347 16
        $this->Geo = new Geo($this);
348 16
        $this->Goal = new Goal($this);
349 16
        $this->Interaction = new Interaction($this);
350 16
        $this->Journey = new Journey($this);
351 16
        $this->Link = new Link($this);
352 16
        $this->NextBestAction = new NextBestAction($this);
353 16
        $this->Profile = new Profile($this);
354 16
        $this->Project = new Project($this);
355 16
        $this->Sale = new Sale($this);
356 16
        $this->Scorecard = new Scorecard($this);
357 16
        $this->Segment = new Segment($this);
358 16
        $this->Subscription = new Subscription($this);
359 16
        $this->Template = new Template($this);
360 16
        $this->Theme = new Theme($this);
361 16
        $this->Touchpoint = new Touchpoint($this);
362 16
        $this->Tracker = new Tracker($this);
363 16
        $this->Tric = new Tric($this);
364 16
        $this->Trigger = new Trigger($this);
365 16
        $this->User = new User($this);
366 16
        $this->Webhook = new Webhook($this);
367 16
    }
368
369
    /**
370
     * Setup the HTTP Client
371
     *
372
     * @return Client
373
     */
374 16
    private function SetHttpClient()
375
    {
376
        $config = [
377 16
            'base-uri' => $this->GetApiEndpoint(),
378 16
            'headers' => $this->_GetHttpClientHeaders()
379
        ];
380 16
        $this->_http_client = new HttpClient($config);
381 16
        return $this;
382
    }
383
384
    /**
385
     * @return HttpClient
386
     */
387 5
    private function GetHttpClient()
388
    {
389 5
        return $this->_http_client;
390
    }
391
392
    /**
393
     * Define the HTTP headers
394
     *
395
     * @return array
396
     */
397 16
    private function _GetHttpClientHeaders()
398
    {
399 16
        $user_agent = 'Datatrics/API '.self::CLIENT_VERSION;
400
        return [
401 16
            'accept' => 'application/json',
402 16
            'content-type' => 'application/json',
403 16
            'user-agent' => $user_agent,
404 16
            'x-apikey' => $this->GetApiKey(),
405 16
            'x-client-name' => $user_agent,
406 16
            'x-datatrics-client-info' => php_uname()
407
        ];
408
    }
409
410
    /**
411
     * @throws \Exception
412
     * @return boolean
413
     */
414 5
    public function CheckApiKey()
415
    {
416 5
        if (empty($this->GetApiKey())) {
417
            throw new \Exception('You have not set an api key. Please use setApiKey() to set the API key.');
418
        }
419 5
        return true;
420
    }
421
422
    /**
423
     * @param $url
424
     * @param null|array $payload
425
     * @return string
426
     */
427 7
    public function GetUrl($url, $payload = [])
428
    {
429 7
        $url = $this->GetApiEndpoint()."/".$this->GetApiVersion().$url;
430 7
        if (count($payload)) {
431
            $url .= "?".http_build_query($payload);
432
        }
433 7
        return $url;
434
    }
435
436
    /**
437
     * @param string $method    HTTP Method
438
     * @param string $url       The url
439
     * @param array $payload    The Payload
440
     * @return Request
441
     */
442 6
    public function BuildRequest($method, $url, $payload = [])
443
    {
444 6
        $body = null;
445 6
        if ($method == self::HTTP_GET) {
446 3
            $url = $this->GetUrl($url, $payload);
447 3
        } elseif ($method == self::HTTP_DELETE) {
448 1
            $url = $this->GetUrl($url, $payload);
449
        } else {
450 2
            $url = $this->GetUrl($url);
451 2
            if (count($payload)) {
452
                $body = json_encode($payload);
453
            }
454
        }
455 6
        return new Request($method, $url, $this->_GetHttpClientHeaders(), $body);
456
    }
457
458
    /**
459
     * @param string $method    HTTP Method
460
     * @param string $url       The url
461
     * @param array $payload    The Payload
462
     * @return mixed
463
     * @throws \Exception
464
     */
465 5
    public function SendRequest($method, $url, $payload = [])
466
    {
467 5
        $this->CheckApiKey();
468 5
        $request = $this->BuildRequest($method, $url, $payload);
469
        try {
470 5
            $response = $this->GetHttpClient()->send($request);
471 5
        } catch (\GuzzleHttp\Exception\ClientException $e) {
472 5
            if ($e->hasResponse()) {
473 5
                $body = json_decode($e->getResponse()->getBody(), true);
474 5
                if (isset($body['error']['message'])) {
475
                    throw new \Exception($body['error']['message'], $e->getResponse()->getStatusCode());
476
                }
477 5
                throw new \Exception($body['message'], $e->getResponse()->getStatusCode());
478
            }
479
            throw $e;
480
        } catch (\Exception $e) {
481
            throw $e;
482
        }
483
        $body = json_decode($response->getBody(), true);
484
        if (json_last_error() !== JSON_ERROR_NONE) {
485
            throw new \Exception(json_last_error_msg());
486
        }
487
        return $body;
488
    }
489
490
    /**
491
     * @param string $url
492
     * @param array $payload
493
     * @return mixed
494
     */
495 1
    public function Post($url, $payload = [])
496
    {
497 1
        return $this->SendRequest(self::HTTP_POST, $url, $payload);
498
    }
499
500
    /**
501
     * @param string $url
502
     * @param array $payload
503
     * @return mixed
504
     */
505 1
    public function Get($url, $payload = [])
506
    {
507 1
        return $this->SendRequest(self::HTTP_GET, $url, $payload);
508
    }
509
510
    /**
511
     * @param string $url
512
     * @param array $payload
513
     * @return mixed
514
     */
515 1
    public function Put($url, $payload = [])
516
    {
517 1
        return $this->SendRequest(self::HTTP_PUT, $url, $payload);
518
    }
519
520
    /**
521
     * @param string $url
522
     * @param array $payload
523
     * @return mixed
524
     */
525 1
    public function Delete($url, $payload = [])
526
    {
527 1
        return $this->SendRequest(self::HTTP_DELETE, $url, $payload);
528
    }
529
}
530