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