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.
Test Failed
Push — 2.0 ( 0b74f1...31a191 )
by Nico
03:34
created

Client::SetHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
namespace Datatrics\API;
3
4
use Datatrics\API\Modules\Apikey;
5
use Datatrics\API\Modules\Behavior;
6
use Datatrics\API\Modules\Billing;
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\Subscription;
25
use Datatrics\API\Modules\Template;
26
use Datatrics\API\Modules\Theme;
27
use Datatrics\API\Modules\Touchpoint;
28
use Datatrics\API\Modules\Tracker;
29
use Datatrics\API\Modules\Tric;
30
use Datatrics\API\Modules\Trigger;
31
use Datatrics\API\Modules\User;
32
use Datatrics\API\Modules\Webhook;
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 string
80
     */
81
    private $_projectId;
82
83
    /**
84
     * @var Apikey
85
     */
86
    public $Apikey;
87
88
    /**
89
     * @var Behavior
90
     */
91
    public $Behavior;
92
93
    /**
94
     * @var Billing
95
     */
96
    public $Billing;
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 Subscription
185
     */
186
    public $Subscription;
187
188
    /**
189
     * @var Template
190
     */
191
    public $Template;
192
193
    /**
194
     * @var Theme
195
     */
196
    public $Theme;
197
198
    /**
199
     * @var Touchpoint
200
     */
201
    public $Touchpoint;
202
203
    /**
204
     * @var Tracker
205
     */
206
    public $Tracker;
207
208
    /**
209
     * @var Tric
210
     */
211
    public $Tric;
212
213
    /**
214
     * @var Trigger
215
     */
216
    public $Trigger;
217
218
    /**
219
     * @var User
220
     */
221
    public $User;
222
223
    /**
224
     * @var Webhook
225
     */
226
    public $Webhook;
227
228
    /**
229
     * Create a new API instance
230
     *
231
     * @param string $apiKey    The API key
232
     * @param string $projectId The Project id
233
     */
234 16
    public function __construct($apiKey, $projectId = null)
235
    {
236 16
        $this->SetApiKey($apiKey);
237 16
        $this->SetProjectId($projectId);
238 16
        $this->_RegisterModules();
239 16
    }
240
241
    /**
242
     * Get the current API version
243
     *
244
     * @return string $api_version
245
     */
246 6
    public function GetApiVersion()
247
    {
248 6
        return $this->_api_version;
249
    }
250
251
    /**
252
     * Get the API endpoint
253
     *
254
     * @return string
255
     */
256 7
    public function GetApiEndpoint()
257
    {
258 7
        return $this->_api_endpoint;
259
    }
260
261
    /**
262
     * Set the API endpoint
263
     *
264
     * @param string $api_endpoint
265
     * @return  Client
266
     */
267 1
    public function SetApiEndpoint($api_endpoint)
268
    {
269 1
        $this->_api_endpoint = $api_endpoint;
270 1
        $this->_RegisterModules();
271 1
        return $this;
272
    }
273
274
    /**
275
     * Get the API key
276
     *
277
     * @return string $api_key
278
     */
279 8
    public function GetApiKey()
280
    {
281 8
        return $this->_api_key;
282
    }
283
284
    /**
285
     * Get the API key
286
     *
287
     * @param string $api_key
288
     * @return Client
289
     */
290 16
    public function SetApiKey($api_key)
291
    {
292 16
        $this->_api_key = $api_key;
293 16
        $this->_RegisterModules();
294 16
        return $this;
295
    }
296
297
    /**
298
     * Get the API project id
299
     *
300
     * @return string $projectId
301
     */
302 16
    public function GetProjectId()
303
    {
304 16
        return $this->_projectId;
305
    }
306
307
    /**
308
     * Set the API project id
309
     *
310
     * @param string $projectId
311
     * @return Client
312
     */
313 16
    public function SetProjectId($projectId)
314
    {
315 16
        $this->_projectId = $projectId;
316 16
        $this->_RegisterModules();
317 16
        return $this;
318
    }
319
320
    /**
321
     * Register Modules
322
     *
323
     * @return void
324
     */
325 16
    private function _RegisterModules()
326
    {
327 16
        $this->Apikey = new Apikey($this);
328 16
        $this->Behavior = new Behavior($this);
329 16
        $this->Billing = new Billing($this);
330 16
        $this->Box = new Box($this);
331 16
        $this->Bucket = new Bucket($this);
332 16
        $this->Campaign = new Campaign($this);
333 16
        $this->Card = new Card($this);
334 16
        $this->Channel = new Channel($this);
335 16
        $this->Content = new Content($this);
336 16
        $this->Geo = new Geo($this);
337 16
        $this->Goal = new Goal($this);
338 16
        $this->Interaction = new Interaction($this);
339 16
        $this->Journey = new Journey($this);
340 16
        $this->Link = new Link($this);
341 16
        $this->NextBestAction = new NextBestAction($this);
342 16
        $this->Profile = new Profile($this);
343 16
        $this->Project = new Project($this);
344 16
        $this->Sale = new Sale($this);
345 16
        $this->Scorecard = new Scorecard($this);
346 16
        $this->Segment = new Segment($this);
347 16
        $this->Subscription = new Subscription($this);
348 16
        $this->Template = new Template($this);
349 16
        $this->Theme = new Theme($this);
350 16
        $this->Touchpoint = new Touchpoint($this);
351 16
        $this->Tracker = new Tracker($this);
352 16
        $this->Tric = new Tric($this);
353 16
        $this->Trigger = new Trigger($this);
354 16
        $this->User = new User($this);
355 16
        $this->Webhook = new Webhook($this);
356 16
    }
357
358
    /**
359
     * Define the HTTP headers
360
     *
361
     * @return array
362
     */
363 4
    private function _GetHttpHeaders()
364
    {
365 4
        $user_agent = 'Datatrics/API '.self::CLIENT_VERSION;
366
        return [
367 4
            'accept' => 'application/json',
368 4
            'content-type' => 'application/json',
369 4
            'user-agent' => $user_agent,
370 4
            'x-apikey' => $this->GetApiKey(),
371 4
            'x-client-name' => $user_agent,
372 4
            'x-datatrics-client-info' => php_uname()
373
        ];
374
    }
375
376
    /**
377
     * @throws \Exception
378
     * @return boolean
379
     */
380 5
    public function CheckApiKey()
381
    {
382 5
        if (empty($this->GetApiKey())) {
383
            throw new \Exception('You have not set an api key. Please use setApiKey() to set the API key.');
384
        }
385 5
        return true;
386
    }
387
388
    /**
389
     * @param $url
390
     * @param null|array $payload
391
     * @return string
392
     */
393 5
    public function GetUrl($url, $payload = array())
394
    {
395 5
        $url = $this->GetApiEndpoint()."/".$this->GetApiVersion().$url;
396 5
        if (count($payload)) {
397
            $url .= "?".http_build_query($payload);
398
        }
399 5
        return $url;
400
    }
401
402
    /**
403
     * @param string $method    HTTP Method
404
     * @param string $url       The url
405
     * @param array $payload    The Payload
406
     * @throws \Exception
407
     * @return array
408
     */
409 6
    public function BuildRequest($method, $url, $payload = array())
410
    {
411 6
        if($method == self::HTTP_POST || $method == self::HTTP_PUT){
412 2
            if (!$payload || !is_array($payload))
0 ignored issues
show
Bug Best Practice introduced by
The expression $payload of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
413
            {
414 2
                throw new \Exception('Invalid payload', 100);
415
            }
416
            $curlOptions = array(
417
                CURLOPT_URL           => $this->getUrl($url),
418
                CURLOPT_CUSTOMREQUEST => strtoupper($method),
419
                CURLOPT_POSTFIELDS    => json_encode($payload),
420
            );
421 4
        }elseif($method == self::HTTP_DELETE){
422
            $curlOptions = array(
423 1
                CURLOPT_URL           => $this->getUrl($url),
424 1
                CURLOPT_CUSTOMREQUEST => self::HTTP_DELETE,
425
            );
426
        }else{
427
            $curlOptions = array(
428 3
                CURLOPT_URL => $this->getUrl($url, $payload),
429 3
                CURLOPT_CUSTOMREQUEST => strtoupper($method)
430
            );
431
        }
432
433
        $curlOptions += array(
434 4
            CURLOPT_RETURNTRANSFER => true,
435 4
            CURLOPT_SSL_VERIFYPEER => false,
436 4
            CURLOPT_HTTPHEADER      => $this->_GetHttpHeaders()
437
        );
438 4
        return $curlOptions;
439
    }
440
441
    /**
442
     * @param string $method    HTTP Method
443
     * @param string $url       The url
444
     * @param array $payload    The Payload
445
     * @return mixed
446
     * @throws \Exception
447
     */
448 5
    public function SendRequest($method, $url, $payload = array())
449
    {
450 5
        $this->CheckApiKey();
451 5
        $curlOptions = $this->BuildRequest($method, $url, $payload);
452 3
        $curlHandle = curl_init();
453 3
        curl_setopt_array($curlHandle, $curlOptions);
454 3
        $responseBody = curl_exec($curlHandle);
455 3
        if (curl_errno($curlHandle))
456
        {
457
            throw new \Exception('Curl error: ' . curl_error($curlHandle), curl_errno($curlHandle));
458
        }
459 3
        $responseBody = json_decode($responseBody, true);
460 3
        if (json_last_error() !== JSON_ERROR_NONE) {
461
            throw new \Exception(json_last_error_msg());
462
        }
463 3
        $responseCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
464 3
        curl_close($curlHandle);
465 3
        if ($responseCode < 200 || $responseCode > 299)
466
        {
467 3
            if($responseBody && array_key_exists('error', $responseBody)){
468
                throw new \Exception($responseBody['error']['message'], $responseCode);
469
            }
470 3
            if($responseBody && array_key_exists('message', $responseBody)){
471 3
                throw new \Exception($responseBody['message'], $responseCode);
472
            }
473
            throw new \Exception('Something went wrong');
474
        }
475
        return $responseBody;
476
    }
477
478
    /**
479
     * @param string $url
480
     * @param array $payload
481
     * @return mixed
482
     */
483 1
    public function Post($url, $payload = array())
484
    {
485 1
        return $this->SendRequest(self::HTTP_POST, $url, $payload);
486
    }
487
488
    /**
489
     * @param string $url
490
     * @param array $payload
491
     * @return mixed
492
     */
493 1
    public function Get($url, $payload = array())
494
    {
495 1
        return $this->SendRequest(self::HTTP_GET, $url, $payload);
496
    }
497
498
    /**
499
     * @param string $url
500
     * @param array $payload
501
     * @return mixed
502
     */
503 1
    public function Put($url, $payload = array())
504
    {
505 1
        return $this->SendRequest(self::HTTP_PUT, $url, $payload);
506
    }
507
508
    /**
509
     * @param string $url
510
     * @param array $payload
511
     * @return mixed
512
     */
513 1
    public function Delete($url, $payload = array())
514
    {
515 1
        return $this->SendRequest(self::HTTP_DELETE, $url, $payload);
516
    }
517
}
518