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 — master ( ba89cf...799599 )
by Oleg
02:27
created

Route4Me::fileUploadRequest()   B

Complexity

Conditions 9
Paths 58

Size

Total Lines 57

Duplication

Lines 7
Ratio 12.28 %

Importance

Changes 0
Metric Value
dl 7
loc 57
rs 7.3826
c 0
b 0
f 0
cc 9
nc 58
nop 1

How to fix   Long Method   

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
namespace Route4Me;
3
4
use Route4Me\Exception\ApiError;
5
use Route4Me\Exception\myErrorHandler;
6
use Route4Me\Enum\Endpoint;
7
8
class Route4Me
9
{
10
    static public $apiKey;
11
    static public $baseUrl = Endpoint::BASE_URL;
12
13
    public static function setApiKey($apiKey)
14
    {
15
        self::$apiKey = $apiKey;
16
    }
17
18
    public static function getApiKey()
19
    {
20
        return self::$apiKey;
21
    }
22
23
    public static function setBaseUrl($baseUrl)
24
    {
25
        self::$baseUrl = $baseUrl;
26
    }
27
28
    public static function getBaseUrl()
29
    {
30
        return self::$baseUrl;
31
    }
32
    
33
    public static function fileUploadRequest($options) {
34
        $query = isset($options['query']) ? array_filter($options['query']) : array();
35
36
        if (sizeof($query)==0) {
37
            return null;
38
            
39
        }
40
        
41
        $body = isset($options['body']) ? array_filter($options['body']) : null;
42
            
43
        $fname = isset($body['strFilename']) ? $body['strFilename'] : '';
44
        
45
        if ($fname=='') {
46
            return null;  
47
        } 
48
49
        $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fname)) : '@'.realpath($fname);
50
        
51
        $url = self::$baseUrl.$options['url'].'?'.http_build_query(array_merge(array('api_key' => self::getApiKey()), $query));
52
        
53
        $ch = curl_init($url);
54
        
55
        $curlOpts = array(
56
            CURLOPT_POST => true,
57
            CURLOPT_RETURNTRANSFER => false,
58
            CURLOPT_TIMEOUT        => 60,
59
            CURLOPT_FOLLOWLOCATION => true,
60
            CURLOPT_SSL_VERIFYHOST => FALSE,
61
            CURLOPT_SSL_VERIFYPEER => FALSE
62
        );
63
        
64
        curl_setopt_array($ch, $curlOpts);
65
        
66
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
67
        
68
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
69
            "Content-Type: multipart/form-data",
70
            'Content-Disposition: form-data; name="strFilename"'
71
        ));
72
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
73
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('strFilename' => $rpath)); 
74
        
75
        $result = curl_exec($ch);
76
77
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
78
        curl_close($ch);
79
80
        $json = json_decode($result, true);
81
        
82 View Code Duplication
        if (200==$code) {
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...
83
            return $json;
84
        } elseif (isset($json['errors'])) {
85
            throw new ApiError(implode(', ', $json['errors']));
86
        } else {
87
            throw new ApiError('Something wrong');
88
        }
89
    }
90
91
    public static function makeRequst($options) {
92
        $errorHandler = new myErrorHandler();
93
        
94
        $old_error_handler = set_error_handler(array($errorHandler, "proc_error"));
0 ignored issues
show
Unused Code introduced by
$old_error_handler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
        
96
        $method = isset($options['method']) ? $options['method'] : 'GET';
97
        $query = isset($options['query']) ? array_filter($options['query'], function($x) { return !is_null($x); } ) : array();
98
99
        $body = isset($options['body']) ? $options['body'] : null;
100
        $file = isset($options['FILE']) ? $options['FILE'] : null;
101
        $headers = array(
102
            "User-Agent: Route4Me php-sdk"
103
        );
104
        
105
        if (isset($options['HTTPHEADER'])) {
106
            $headers[] = $options['HTTPHEADER'];
107
        }
108
         
109
        if (isset($options['HTTPHEADERS'])) {
110
            foreach ($options['HTTPHEADERS'] As $header) {
111
                $headers[] = $header;
112
            } 
113
        }
114
115
        $ch = curl_init();
116
        
117
        $url = $options['url'].'?'.http_build_query(array_merge(
118
            $query, array('api_key' => self::getApiKey())
119
        ));
120
121
        $baseUrl = self::getBaseUrl();
122
123
        $curlOpts = arraY(
124
            CURLOPT_URL            => $baseUrl.$url,
125
            CURLOPT_RETURNTRANSFER => true,
126
            CURLOPT_TIMEOUT        => 80,
127
            CURLOPT_FOLLOWLOCATION => true,
128
            CURLOPT_SSL_VERIFYHOST => FALSE,
129
            CURLOPT_SSL_VERIFYPEER => FALSE,
130
            CURLOPT_HTTPHEADER     => $headers
131
        );
132
        
133
        curl_setopt_array($ch, $curlOpts);
134
        
135
        if ($file!=null) {
136
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
137
            $fp=fopen($file, 'r');
138
            curl_setopt($ch, CURLOPT_INFILE, $fp);
139
            curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
140
        }
141
142
        switch ($method) {
143
        case 'DELETE':
144
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
145
            break;
146 View Code Duplication
        case 'DELETEARRAY':
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...
147
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
148
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
149
            break;
150
        case 'PUT':
151
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
152
            break;
153
        case 'POST':
154
           if (isset($body)) {
155
                $bodyData = json_encode($body);
156
               if (isset($options['HTTPHEADER'])) {
157
                  if (strpos($options['HTTPHEADER'], "multipart/form-data")>0) {
158
                      $bodyData = $body;
159
                  }
160
               }
161
               
162
               curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); 
163
            } 
164
            break;
165
        case 'ADD':
166
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break;
167
        }
168
169 View Code Duplication
        if (is_numeric(array_search($method, array('DELETE', '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...
170
            if (isset($body)) {
171
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
172
            } 
173
        }
174
175
        $result = curl_exec($ch);
176
177
        $isxml = FALSE;
178
        $jxml = "";
179 View Code Duplication
        if (strpos($result, '<?xml')>-1) {
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...
180
            $xml = simplexml_load_string($result);
181
            //$jxml = json_encode($xml);
182
            $jxml = self::object2array($xml);
183
            $isxml = TRUE;
184
        }
185
        
186
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
187
        curl_close($ch);
188
        
189
        if (200==$code) {
190
            if ($isxml) {
191
                $json = $jxml;
192
            } else {
193
                $json = json_decode($result, true);
194
            }
195
            
196
            if (isset($json['errors'])) {
197
                throw new ApiError(implode(', ', $json['errors']));
198
            } else {
199
                return $json;
200
            }
201
        }  elseif (409==$code) {
202
            throw new ApiError('Wrong API key');
203
        } else {
204
            throw new ApiError('Something wrong');
205
        }
206
    }
207
208
    /**
209
     * @param $object: JSON object
210
     */
211
    public static function object2array($object)
212
    {
213
        return @json_decode(@json_encode($object), 1);
214
    }
215
216
    public static function makeUrlRequst($url, $options) {
217
        $method = isset($options['method']) ? $options['method'] : 'GET';
218
        $query = isset($options['query']) ?
219
            array_filter($options['query'], function($x) { return !is_null($x); } ) : array();
220
        $body = isset($options['body']) ? $options['body'] : null;
221
        $ch = curl_init();
222
        
223
        $curlOpts = arraY(
224
            CURLOPT_URL            => $url,
225
            CURLOPT_RETURNTRANSFER => true,
226
            CURLOPT_TIMEOUT        => 60,
227
            CURLOPT_FOLLOWLOCATION => true,
228
            CURLOPT_SSL_VERIFYHOST => FALSE,
229
            CURLOPT_SSL_VERIFYPEER => FALSE,
230
            CURLOPT_HTTPHEADER     => array(
231
                'User-Agent' => 'Route4Me php-sdk'
232
            )
233
        );
234
        
235
        curl_setopt_array($ch, $curlOpts);
236
        
237
        switch ($method) {
238
        case 'DELETE':
239
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
240
            break;
241 View Code Duplication
        case 'DELETEARRAY':
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...
242
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
243
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
244
            break;
245
        case 'PUT':
246
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
247
            break;
248
        case 'POST':
249
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
250
            break;
251
        case 'ADD':
252
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break;
253
        }
254
        
255 View Code Duplication
        if (is_numeric(array_search($method, array('DELETE', 'PUT', 'POST')))) {
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...
256
            if (isset($body)) {
257
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
258
            } 
259
        }
260
261
        $result = curl_exec($ch);
262
        
263
        $isxml = FALSE;
264
        $jxml = "";
265
        
266 View Code Duplication
        if (strpos($result, '<?xml')>-1) {
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...
267
            $xml = simplexml_load_string($result);
268
            $jxml = json_encode($xml);
269
            $isxml = TRUE;
270
        }
271
        
272
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
273
        curl_close($ch);
274
        
275
        if ($isxml) {
276
            $json = $jxml;
277
        } else {
278
            $json = json_decode($result, true);
279
        }
280
        
281 View Code Duplication
        if (200==$code) {
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...
282
            return $json;
283
        } elseif (isset($json['errors'])) {
284
            throw new ApiError(implode(', ', $json['errors']));
285
        } else {
286
            throw new ApiError('Something wrong');
287
        }
288
    }
289
    
290
    /**
291
     * Prints on the screen main keys and values of the array 
292
     * @param $results: object to be printed on the screen.
293
     * @param $deepPrinting: if true, object will be printed recursively.
294
     */
295
    public static function simplePrint($results, $deepPrinting = null)
296
    {
297
        if (isset($results)) {
298
            if (is_array($results)) {
299
                foreach ($results as $key=>$result) {
300
                    if (is_array($result)) {
301
                        foreach ($result as $key1=>$result1) {
302
                            if (is_array($result1)) {
303
                                  if ($deepPrinting) {
304
                                      echo "<br>$key1 ------><br>";
305
                                      Route4Me::simplePrint($result1, true);
306
                                      echo "------<br>";
307
                                  } else {
308
                                      echo $key1." --> "."Array() <br>";
309
                                  } 
310 View Code Duplication
                            } else {
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...
311
                                if (is_object($result1)) {
312
                                    if ($deepPrinting) {
313
                                        echo "<br>$key1 ------><br>";
314
                                        $oarray = (array)$result1;
315
                                        Route4Me::simplePrint($oarray, true);
316
                                        echo "------<br>";
317
                                    } else {
318
                                        echo $key1." --> "."Object <br>";
319
                                    } 
320
                                } else {
321
                                    if (!is_null($result1)) {
322
                                        echo $key1." --> ".$result1."<br>"; 
323
                                    }   
324
                                }
325
                            }
326
                        }
327 View Code Duplication
                    } else {
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...
328
                        if (is_object($result)) {
329
                            if ($deepPrinting) {
330
                                echo "<br>$key ------><br>";
331
                                $oarray = (array)$result;
332
                                Route4Me::simplePrint($oarray, true);
333
                                echo "------<br>";
334
                            } else {
335
                                echo $key." --> "."Object <br>";
336
                            } 
337
                        } else {
338
                            if (!is_null($result)) {
339
                                echo $key." --> ".$result."<br>";
340
                            }
341
                        }
342
                        
343
                    }
344
                    //echo "<br>";
345
                }
346
            } 
347
        }
348
    }
349
}
350