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
Branch Editing-Fixing (c4d168)
by Igor
03:30
created

Route::getRoutes()   B

Complexity

Conditions 10
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 3
nop 2
dl 0
loc 27
rs 7.6666
c 0
b 0
f 0

How to fix   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
namespace Route4Me;
3
4
use Route4Me\Common;
5
use Route4Me\Address;
6
use Route4Me\Exception\BadParam;
7
use Route4Me\RouteParameters;
8
use Route4Me\Route4Me;
9
use GuzzleHttp\Client;
10
use Route4Me\Enum\Endpoint;
11
12
class Route extends Common
13
{
14
    public $route_id;
15
    public $member_id;
16
    public $route_destination_id;
17
    public $optimization_problem_id;
18
    public $vehicle_alias;
19
    public $driver_alias;
20
    public $trip_distance;
21
    public $mpg;
22
    public $gas_price;
23
    public $route_duration_sec;
24
    public $destination_count;
25
    public $parameters;
26
    public $addresses = array();
27
    public $links = array();
28
    public $directions = array();
29
    public $path = array();
30
    public $tracking_history = array();
31
    public $recipient_email;
32
    public $httpheaders;
33
    public $is_unrouted;
34
    public $time;
35
    
36
    public $dev_lat;
37
    public $dev_lng;
38
    
39
    public $user_route_rating;
40
    public $member_email;
41
    public $member_first_name;
42
    public $member_last_name;
43
    public $channel_name;
44
    public $route_cost;
45
    public $route_revenue;
46
    public $net_revenue_per_distance_unit;
47
    public $created_timestamp;
48
    public $planned_total_route_duration;
49
    public $actual_travel_distance;
50
    public $actual_travel_time;
51
    public $actual_footsteps;
52
    public $working_time;
53
    public $driving_time;
54
    public $idling_time;
55
    public $paying_miles;
56
    public $geofence_polygon_type;
57
    public $geofence_polygon_size;
58
    public $notes;
59
    public $member_config_storage;
60
61
    public static function fromArray(array $params) 
62
    {
63
        $route = new Route();
64
        $route->route_id                = Common::getValue($params, 'route_id');
65
        $route->member_id               = Common::getValue($params, 'member_id');
66
        $route->optimization_problem_id = Common::getValue($params, 'optimization_problem_id');
67
        $route->vehicle_alias           = Common::getValue($params, 'vehicle_alias');
68
        $route->driver_alias            = Common::getValue($params, 'driver_alias');
69
        $route->trip_distance           = Common::getValue($params, 'trip_distance');
70
        $route->mpg                     = Common::getValue($params, 'mpg');
71
        $route->gas_price               = Common::getValue($params, 'gas_price');
72
        $route->route_duration_sec      = Common::getvalue($params, 'route_duration_sec');
73
        $route->destination_count       = Common::getvalue($params, 'destination_count');
74
        $route->is_unrouted             = Common::getvalue($params, 'is_unrouted');
75
76
        // Make RouteParameters
77
        if (isset($params['parameters'])) {
78
            $route->parameters = RouteParameters::fromArray($params['parameters']);
79
        }
80
81 View Code Duplication
        if (isset($params['addresses'])) {
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...
82
            $addresses = array();
83
            foreach ($params['addresses'] as $address) {
84
                $addresses[] = Address::fromArray($address);
85
            }
86
87
            $route->addresses = $addresses;
88
        }
89
90
        $route->links            = Common::getValue($params, 'links', array());
91
        $route->directions       = Common::getValue($params, 'directions', array());
92
        $route->path             = Common::getValue($params, 'path', array());
93
        $route->tracking_history = Common::getValue($params, 'tracking_history', array());
94
95
        return $route;
96
    }
97
98
    public static function getRoutes($routeId=null, $params=null)
99
    {
100
        $result = Route4Me::makeRequst(array(
101
            'url'    => Endpoint::ROUTE_V4,
102
            'method' => 'GET',
103
            'query'  => array(
104
                'api_key'                  => Route4Me::getApiKey(),
105
                'route_id'                 => !is_null($routeId) ? implode(',', (array) $routeId) : null,
106
                'route_path_output'        => isset($params['route_path_output']) ? $params['route_path_output'] : null,
107
                'query'                    => isset($params['query']) ? $params['query'] : null,
108
                'directions'               => isset($params['directions']) ? $params['directions'] : null,
109
                'device_tracking_history'  => isset($params['device_tracking_history']) ? $params['device_tracking_history'] : null,
110
                'limit'                    => isset($params['limit']) ? $params['limit'] : null,
111
                'offset'                   => isset($params['offset']) ? $params['offset'] : null
112
            )
113
        ));
114
        
115
        if ($routeId) {
116
            return Route::fromArray($result); die("");
0 ignored issues
show
Unused Code introduced by
die(''); 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...
117
        } else {
118
            $routes = array();
119
            foreach($result as $route) {
120
                $routes[] = Route::fromArray($route);
121
            }
122
            return $routes;
123
        }
124
    }
125
126 View Code Duplication
    public function getRoutePoints($routeId, $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
127
    {
128
        $result = Route4Me::makeRequst(array(
129
            'url'    => Endpoint::ROUTE_V4,
130
            'method' => 'GET',
131
            'query'  => array(
132
                'api_key'           => Route4Me::getApiKey(),
133
                'route_id'          => $routeId,
134
                'route_path_output' => isset($params['route_path_output']) ? $params['route_path_output'] : null,
135
                'compress_path_points' => isset($params['compress_path_points']) ? $params['compress_path_points'] : null,
136
                'directions'        => isset($params['directions']) ? $params['directions'] : null,
137
            )
138
        ));
139
140
        return $result;
141
    }
142
143 View Code Duplication
    public function duplicateRoute($route_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
144
    {
145
        $result = Route4Me::makeRequst(array(
146
            'url'    => Endpoint::ROUTE_DUPLICATE,
147
            'method' => 'GET',
148
            'query'  => array(
149
                'api_key'  => Route4Me::getApiKey(),
150
                'route_id' => $route_id,
151
                'to'       => 'none',
152
            )
153
        ));
154
        
155
        return $result;
156
    }
157
    
158 View Code Duplication
    public function resequenceRoute($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
159
    {
160
        $result = Route4Me::makeRequst(array(
161
            'url'    => Endpoint::ROUTE_V4,
162
            'method' => 'PUT',
163
            'query'  => array(
164
                'api_key'              => Route4Me::getApiKey(),
165
                'route_id'             => isset($params['route_id']) ? $params['route_id'] : null,
166
                'route_destination_id' => isset($params['route_destination_id']) ? $params['route_destination_id'] : null,
167
            ),
168
            'body'   => array(
169
                'addresses' => isset($params['addresses']) ? $params['addresses'] : null,
170
            )
171
        ));
172
        
173
        return $result;
174
    }
175
    
176 View Code Duplication
    public function resequenceAllAddresses($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
177
    {
178
        $result = Route4Me::makeRequst(array(
179
            'url'    => Endpoint::REOPTIMIZE_V3_2,
180
            'method' => 'GET',
181
            'query'  => array(
182
                'api_key'              => Route4Me::getApiKey(),
183
                'route_id'             => isset($params['route_id']) ? $params['route_id'] : null,
184
                'disable_optimization' => isset($params['disable_optimization']) ? $params['disable_optimization'] : null,
185
                'optimize'             => isset($params['optimize']) ? $params['optimize'] : null,
186
            )
187
        ));
188
        
189
        return $result;
190
    }
191
192
    public function mergeRoutes($params)
193
    {
194
        $result = Route4Me::makeRequst(array(
195
            'url'    => Endpoint::ROUTES_MERGE,
196
            'method' => 'POST',
197
            'query'  => array(
198
                'api_key' => Route4Me::getApiKey(),
199
              ),
200
            'body'   => array(
201
                'route_ids'     => isset($params['route_ids']) ? $params['route_ids'] : null,
202
                'depot_address' => isset($params['depot_address']) ? $params['depot_address'] : null,
203
                'remove_origin' => isset($params['remove_origin']) ? $params['remove_origin'] : null,
204
                'depot_lat'     => isset($params['depot_lat']) ? $params['depot_lat'] : null,
205
                'depot_lat'     => isset($params['depot_lat']) ? $params['depot_lat'] : null
206
              ),
207
            'HTTPHEADER'  => 'Content-Type: multipart/form-data'
208
        ));
209
        
210
        return $result;
211
    }
212
    
213 View Code Duplication
    public function shareRoute($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
214
    {
215
        $result = Route4Me::makeRequst(array(
216
            'url'    => Endpoint::ROUTE_SHARE,
217
            'method' => 'POST',
218
            'query'  => array(
219
                'api_key'         => Route4Me::getApiKey(),
220
                'route_id'        => isset($params['route_id']) ? $params['route_id'] : null,
221
                'response_format' => isset($params['response_format']) ? $params['response_format'] : null,
222
            ),
223
            'body'  => array(
224
                'recipient_email' => isset($params['recipient_email']) ? $params['recipient_email'] : null,
225
            ),
226
            'HTTPHEADER'  => 'Content-Type: multipart/form-data'
227
        ));
228
        
229
        return $result;
230
    }
231
    
232
    // Returns random route_id from existing routes between $offset and $offset+$limit
233
    public function getRandomRouteId($offset,$limit)
0 ignored issues
show
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
234
    {
235
        $query['limit'] = isset($params['limit']) ? $params['limit'] : 30;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Bug introduced by
The variable $params seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
236
        $query['offset'] = isset($params['offset']) ? $params['offset'] : 0;
237
            
238
        $json = Route4Me::makeRequst(array(
239
            'url'    => Endpoint::ROUTE_V4,
240
            'method' => 'GET',
241
            'query'  => $query
242
        ));
243
        
244
        if (sizeof($json)>0) {
245
            $routes = array();
246
            
247
            foreach($json as $route) {
248
                $routes[] = Route::fromArray($route);
249
            }
250
            
251
            $num=rand(0,sizeof($routes)-1);
252
            $rRoute=(array)$routes[$num];
253
            
254
            if (is_array($rRoute)) {
255
                return $rRoute["route_id"];
256
            } else {
257
                return null;
258
            }
259
        } else {
260
            echo "<br> There are no routes in the account. Please, create the routes first. <br>";
261
            return null;
262
        }
263
    }
264
265
    public function update()
266
    {
267
        $route = Route4Me::makeRequst(array(
268
            'url'    => Endpoint::ROUTE_V4,
269
            'method' => 'PUT',
270
            'query'  => array(
271
                'route_id'  => isset($this->route_id) ? $this->route_id : null
272
            ),
273
            'body' => array (
274
                'parameters' => $this->parameters,
275
                ),
276
            'HTTPHEADER'  => isset($this->httpheaders) ? $this->httpheaders : null,
277
        ));
278
279
        return Route::fromArray($route);
280
    }
281
    
282
    public function updateAddress($address=null)
0 ignored issues
show
Unused Code introduced by
The parameter $address is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
283
    {
284
        $body = sizeof($this->addresses)<1 ? get_object_vars($this->parameters) 
285
            : (isset($this->addresses[0]) ? $this->addresses[0] : get_object_vars($this->parameters));
286
287
        $result = Route4Me::makeRequst(array(
288
            'url'    => Endpoint::ADDRESS_V4,
289
            'method' => 'PUT',
290
            'query'  => array(
291
                'route_id'             => isset($this->route_id) ? $this->route_id : null,
292
                'route_destination_id' => isset($this->route_destination_id) ? $this->route_destination_id : null,
293
            ),
294
            'body'        => $body,
295
            'HTTPHEADER'  => isset($this->httpheaders) ? $this->httpheaders : null,
296
        ));
297
298
        return $result;
299
    }
300
301
    public function updateRouteAddress()
302
    {
303
        $result = Route4Me::makeRequst(array(
304
            'url'    => Endpoint::ADDRESS_V4,
305
            'method' => 'PUT',
306
            'query'  => array(
307
                'route_id'             => isset($this->route_id) ? $this->route_id : null,
308
                'route_destination_id' => isset($this->route_destination_id) ? $this->route_destination_id : null,
309
            ),
310
            'body'        => array(
311
                "parameters" => isset($this->parameters) ? get_object_vars($this->parameters) : null,
312
                "addresses"  => isset($this->addresses) ? $this->addresses : null
313
            ),
314
            'HTTPHEADER'  => isset($this->httpheaders) ? $this->httpheaders : null,
315
        ));
316
317
        return $result;
318
    }
319
320
    public function addAddresses($params)
321
    {
322
        $route = Route4Me::makeRequst(array(
323
            'url'    => Endpoint::ROUTE_V4,
324
            'method' => 'PUT',
325
            'query'  => array(
326
                'api_key'   => Route4Me::getApiKey(),
327
                'route_id'  => isset($params['route_id']) ? $params['route_id'] : null
328
            ),
329
            'body'   => array(
330
                'addresses' => isset($params['addresses']) ? $params['addresses'] : null
331
            ),
332
            'HTTPHEADER'  => isset($this->httpheaders) ? $this->httpheaders : null,
333
        ));
334
335
        return Route::fromArray($route);
336
    }
337
    
338 View Code Duplication
    public function insertAddressOptimalPosition(array $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
339
    {
340
        $route = Route4Me::makeRequst(array(
341
            'url'    => Endpoint::ROUTE_V4,
342
            'method' => 'PUT',
343
            'query'  => array(
344
                'api_key'  => Route4Me::getApiKey(),
345
                'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
346
            ),
347
            'body'   => array(
348
                'addresses'        => isset($params['addresses']) ? $params['addresses'] : null,
349
                'optimal_position' => isset($params['optimal_position']) ? $params['optimal_position'] : null,
350
            )
351
        ));
352
353
        return Route::fromArray($route);
354
    }
355
    
356
    public function addNoteFile($params)
357
    {
358
        $fname = isset($params['strFilename']) ? $params['strFilename'] : null;
359
        $rpath = realpath($fname);
360
        
361
        $result= Route4Me::makeRequst(array(
362
            'url'    => Endpoint::ROUTE_NOTES_ADD,
363
            'method' => 'POST',
364
            'query'  => array(
365
                'api_key'     => Route4Me::getApiKey(),
366
                'route_id'    => isset($params['route_id']) ? $params['route_id'] : null,
367
                'address_id'  => isset($params['address_id']) ? $params['address_id'] : null,
368
                'dev_lat'     => isset($params['dev_lat']) ? $params['dev_lat'] : null,
369
                'dev_lng'     => isset($params['dev_lng']) ? $params['dev_lng'] : null,
370
                'device_type' => isset($params['device_type']) ? $params['device_type'] : null,
371
                'dev_lng'     => isset($params['dev_lng']) ? $params['dev_lng'] : null,
372
            ),
373
            'body'  => array(
374
                'strUpdateType'   => isset($params['strUpdateType']) ? $params['strUpdateType'] : null,
375
                'strFilename'     => isset($params['strFilename']) ? $params['strFilename'] : null,
376
                'strNoteContents' => isset($params['strNoteContents']) ? $params['strNoteContents'] : null,
377
            ),
378
            'FILE'  => $rpath,
379
380
            'HTTPHEADER' => array(
381
                'Content-Type: application/x-www-form-urlencoded'
382
            )
383
        ));
384
385
        return $result;
386
    }
387
388
    public function delete($route_id)
389
    {
390
        $result = Route4Me::makeRequst(array(
0 ignored issues
show
Unused Code introduced by
$result 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...
391
            'url'    => Endpoint::ROUTE_V4,
392
            'method' => 'DELETE',
393
            'query'  => array( 'route_id' => $route_id )
394
        ));
395
        
396
         $result = Route4Me::makeRequst(array(
397
            'url'    => Endpoint::ROUTES_DELETE,
398
            'method' => 'DELETE',
399
            'query'  => array(
400
                'api_key' => Route4Me::getApiKey(),
401
                'route_id' => $route_id,
402
            )
403
        ));
404
        
405
        return $result;
406
    }
407
    
408
    public function GetAddressesFromRoute($route_id)
409
    {
410
        $route1=Route::getRoutes($route_id,null);
411
        if (isset($route1)) {
412
            return $route1->addresses;
413
        } else {
414
            return null;
415
        }
416
    }
417
    
418
    public function GetRandomAddressFromRoute($route_id)
419
    {
420
        $route1 = Route::getRoutes($route_id,null);
421
        
422
        if (isset($route1)) {
423
            $addresses = $route1->addresses;
424
            
425
            $rnd = rand(0,sizeof($addresses)-1);
426
            
427
            return $addresses[$rnd];
428
            
429
        } else {
430
            return null;
431
        }
432
    }
433
434
    public function getRouteId()
435
    {
436
        return $this->route_id;
437
    }
438
439
    public function getOptimizationId()
440
    {
441
        return $this->optimization_problem_id;
442
    }
443
    
444 View Code Duplication
    public function GetLastLocation(array $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
445
    {
446
        $route = Route4Me::makeRequst(array(
447
            'url'    => Endpoint::ROUTE_V4,
448
            'method' => 'GET',
449
            'query'  => array(
450
                'api_key'                 => Route4Me::getApiKey(),
451
                'route_id'                => isset($params['route_id']) ? $params['route_id'] : null,
452
                'device_tracking_history' => isset($params['device_tracking_history']) ? $params['device_tracking_history'] : null
453
            )
454
        ));
455
456
        return Route::fromArray($route);
457
    }
458
    
459 View Code Duplication
    public function GetTrackingHistoryFromTimeRange(array $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
460
    {
461
        $route = Route4Me::makeRequst(array(
462
            'url'    => Endpoint::GET_DEVICE_LOCATION,
463
            'method' => 'GET',
464
            'query'  => array(
465
                'api_key'     => Route4Me::getApiKey(),
466
                'route_id'    => isset($params['route_id']) ? $params['route_id'] : null,
467
                'format'      => isset($params['format']) ? $params['format'] : null,
468
                'time_period' => isset($params['time_period']) ? $params['time_period'] : null,
469
                'start_date'  => isset($params['start_date']) ? $params['start_date'] : null,
470
                'end_date'    => isset($params['end_date']) ? $params['end_date'] : null
471
                )
472
        ));
473
474
        return $route;
475
    }
476
    
477 View Code Duplication
    public function GetAssetTracking(array $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
478
    {
479
        $route = Route4Me::makeRequst(array(
480
            'url'    => Endpoint::STATUS_V4,
481
            'method' => 'GET',
482
            'query'  => array(
483
                'api_key'  => Route4Me::getApiKey(),
484
                'tracking' => isset($params['tracking']) ? $params['tracking'] : null
485
                )
486
        ));
487
488
        return $route;
489
    }
490
}
491