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.

DirectionRequest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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