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 — master ( 2da744...697c5a )
by James
02:22
created

Api::request()   D

Complexity

Conditions 13
Paths 121

Size

Total Lines 79
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 5 Features 2
Metric Value
c 11
b 5
f 2
dl 0
loc 79
rs 4.761
cc 13
eloc 54
nc 121
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Cloudflare;
4
5
use Cloudflare\Exception\AuthenticationException;
6
use Cloudflare\Exception\UnauthorizedException;
7
8
/**
9
 * CloudFlare API wrapper
10
 *
11
 * A work in progress library for the Cloudflare API. The documentation for the API can be found at https://www.cloudflare.com/docs/.
12
 *
13
 * @author James Bell <[email protected]>
14
 *
15
 * @version 1
16
 */
17
class Api
18
{
19
    /**
20
     * Holds the provided email address for API authentication
21
     *
22
     * @var string
23
     */
24
    public $email;
25
26
    /**
27
     * Holds the provided auth_key for API authentication
28
     *
29
     * @var string
30
     */
31
    public $auth_key;
32
33
    /**
34
     * Holds the curl options
35
     *
36
     * @var array
37
     */
38
    public $curl_options;
39
40
    /**
41
     * Make a new instance of the API client
42
     * This can be done via providing the email address and api key as seperate parameters
43
     * or by passing in an already instantiated object from which the details will be extracted
44
     */
45
    public function __construct()
46
    {
47
        $num_args = func_num_args();
48
        if ($num_args === 1) {
49
            $parameters = func_get_args();
50
            $client = $parameters[0];
51
            $this->email = $client->email;
52
            $this->auth_key = $client->auth_key;
53
            $this->curl_options = $client->curl_options;
54
        } elseif ($num_args === 2) {
55
            $parameters = func_get_args();
56
            $this->email = $parameters[0];
57
            $this->auth_key = $parameters[1];
58
        }
59
    }
60
61
    /**
62
     * Setter to allow the setting of the email address
63
     *
64
     * @param string $email The email address associated with the Cloudflare account
65
     */
66
    public function setEmail($email)
67
    {
68
        $this->email = $email;
69
    }
70
71
    /**
72
     * Setter to allow the setting of the Authentication Key
73
     *
74
     * @param string $token Authentication key, this can be retrieve from the 'My Account' section of the Cloudflare account
75
     */
76
    public function setAuthKey($token)
77
    {
78
        $this->auth_key = $token;
79
    }
80
81
    /**
82
     * Setter to allow the adding / changing of the Curl options that will be used within the HTTP requests
83
     *
84
     * @param int   $key   The CURLOPT_XXX option to set e.g. CURLOPT_TIMEOUT
85
     * @param mixed $value The value to be set on option e.g. 10
86
     */
87
    public function setCurlOption($key, $value)
88
    {
89
        $this->curl_options[$key] = $value;
90
    }
91
92
    /**
93
     * API call method for sending requests using GET
94
     *
95
     * @param string     $path Path of the endpoint
96
     * @param array|null $data Data to be sent along with the request
97
     *
98
     * @return mixed
99
     */
100
    public function get($path, array $data = null)
101
    {
102
        return $this->request($path, $data, 'get');
103
    }
104
105
    /**
106
     * API call method for sending requests using POST
107
     *
108
     * @param string     $path Path of the endpoint
109
     * @param array|null $data Data to be sent along with the request
110
     *
111
     * @return mixed
112
     */
113
    public function post($path, array $data = null)
114
    {
115
        return $this->request($path, $data, 'post');
116
    }
117
118
    /**
119
     * API call method for sending requests using PUT
120
     *
121
     * @param string     $path Path of the endpoint
122
     * @param array|null $data Data to be sent along with the request
123
     *
124
     * @return mixed
125
     */
126
    public function put($path, array $data = null)
127
    {
128
        return $this->request($path, $data, 'put');
129
    }
130
131
    /**
132
     * API call method for sending requests using DELETE
133
     *
134
     * @param string     $path Path of the endpoint
135
     * @param array|null $data Data to be sent along with the request
136
     *
137
     * @return mixed
138
     */
139
    public function delete($path, array $data = null)
140
    {
141
        return $this->request($path, $data, 'delete');
142
    }
143
144
    /**
145
     * API call method for sending requests using PATCH
146
     *
147
     * @param string     $path Path of the endpoint
148
     * @param array|null $data Data to be sent along with the request
149
     *
150
     * @return mixed
151
     */
152
    public function patch($path, array $data = null)
153
    {
154
        return $this->request($path, $data, 'patch');
155
    }
156
157
    /**
158
     * @codeCoverageIgnore
159
     *
160
     * API call method for sending requests using GET, POST, PUT, DELETE OR PATCH
161
     *
162
     * @param string      $path   Path of the endpoint
163
     * @param array|null  $data   Data to be sent along with the request
164
     * @param string|null $method Type of method that should be used ('GET', 'POST', 'PUT', 'DELETE', 'PATCH')
165
     *
166
     * @return mixed
167
     */
168
    protected function request($path, array $data = null, $method = null)
169
    {
170
        if (!isset($this->email, $this->auth_key) || false === filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
171
            throw new AuthenticationException('Authentication information must be provided');
172
        }
173
174
        $data = (is_null($data) ? [] : $data);
175
        $method = (is_null($method) ? 'get' : $method);
176
177
        //Removes null entries
178
        $data = array_filter($data, function ($val) {
179
            return !is_null($val);
180
        });
181
182
        $url = 'https://api.cloudflare.com/client/v4/'.$path;
183
184
        $default_curl_options = [
185
            CURLOPT_VERBOSE        => false,
186
            CURLOPT_FORBID_REUSE   => true,
187
            CURLOPT_RETURNTRANSFER => 1,
188
            CURLOPT_HEADER         => false,
189
            CURLOPT_TIMEOUT        => 30,
190
            CURLOPT_SSL_VERIFYPEER => false,
191
            CURLOPT_FOLLOWLOCATION => true,
192
        ];
193
194
        $curl_options = $default_curl_options;
195
        if (isset($this->curl_options) && is_array($this->curl_options)) {
196
            $curl_options = array_replace($default_curl_options, $this->curl_options);
197
        }
198
199
        $headers = ["X-Auth-Email: {$this->email}", "X-Auth-Key: {$this->auth_key}"];
200
201
        $ch = curl_init();
202
        curl_setopt_array($ch, $curl_options);
203
204
        $headers[] = 'Content-type: application/json';
205
        $json_data = json_encode($data);
206
207
        if ($method === 'post') {
208
            curl_setopt($ch, CURLOPT_POST, true);
209
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
210
        } elseif ($method === 'put') {
0 ignored issues
show
Duplication introduced by
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...
211
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
212
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
213
        } elseif ($method === 'delete') {
214
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
215
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
216
        } elseif ($method === 'patch') {
0 ignored issues
show
Duplication introduced by
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...
217
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
218
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
219
        } else {
220
            $url .= '?'.http_build_query($data);
221
        }
222
223
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
224
        curl_setopt($ch, CURLOPT_URL, $url);
225
226
        $http_result = curl_exec($ch);
227
        $error = curl_error($ch);
228
        $information = curl_getinfo($ch);
229
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
230
231
        if (in_array($http_code, [401, 403])) {
232
            throw new UnauthorizedException('You do not have permission to perform this request');
233
        }
234
235
        $response = json_decode($http_result);
236
237
        curl_close($ch);
238
        if ($response->success !== true) {
239
            $response->error = $error;
240
            $response->http_code = $http_code;
241
            $response->method = $method;
242
            $response->information = $information;
243
        }
244
245
        return $response;
246
    }
247
}
248