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 ( fb4bbc...dcdbd9 )
by Nico
29:09 queued 02:20
created

Api.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Datatrics package.
5
 *
6
 * (c) Datatrics
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Datatrics\Api;
13
14
/**
15
 * Api component
16
 *
17
 * @author Nico Borghuis <[email protected]>
18
 * @author Bas Nieland <[email protected]>
19
 * @author Pepijn Yben <[email protected]>
20
 */
21
class Api
22
{
23
    /**
24
     * The allowed HTTP Methods.
25
     *
26
     * @var array
27
     */
28
    private $allowedMethods = array('GET','POST');
29
30
    /**
31
     * The url of the API.
32
     *
33
     * @var string
34
     */
35
    private $url = 'https://api.datatrics.com';
36
37
    /**
38
     * The version of the API.
39
     *
40
     * @var float
41
     */
42
    private $version = 1.1;
43
44
    /**
45
     * The API key.
46
     *
47
     * @var string
48
     */
49
    private $apiKey;
50
51
    /**
52
     * The API will use this as the default project id.
53
     *
54
     * @var string
55
     */
56
    private $projectId;
57
58
    /**
59
     * Create a new API instance.
60
     *
61
     * @param string $apiKey
62
     * @param string $projectId
63
     */
64
    public function __construct($apiKey, $projectId = null)
65
    {
66
        $this->setApiKey($apiKey);
67
        $this->setProjectId($projectId);
68
    }
69
70
    /**
71
     * Get the endpoint.
72
     *
73
     * @param string $method
74
     * @param string $action
75
     * @param array  $query
76
     *
77
     * @return string
78
     *
79
     * @throws \Exception
80
     */
81
    private function getEndpoint($method, $action, $query = null)
82
    {
83
        if (is_null($query)) {
84
            return $this->url.'/'.$this->version.'/'.$method.'/'.$action;
85
        }
86
        if (is_array($query)) {
87
            return $this->url.'/'.$this->version.'/'.$method.'/'.$action.'?'.http_build_query($query);
88
        }
89
        throw new \Exception('The given query is not an array');
90
    }
91
92
    /**
93
     * Do the request to the API.
94
     *
95
     * @param string $url
96
     * @param array  $opts
97
     * @param string $httpMethod
98
     *
99
     * @return mixed
100
     *
101
     * @throws \Exception
102
     */
103
    private function request($url, $opts = array(), $httpMethod = 'GET')
104
    {
105
        if (!in_array($httpMethod, $this->allowedMethods)) {
106
            throw new \Exception('Method not allowed');
107
        }
108
109
        $opts[CURLOPT_URL] = $url;
110
        $opts[CURLOPT_TIMEOUT] = 100;
111
        $opts[CURLOPT_USERAGENT] = 'Datatrics/API '.$this->version;
112
        $opts[CURLOPT_SSL_VERIFYPEER] = true;
113
        $opts[CURLOPT_RETURNTRANSFER] = true;
114
        $ch = curl_init();
115
        curl_setopt_array($ch, $opts);
116
        $data = curl_exec($ch);
117
        if (curl_errno($ch)) {
118
            $message = curl_error($ch);
119
            $code = curl_errno($ch);
120
            curl_close($ch);
121
            throw new \Exception($message, $code);
122
        } else {
123
            $info = curl_getinfo($ch);
124
            curl_close($ch);
125
            if ($info['http_code'] >= 200 && $info['http_code'] < 300) {
126
                return json_decode($data, true);
127
            } else {
128
                throw new \Exception('HTTP Error', $info['http_code']);
129
            }
130
        }
131
132
        return;
0 ignored issues
show
return; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
133
    }
134
135
    /**
136
     * Do the get request.
137
     *
138
     * @param string $method
139
     * @param string $action
140
     * @param array  $query
141
     *
142
     * @return array
143
     *
144
     * @throws \Exception
145
     */
146
    public function GET($method, $action, $query = array())
147
    {
148 View Code Duplication
        if (!isset($query['projectid']) && !is_null($this->projectId)) {
0 ignored issues
show
This code seems to be duplicated across 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...
149
            $query['projectid'] = $this->getProjectId();
150
        }
151
152
        $query['apikey'] = $this->getApiKey();
153
        $query = array_reverse($query);
154
        $url = $this->getEndpoint($method, $action, $query);
155
        $opts = array();
156
157
        return $this->request($url, $opts, 'GET');
158
    }
159
160
    /**
161
     * Do the post request.
162
     *
163
     * @param string $method
164
     * @param string $action
165
     * @param array  $query
166
     *
167
     * @return array
168
     *
169
     * @throws \Exception
170
     */
171
    public function POST($method, $action, $query = array())
172
    {
173 View Code Duplication
        if (!isset($query['projectid']) && !is_null($this->projectId)) {
0 ignored issues
show
This code seems to be duplicated across 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...
174
            $query['projectid'] = $this->getProjectId();
175
        }
176
177
        $query['apikey'] = $this->getApiKey();
178
        $query = array_reverse($query);
179
        $url = $this->getEndpoint($method, $action);
180
        $opts = array(
181
            CURLOPT_POST => true,
182
            CURLOPT_POSTFIELDS => http_build_query($query),
183
        );
184
185
        return $this->request($url, $opts, 'POST');
186
    }
187
188
    /**
189
     * Get the apiKey.
190
     *
191
     * @return string
192
     *
193
     * @throws \Exception
194
     */
195
    public function getApiKey()
196
    {
197
        if (is_null($this->apiKey)) {
198
            throw new \Exception('Apikey required');
199
        }
200
201
        return $this->apiKey;
202
    }
203
204
    /**
205
     * Set the apiKey.
206
     *
207
     * @param string $apiKey
208
     */
209
    public function setApiKey($apiKey)
210
    {
211
        $this->apiKey = $apiKey;
212
    }
213
214
    /**
215
     * Get the default project id.
216
     *
217
     * @return string
218
     */
219
    public function getProjectId()
220
    {
221
        return $this->projectId;
222
    }
223
224
    /**
225
     * Set the default project id.
226
     *
227
     * @param string $projectId
228
     */
229
    public function setProjectId($projectId)
230
    {
231
        $this->projectId = $projectId;
232
    }
233
234
    /**
235
     * Get the api url
236
     *
237
     * @return string
238
     */
239
    public function getUrl()
240
    {
241
        return $this->url;
242
    }
243
244
    /**
245
     * Set the api url
246
     *
247
     * @param string $url
248
     */
249
    public function setUrl($url)
250
    {
251
        $this->url = $url;
252
    }
253
}
254