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 — services ( fe049d )
by Eric
03:30
created

DistanceMatrixRequest::build()   D

Complexity

Conditions 11
Paths 1024

Size

Total Lines 53
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
rs 4.1538
cc 11
eloc 27
nc 1024
nop 0

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
/*
4
 * This file is part of the Ivory Google Map package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\GoogleMap\Service\DistanceMatrix\Request;
13
14
use Ivory\GoogleMap\Service\Base\Location\LocationInterface;
15
16
/**
17
 * @see http://code.google.com/apis/maps/documentation/javascript/reference.html#DistanceMatrixRequest
18
 *
19
 * @author GeLo <[email protected]>
20
 */
21
class DistanceMatrixRequest
22
{
23
    /**
24
     * @var LocationInterface[]
25
     */
26
    private $origins = [];
27
28
    /**
29
     * @var LocationInterface[]
30
     */
31
    private $destinations = [];
32
33
    /**
34
     * @var \DateTime|null
35
     */
36
    private $departureTime;
37
38
    /**
39
     * @var \DateTime|null
40
     */
41
    private $arrivalTime;
42
43
    /**
44
     * @var string|null
45
     */
46
    private $travelMode;
47
48
    /**
49
     * @var string|null
50
     */
51
    private $avoid;
52
53
    /**
54
     * @var string|null
55
     */
56
    private $trafficModel;
57
58
    /**
59
     * @var string[]
60
     */
61
    private $transitModes = [];
62
63
    /**
64
     * @var string|null
65
     */
66
    private $transitRoutingPreference;
67
68
    /**
69
     * @var string|null
70
     */
71
    private $region;
72
73
    /**
74
     * @var string|null
75
     */
76
    private $unitSystem;
77
78
    /**
79
     * @var string|null
80
     */
81
    private $language;
82
83
    /**
84
     * @param LocationInterface[] $origins
85
     * @param LocationInterface[] $destinations
86
     */
87
    public function __construct(array $origins, array $destinations)
88
    {
89
        $this->setOrigins($origins);
90
        $this->setDestinations($destinations);
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    public function hasOrigins()
97
    {
98
        return !empty($this->origins);
99
    }
100
101
    /**
102
     * @return LocationInterface[]
103
     */
104
    public function getOrigins()
105
    {
106
        return $this->origins;
107
    }
108
109
    /**
110
     * @param LocationInterface[] $origins
111
     */
112
    public function setOrigins(array $origins)
113
    {
114
        $this->origins = [];
115
        $this->addOrigins($origins);
116
    }
117
118
    /**
119
     * @param LocationInterface[] $origins
120
     */
121
    public function addOrigins(array $origins)
122
    {
123
        foreach ($origins as $origin) {
124
            $this->addOrigin($origin);
125
        }
126
    }
127
128
    /**
129
     * @param LocationInterface $origin
130
     *
131
     * @return bool
132
     */
133
    public function hasOrigin(LocationInterface $origin)
134
    {
135
        return in_array($origin, $this->origins, true);
136
    }
137
138
    /**
139
     * @param LocationInterface $origin
140
     */
141
    public function addOrigin(LocationInterface $origin)
142
    {
143
        if (!$this->hasOrigin($origin)) {
144
            $this->origins[] = $origin;
145
        }
146
    }
147
148
    /**
149
     * @param LocationInterface $origin
150
     */
151
    public function removeOrigin(LocationInterface $origin)
152
    {
153
        unset($this->origins[array_search($origin, $this->origins, true)]);
154
        $this->origins = array_values($this->origins);
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function hasDestinations()
161
    {
162
        return !empty($this->destinations);
163
    }
164
165
    /**
166
     * @return LocationInterface[]
167
     */
168
    public function getDestinations()
169
    {
170
        return $this->destinations;
171
    }
172
173
    /**
174
     * @param LocationInterface[] $destinations
175
     */
176
    public function setDestinations(array $destinations)
177
    {
178
        $this->destinations = [];
179
        $this->addDestinations($destinations);
180
    }
181
182
    /**
183
     * @param LocationInterface[] $destinations
184
     */
185
    public function addDestinations(array $destinations)
186
    {
187
        foreach ($destinations as $destination) {
188
            $this->addDestination($destination);
189
        }
190
    }
191
192
    /**
193
     * @param LocationInterface $destination
194
     *
195
     * @return bool
196
     */
197
    public function hasDestination(LocationInterface $destination)
198
    {
199
        return in_array($destination, $this->destinations, true);
200
    }
201
202
    /**
203
     * @param LocationInterface $destination
204
     */
205
    public function addDestination(LocationInterface $destination)
206
    {
207
        if (!$this->hasDestination($destination)) {
208
            $this->destinations[] = $destination;
209
        }
210
    }
211
212
    /**
213
     * @param LocationInterface $destination
214
     */
215
    public function removeDestination(LocationInterface $destination)
216
    {
217
        unset($this->destinations[array_search($destination, $this->destinations, true)]);
218
        $this->destinations = array_values($this->destinations);
219
    }
220
221
    /**
222
     * @return bool
223
     */
224
    public function hasDepartureTime()
225
    {
226
        return $this->departureTime !== null;
227
    }
228
229
    /**
230
     * @return \DateTime|null
231
     */
232
    public function getDepartureTime()
233
    {
234
        return $this->departureTime;
235
    }
236
237
    /**
238
     * @param \DateTime|null $departureTime
239
     */
240
    public function setDepartureTime(\DateTime $departureTime = null)
241
    {
242
        $this->departureTime = $departureTime;
243
    }
244
245
    /**
246
     * @return bool
247
     */
248
    public function hasArrivalTime()
249
    {
250
        return $this->arrivalTime !== null;
251
    }
252
253
    /**
254
     * @return \DateTime|null
255
     */
256
    public function getArrivalTime()
257
    {
258
        return $this->arrivalTime;
259
    }
260
261
    /**
262
     * @param \DateTime|null $arrivalTime
263
     */
264
    public function setArrivalTime(\DateTime $arrivalTime = null)
265
    {
266
        $this->arrivalTime = $arrivalTime;
267
    }
268
269
    /**
270
     * @return bool
271
     */
272
    public function hasTravelMode()
273
    {
274
        return $this->travelMode !== null;
275
    }
276
277
    /**
278
     * @return string|null
279
     */
280
    public function getTravelMode()
281
    {
282
        return $this->travelMode;
283
    }
284
285
    /**
286
     * @param string|null $travelMode
287
     */
288
    public function setTravelMode($travelMode = null)
289
    {
290
        $this->travelMode = $travelMode;
291
    }
292
293
    /**
294
     * @return bool
295
     */
296
    public function hasAvoid()
297
    {
298
        return $this->avoid !== null;
299
    }
300
301
    /**
302
     * @return string|null
303
     */
304
    public function getAvoid()
305
    {
306
        return $this->avoid;
307
    }
308
309
    /**
310
     * @param string|null $avoid
311
     */
312
    public function setAvoid($avoid = null)
313
    {
314
        $this->avoid = $avoid;
315
    }
316
317
    /**
318
     * @return bool
319
     */
320
    public function hasTrafficModel()
321
    {
322
        return $this->trafficModel !== null;
323
    }
324
325
    /**
326
     * @return string|null
327
     */
328
    public function getTrafficModel()
329
    {
330
        return $this->trafficModel;
331
    }
332
333
    /**
334
     * @param string|null $trafficModel
335
     */
336
    public function setTrafficModel($trafficModel)
337
    {
338
        $this->trafficModel = $trafficModel;
339
    }
340
341
    /**
342
     * @return bool
343
     */
344
    public function hasTransitModes()
345
    {
346
        return !empty($this->transitModes);
347
    }
348
349
    /**
350
     * @return string[]
351
     */
352
    public function getTransitModes()
353
    {
354
        return $this->transitModes;
355
    }
356
357
    /**
358
     * @param string[] $transitModes
359
     */
360
    public function setTransitModes(array $transitModes)
361
    {
362
        $this->transitModes = [];
363
        $this->addTransitModes($transitModes);
364
    }
365
366
    /**
367
     * @param string[] $transitModes
368
     */
369
    public function addTransitModes(array $transitModes)
370
    {
371
        foreach ($transitModes as $transitMode) {
372
            $this->addTransitMode($transitMode);
373
        }
374
    }
375
376
    /**
377
     * @param string $transitMode
378
     *
379
     * @return bool
380
     */
381
    public function hasTransitMode($transitMode)
382
    {
383
        return in_array($transitMode, $this->transitModes, true);
384
    }
385
386
    /**
387
     * @param string $transitMode
388
     */
389
    public function addTransitMode($transitMode)
390
    {
391
        if (!$this->hasTransitMode($transitMode)) {
392
            $this->transitModes[] = $transitMode;
393
        }
394
    }
395
396
    /**
397
     * @param string $transitMode
398
     */
399
    public function removeTransitMode($transitMode)
400
    {
401
        unset($this->transitModes[array_search($transitMode, $this->transitModes, true)]);
402
        $this->transitModes = array_values($this->transitModes);
403
    }
404
405
    /**
406
     * @return bool
407
     */
408
    public function hasTransitRoutingPreference()
409
    {
410
        return $this->transitRoutingPreference !== null;
411
    }
412
413
    /**
414
     * @return string|null
415
     */
416
    public function getTransitRoutingPreference()
417
    {
418
        return $this->transitRoutingPreference;
419
    }
420
421
    /**
422
     * @param string|null $transitRoutingPreference
423
     */
424
    public function setTransitRoutingPreference($transitRoutingPreference)
425
    {
426
        $this->transitRoutingPreference = $transitRoutingPreference;
427
    }
428
429
    /**
430
     * @return bool
431
     */
432
    public function hasRegion()
433
    {
434
        return $this->region !== null;
435
    }
436
437
    /**
438
     * @return string|null
439
     */
440
    public function getRegion()
441
    {
442
        return $this->region;
443
    }
444
445
    /**
446
     * @param string|null $region
447
     */
448
    public function setRegion($region = null)
449
    {
450
        $this->region = $region;
451
    }
452
453
    /**
454
     * @return bool
455
     */
456
    public function hasUnitSystem()
457
    {
458
        return $this->unitSystem !== null;
459
    }
460
461
    /**
462
     * @return string|null
463
     */
464
    public function getUnitSystem()
465
    {
466
        return $this->unitSystem;
467
    }
468
469
    /**
470
     * @param string|null $unitSystem
471
     */
472
    public function setUnitSystem($unitSystem = null)
473
    {
474
        $this->unitSystem = $unitSystem;
475
    }
476
477
    /**
478
     * @return bool
479
     */
480
    public function hasLanguage()
481
    {
482
        return $this->language !== null;
483
    }
484
485
    /**
486
     * @return string|null
487
     */
488
    public function getLanguage()
489
    {
490
        return $this->language;
491
    }
492
493
    /**
494
     * @param string|null $language
495
     */
496
    public function setLanguage($language = null)
497
    {
498
        $this->language = $language;
499
    }
500
501
    /**
502
     * @return mixed[]
503
     */
504
    public function build()
505
    {
506
        $locationBuilder = function (LocationInterface $location) {
507
            return $location->build();
508
        };
509
510
        $query = [
511
            'origins'      => implode('|', array_map($locationBuilder, $this->origins)),
512
            'destinations' => implode('|', array_map($locationBuilder, $this->destinations)),
513
        ];
514
515
        if ($this->hasDepartureTime()) {
516
            $query['departure_time'] = $this->departureTime->getTimestamp();
517
        }
518
519
        if ($this->hasArrivalTime()) {
520
            $query['arrival_time'] = $this->arrivalTime->getTimestamp();
521
        }
522
523
        if ($this->hasTravelMode()) {
524
            $query['mode'] = strtolower($this->travelMode);
525
        }
526
527
        if ($this->hasAvoid()) {
528
            $query['avoid'] = $this->avoid;
529
        }
530
531
        if ($this->hasTrafficModel()) {
532
            $query['traffic_model'] = $this->trafficModel;
533
        }
534
535
        if ($this->hasTransitModes()) {
536
            $query['transit_mode'] = implode('|', $this->transitModes);
537
        }
538
539
        if ($this->hasTransitRoutingPreference()) {
540
            $query['transit_routing_preference'] = $this->transitRoutingPreference;
541
        }
542
543
        if ($this->hasUnitSystem()) {
544
            $query['units'] = strtolower($this->unitSystem);
545
        }
546
547
        if ($this->hasRegion()) {
548
            $query['region'] = $this->region;
549
        }
550
551
        if ($this->hasLanguage()) {
552
            $query['language'] = $this->language;
553
        }
554
555
        return $query;
556
    }
557
}
558