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 ( 8c2b4f...09d09e )
by Oleg
02:19
created

AddressBookLocation::validateScheduleWeekDays()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23

Duplication

Lines 18
Ratio 78.26 %

Importance

Changes 0
Metric Value
dl 18
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
namespace Route4Me;
3
4
use Route4Me\Common;
5
use Route4Me\Enum\Endpoint;
6
7
class AddressBookLocation extends Common
8
{
9
    public $address_id;
10
    public $address_group;
11
    public $address_alias;
12
    public $address_1;
13
    public $address_2;
14
    public $first_name;
15
    public $last_name;
16
    public $address_email;
17
    public $address_phone_number;
18
    public $address_city;
19
    public $address_state_id;
20
    public $address_country_id;
21
    public $address_zip;
22
    public $cached_lat;
23
    public $cached_lng;
24
    public $curbside_lat;
25
    public $curbside_lng;
26
    public $color;
27
    public $address_custom_data;
28
    public $schedule;
29
    
30
    public $created_timestamp;
31
    public $member_id;
32
    public $schedule_blacklist;
33
    public $in_route_count;
34
    public $last_visited_timestamp;
35
    public $last_routed_timestamp;
36
    public $local_time_window_start;
37
    public $local_time_window_end;
38
    public $local_time_window_start_2;
39
    public $local_time_window_end_2;
40
    public $service_time;
41
    public $local_timezone_string;
42
    public $address_icon;
43
    public $address_stop_type;
44
    public $address_cube;
45
    public $address_pieces;
46
    public $address_reference_no;
47
    public $address_revenue;
48
    public $address_weight;
49
    public $address_priority;
50
    public $address_customer_po;
51
    
52
    public static function fromArray(array $params)
53
    {
54
        $addressbooklocation = new AddressBookLocation();
55
        
56
        foreach ($params as $key => $value) {
57
            if (property_exists($addressbooklocation, $key)) {
58
                $addressbooklocation->{$key} = $value;
59
            }
60
        }
61
        
62
        return $addressbooklocation;
63
    }
64
    
65
    
66
    
67 View Code Duplication
    public static function getAddressBookLocation($addressId)
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...
68
    {
69
        $ablocations = Route4Me::makeRequst(array(
70
            'url'    => Endpoint::ADDRESS_BOOK_V4,
71
            'method' => 'GET',
72
            'query'  => array(
73
                'query' => $addressId,
74
                'limit' => 30
75
            )
76
        ));
77
78
        return $ablocations;
79
    }
80
    
81 View Code Duplication
    public static function searchAddressBookLocations($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...
82
    {
83
        $allQueryFields = array('display', 'query', 'fields', 'limit', 'offset');
84
        
85
        $result = Route4Me::makeRequst(array(
86
            'url'    => Endpoint::ADDRESS_BOOK_V4,
87
            'method' => 'GET',
88
            'query'  => Route4Me::generateRequestParameters($allQueryFields, $params)
89
        ));
90
91
        return $result;
92
    }
93
    
94 View Code Duplication
    public static function getAddressBookLocations($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...
95
    {
96
        $allQueryFields = array('limit', 'offset', 'address_id');
97
        
98
        $ablocations = Route4Me::makeRequst(array(
99
            'url'    => Endpoint::ADDRESS_BOOK_V4,
100
            'method' => 'GET',
101
            'query'  => Route4Me::generateRequestParameters($allQueryFields, $params)
102
        ));
103
104
        return $ablocations;
105
    }
106
    
107
    public static function getRandomAddressBookLocation($params)
108
    {
109
        $ablocations = self::getAddressBookLocations($params);
110
        
111
        if (isset($ablocations["results"])) {
112
            $locationsSize = sizeof($ablocations["results"]);
113
            
114
            if ($locationsSize>0) {
115
                $randomLocationIndex = rand(0, $locationsSize - 1);
116
                return $ablocations["results"][$randomLocationIndex];
117
            } 
118
        } 
119
120
        return null;
121
    }
122
    
123
    /**
124
     * @param AddressBookLocation $params
125
    */
126 View Code Duplication
    public static function addAdressBookLocation($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
        $allBodyFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('address_id', 'in_route_count'));
129
        
130
        $response = Route4Me::makeRequst(array(
131
            'url'    => Endpoint::ADDRESS_BOOK_V4,
132
            'method' => 'POST',
133
            'body'   => Route4Me::generateRequestParameters($allBodyFields, $params)
134
        ));
135
136
        return $response;
137
    }
138
    
139 View Code Duplication
    public function deleteAdressBookLocation($address_ids)
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...
140
    {
141
        $result = Route4Me::makeRequst(array(
142
            'url'    => Endpoint::ADDRESS_BOOK_V4,
143
            'method' => 'DELETEARRAY',
144
            'query'  => array(
145
                'address_ids' => $address_ids
146
            )
147
        ));
148
149
        return $result;
150
    }
151
    
152
    public function updateAdressBookLocation($params)
153
    {
154
        $allBodyFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('in_route_count'));
155
156
        $response = Route4Me::makeRequst(array(
157
            'url'    => Endpoint::ADDRESS_BOOK_V4,
158
            'method' => 'PUT',
159
            'body'   => Route4Me::generateRequestParameters($allBodyFields, $params)
160
        ));
161
162
        return $response;
163
    }
164
        
165
    public static function validateScheduleMode($scheduleMode)
166
    {
167
        $schedMmodes = array("daily", "weekly", "monthly", "annually");
168
        
169
        if (in_array($scheduleMode, $schedMmodes)) {
170
            return TRUE; 
171
        } else {
172
            return FALSE;
173
        }
174
    }
175
    
176
    public static function validateScheduleEnable($scheduleEnabled)
177
    {
178
        $schedEnables = array(TRUE, FALSE);
179
        
180
        if (in_array($scheduleEnabled, $schedEnables)) {
181
            return TRUE;
182
        } else {
183
            return FALSE;
184
        }
185
    }
186
    
187
    public static function validateScheduleEvery($scheduleEvery)
188
    {
189
        if (is_numeric($scheduleEvery)) {
190
            return TRUE;
191
        } else {
192
            return FALSE;
193
        }
194
    }
195
    
196 View Code Duplication
    public static function validateScheduleWeekDays($scheduleWeekDays)
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...
197
    {
198
        $weekdays = explode(',', $scheduleWeekDays);
199
        
200
        if (sizeof($weekdays)<1) {
201
            return FALSE;
202
        }
203
        
204
        $isValid = TRUE;
205
        
206
        for ($i=0; $i<sizeof($weekdays); $i++) { 
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
207
            if (is_numeric($weekdays[$i])) {
208
                $wday = intval($weekdays[$i]);
209
                if ($wday<1 || $wday>7) {
210
                    $isValid = FALSE;
211
                }
212
            } else {
213
                $isValid = FALSE;
214
            }
215
        }
216
        
217
        return $isValid;
218
    }
219
    
220
    public static function validateScheduleMonthlyMode($scheduleMonthlyMode)
221
    {
222
        $schedMonthlyMmodes = array("dates", "nth");
223
        
224
        if (in_array($scheduleMonthlyMode, $schedMonthlyMmodes)) {
225
            return TRUE;
226
        } else {
227
            return FALSE;
228
        }
229
    }
230
    
231 View Code Duplication
    public static function validateScheduleMonthlyDates($scheduleMonthlyDates)
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...
232
    {
233
        $monthlyDates = explode(',', $scheduleMonthlyDates);
234
        
235
        if (sizeof($monthlyDates)<1) {
236
            return FALSE;
237
        }
238
        
239
        $isValid = TRUE;
240
        
241
        for ($i=0; $i<sizeof($monthlyDates); $i++) { 
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
242
            if (is_numeric($monthlyDates[$i])) {
243
                $mday = intval($monthlyDates[$i]);
244
                if ($mday <1 || $mday > 31) {
245
                    $isValid = FALSE;
246
                }
247
            } else {
248
                $isValid = FALSE;
249
            }
250
        }
251
252
        return $isValid;
253
    }
254
    
255
    public static function validateScheduleNthN($scheduleNthN)
256
    {
257
        if (!is_numeric($scheduleNthN)) {
258
            return FALSE;
259
        }
260
        
261
        $schedNthNs = array(1, 2, 3, 4, 5, -1);
262
        
263
        if (in_array($scheduleNthN, $schedNthNs)) {
264
            return TRUE;
265
        } else {
266
            return FALSE;
267
        }
268
    }
269
    
270
    public static function validateScheduleNthWhat($scheduleNthWhat)
271
    {
272
        if (!is_numeric($scheduleNthWhat)) {
273
            return FALSE;
274
        }
275
        
276
        $schedNthWhats = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
277
        
278
        if (in_array($scheduleNthWhat, $schedNthWhats)) {
279
            return TRUE;
280
        } else {
281
            return FALSE;
282
        }
283
    }
284
    
285
    /** Function adds the locations (with/without schedule) from the CSV file. 
286
     * $csvFileHandle - a file handler.
287
     * Returns array $results which contains two arrays: fail and succes.
288
     */
289
    public function addLocationsFromCsvFile($csvFileHandle, $locationsFieldsMapping)
290
    {
291
        $max_line_length = 512;
292
        $delemietr = ',';
293
        
294
        $results = array();
295
        $results['fail'] = array();
296
        $results['success'] = array();
297
        
298
        $columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr);
299
        
300
        $addressBookFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('address_id', 'in_route_count'));
301
302
        if (empty($columns)) {
303
            array_push($results['fail'], 'Empty CSV table');
304
            return ($results);
305
        }
306
307
        $iRow = 1;
308
        
309
        while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr))!==false) {
310
            if (!isset($rows[$locationsFieldsMapping['cached_lat']]) || !isset($rows[$locationsFieldsMapping['cached_lng']]) 
311
                  || !isset($rows[$locationsFieldsMapping['address_1']]) || array(null)==$rows) {
312
                continue;
313
            }
314
                      
315
            $curSchedule = "";
316
            $mode = "";
317
            
318
            $failCount = sizeof($results['fail']); 
319
            
320 View Code Duplication
            if (isset($rows[$locationsFieldsMapping['schedule_mode']])) {
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...
321
                if ($this->validateScheduleMode($rows[$locationsFieldsMapping['schedule_mode']])) {
322
                    $curSchedule = '"mode":"'.$rows[$locationsFieldsMapping['schedule_mode']].'",'; 
323
                    $mode = $rows[$locationsFieldsMapping['schedule_mode']];
324
                } else {
325
                    array_push($results['fail'], "$iRow --> Wrong schedule mode parameter"); 
326
                }
327
            } else {
328
                array_push($results['fail'], "$iRow --> The schedule mode parameter is not set"); 
329
            }
330
            
331
            if (isset($rows[$locationsFieldsMapping['schedule_enabled']])) {
332
                if ($this->validateScheduleEnable($rows[$locationsFieldsMapping['schedule_enabled']])) { 
333
                    $curSchedule .= '"enabled":'.$rows[$locationsFieldsMapping['schedule_enabled']].',';
334
                } else {
335
                    array_push($results['fail'], "$iRow --> The schedule enabled parameter is not set ");  
336
                }
337
            }
338
            
339
            if (isset($rows[$locationsFieldsMapping['schedule_every']])) {
340
                if ($this->validateScheduleEvery($rows[$locationsFieldsMapping['schedule_every']])) {
341
                    $curSchedule.='"'.$mode.'":{'.'"every":'.$rows[$locationsFieldsMapping['schedule_every']].','; 
342
                    if ($mode=='daily') {
343
                        $curSchedule = trim($curSchedule,',');
344
                        $curSchedule.='}';
345
                    }
346
                } else {
347
                    array_push($results['fail'], "$iRow --> The parameter sched_every is not set"); 
348
                }
349
            }
350
            
351
            if ($mode!='daily') {
352
                switch ($mode) {
353
                    case 'weekly':
354 View Code Duplication
                        if (isset($rows[$locationsFieldsMapping['schedule_weekdays']])) {
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...
355
                            if ($this->validateScheduleWeekDays($rows[$locationsFieldsMapping['schedule_weekdays']])) {
356
                                 $curSchedule .= '"weekdays":['.$rows[$locationsFieldsMapping['schedule_weekdays']].']}';
357
                            } else {
358
                                array_push($results['fail'], "$iRow --> Wrong weekdays"); 
359
                            }
360
                        } else {
361
                            array_push($results['fail'], "$iRow --> The parameters sched_weekdays is not set"); 
362
                        }
363
                        break;
364
                    case 'monthly':
365
                        $monthlyMode = "";
366 View Code Duplication
                        if (isset($rows[$locationsFieldsMapping['monthly_mode']])) {
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...
367
                            if ($this->validateScheduleMonthlyMode($rows[$locationsFieldsMapping['monthly_mode']])) {
368
                                 $monthlyMode = $rows[$locationsFieldsMapping['monthly_mode']];
369
                                 $curSchedule .= '"mode": "'.$rows[$locationsFieldsMapping['monthly_mode']].'",';
370
                            } else {
371
                                array_push($results['fail'], "$iRow --> Wrong monthly mode"); 
372
                            }
373
                        } else {
374
                            array_push($results['fail'], "$iRow --> The parameter sched_monthly_mode is not set"); 
375
                        }
376
                        
377
                        if ($monthlyMode!="") {
378
                            switch ($monthlyMode) {
379
                                case 'dates':
380
                                    if (isset($rows[$locationsFieldsMapping['monthly_dates']])) {
381
                                        if ($this->validateScheduleMonthlyDates($rows[$locationsFieldsMapping['monthly_dates']])) {
382
                                             $curSchedule .= '"dates":['.$rows[$locationsFieldsMapping['monthly_dates']].']}';
383
                                        } else {
384
                                            array_push($results['fail'], "$iRow --> Wrong monthly dates"); 
385
                                        }
386
                                    }
387
                                    break;
388
                                case 'nth':
389 View Code Duplication
                                    if (isset($rows[$locationsFieldsMapping['monthly_nth_n']])) {
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...
390
                                        if ($this->validateScheduleNthN($rows[$locationsFieldsMapping['monthly_nth_n']])) {
391
                                             $curSchedule .= '"nth":{"n":'.$rows[$locationsFieldsMapping['monthly_nth_n']].',';
392
                                        } else {
393
                                            array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_n"); 
394
                                        }
395
                                    } else {
396
                                        array_push($results['fail'], "$iRow --> The parameter sched_nth_n is not set"); 
397
                                    }
398
                                    
399 View Code Duplication
                                    if ($curSchedule!="") {
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...
400
                                        if (isset($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) {
401
                                            if ($this->validateScheduleNthWhat($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) {
402
                                                 $curSchedule .= '"what":'.$rows[$locationsFieldsMapping['monthly_nth_wwhat']].'}}';
403
                                            } else {
404
                                                array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_what"); 
405
                                            }
406
                                        } else {
407
                                            array_push($results['fail'], "$iRow --> The parameter sched_nth_what is not set"); 
408
                                        }
409
                                    }
410
                                    break;
411
                            }
412
                        }
413
                        break;
414
                    default:
415
                        $curSchedule = "";
416
                        break;
417
                }
418
            }
419
420
            if (sizeof($results['fail'])>$failCount) {
421
                $curSchedule = "";
422
            }
423
424
            if (($mode=='daily' || $mode=='weekly' || $mode=='monthy') && $curSchedule=="") {
425
                $iRow++; 
426
                continue;
427
            }
428
            
429
            $curSchedule = strtolower($curSchedule);
430
            
431
            $curSchedule = '[{'.$curSchedule.'}]';
432
433
            $oSchedule = json_decode($curSchedule,TRUE);
0 ignored issues
show
Unused Code introduced by
$oSchedule 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...
434
            
435
            $parametersArray = array();
436
            
437
            foreach ($addressBookFields as $addressBookField) {
438
                if (isset($locationsFieldsMapping[$addressBookField])) {
439
                    $parametersArray[$addressBookField] = $rows[$locationsFieldsMapping[$addressBookField]];
440
                }
441
            }
442
            
443
            $AdressBookLocationParameters = AddressBookLocation::fromArray($parametersArray);
444
            
445
            $abContacts = new AddressBookLocation();
446
447
            $abcResults = $abContacts->addAdressBookLocation($AdressBookLocationParameters); //temporarry
448
            
449
            array_push($results['success'], "The schedule location with address_id = ".strval($abcResults["address_id"])." added successfuly.");
450
        }
451
452
        return $results;
453
    }
454
 }
455