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.
Passed
Push — 2.0 ( 31a191...9e0ccd )
by Nico
01:41
created

Client::BuildRequest()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 15
cts 16
cp 0.9375
rs 8.439
cc 6
eloc 21
nc 4
nop 3
crap 6.0087
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 18
    public function __construct($apiKey, $projectId = null)
235
    {
236 18
        $this->SetApiKey($apiKey);
237 18
        $this->SetProjectId($projectId);
238 18
        $this->_RegisterModules();
239 18
    }
240
241
    /**
242
     * Get the current API version
243
     *
244
     * @return string $api_version
245
     */
246 10
    public function GetApiVersion()
247
    {
248 10
        return $this->_api_version;
249
    }
250
251
    /**
252
     * Get the API endpoint
253
     *
254
     * @return string
255
     */
256 11
    public function GetApiEndpoint()
257
    {
258 11
        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 10
    public function GetApiKey()
280
    {
281 10
        return $this->_api_key;
282
    }
283
284
    /**
285
     * Get the API key
286
     *
287
     * @param string $api_key
288
     * @return Client
289
     */
290 18
    public function SetApiKey($api_key)
291
    {
292 18
        $this->_api_key = $api_key;
293 18
        $this->_RegisterModules();
294 18
        return $this;
295
    }
296
297
    /**
298
     * Get the API project id
299
     *
300
     * @return string $projectId
301
     */
302 18
    public function GetProjectId()
303
    {
304 18
        return $this->_projectId;
305
    }
306
307
    /**
308
     * Set the API project id
309
     *
310
     * @param string $projectId
311
     * @return Client
312
     */
313 18
    public function SetProjectId($projectId)
314
    {
315 18
        $this->_projectId = $projectId;
316 18
        $this->_RegisterModules();
317 18
        return $this;
318
    }
319
320
    /**
321
     * Register Modules
322
     *
323
     * @return void
324
     */
325 18
    private function _RegisterModules()
326
    {
327 18
        $this->Apikey = new Apikey($this);
328 18
        $this->Behavior = new Behavior($this);
329 18
        $this->Billing = new Billing($this);
330 18
        $this->Box = new Box($this);
331 18
        $this->Bucket = new Bucket($this);
332 18
        $this->Campaign = new Campaign($this);
333 18
        $this->Card = new Card($this);
334 18
        $this->Channel = new Channel($this);
335 18
        $this->Content = new Content($this);
336 18
        $this->Geo = new Geo($this);
337 18
        $this->Goal = new Goal($this);
338 18
        $this->Interaction = new Interaction($this);
339 18
        $this->Journey = new Journey($this);
340 18
        $this->Link = new Link($this);
341 18
        $this->NextBestAction = new NextBestAction($this);
342 18
        $this->Profile = new Profile($this);
343 18
        $this->Project = new Project($this);
344 18
        $this->Sale = new Sale($this);
345 18
        $this->Scorecard = new Scorecard($this);
346 18
        $this->Segment = new Segment($this);
347 18
        $this->Subscription = new Subscription($this);
348 18
        $this->Template = new Template($this);
349 18
        $this->Theme = new Theme($this);
350 18
        $this->Touchpoint = new Touchpoint($this);
351 18
        $this->Tracker = new Tracker($this);
352 18
        $this->Tric = new Tric($this);
353 18
        $this->Trigger = new Trigger($this);
354 18
        $this->User = new User($this);
355 18
        $this->Webhook = new Webhook($this);
356 18
    }
357
358
    /**
359
     * Define the HTTP headers
360
     *
361
     * @return array
362
     */
363 8
    private function _GetHttpHeaders()
364
    {
365 8
        $user_agent = 'Datatrics/API '.self::CLIENT_VERSION;
366
        return [
367 8
            'accept' => 'application/json',
368 8
            'content-type' => 'application/json',
369 8
            'user-agent' => $user_agent,
370 8
            'x-apikey' => $this->GetApiKey(),
371 8
            'x-client-name' => $user_agent,
372 8
            'x-datatrics-client-info' => php_uname()
373
        ];
374
    }
375
376
    /**
377
     * @throws \Exception
378
     * @return boolean
379
     */
380 7
    public function CheckApiKey()
381
    {
382 7
        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 7
        return true;
386
    }
387
388
    /**
389
     * @param $url
390
     * @param null|array $payload
391
     * @return string
392
     */
393 9
    public function GetUrl($url, $payload = array())
394
    {
395 9
        $url = $this->GetApiEndpoint()."/".$this->GetApiVersion().$url;
396 9
        if (count($payload)) {
397 2
            $url .= "?".http_build_query($payload);
398
        }
399 9
        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 8
    public function BuildRequest($method, $url, $payload = array())
410
    {
411 8
        if($method == self::HTTP_POST || $method == self::HTTP_PUT){
412 3
            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
                throw new \Exception('Invalid payload', 100);
415
            }
416
            $curlOptions = array(
417 3
                CURLOPT_URL           => $this->getUrl($url),
418 3
                CURLOPT_CUSTOMREQUEST => strtoupper($method),
419 3
                CURLOPT_POSTFIELDS    => json_encode($payload),
420
            );
421 5
        }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 4
                CURLOPT_URL => $this->getUrl($url, $payload),
429 4
                CURLOPT_CUSTOMREQUEST => strtoupper($method)
430
            );
431
        }
432
433
        $curlOptions += array(
434 8
            CURLOPT_RETURNTRANSFER => true,
435 8
            CURLOPT_SSL_VERIFYPEER => false,
436 8
            CURLOPT_HTTPHEADER      => $this->_GetHttpHeaders()
437
        );
438 8
        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 7
    public function SendRequest($method, $url, $payload = array())
449
    {
450 7
        $this->CheckApiKey();
451 7
        $curlOptions = $this->BuildRequest($method, $url, $payload);
452 7
        $curlHandle = curl_init();
453 7
        curl_setopt_array($curlHandle, $curlOptions);
454 7
        $responseBody = curl_exec($curlHandle);
455 7
        if (curl_errno($curlHandle))
456
        {
457
            throw new \Exception('Curl error: ' . curl_error($curlHandle), curl_errno($curlHandle));
458
        }
459 7
        $responseBody = json_decode($responseBody, true);
460 7
        if (json_last_error() !== JSON_ERROR_NONE) {
461
            throw new \Exception(json_last_error_msg());
462
        }
463 7
        $responseCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
464 7
        curl_close($curlHandle);
465 7
        if ($responseCode < 200 || $responseCode > 299)
466
        {
467 7
            if($responseBody && array_key_exists('error', $responseBody)){
468 2
                throw new \Exception($responseBody['error']['message'], $responseCode);
469
            }
470 5
            if($responseBody && array_key_exists('message', $responseBody)){
471 5
                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 2
    public function Post($url, $payload = array())
484
    {
485 2
        return $this->SendRequest(self::HTTP_POST, $url, $payload);
486
    }
487
488
    /**
489
     * @param string $url
490
     * @param array $payload
491
     * @return mixed
492
     */
493 2
    public function Get($url, $payload = array())
494
    {
495 2
        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