DirectionRequest   F
last analyzed

Complexity

Total Complexity 75

Size/Duplication

Total Lines 558
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 75
lcom 1
cbo 2
dl 0
loc 558
ccs 168
cts 168
cp 1
rs 2.4
c 0
b 0
f 0

53 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOrigin() 0 4 1
A setOrigin() 0 4 1
A getDestination() 0 4 1
A setDestination() 0 4 1
A hasDepartureTime() 0 4 1
A getDepartureTime() 0 4 1
A setDepartureTime() 0 4 1
A hasArrivalTime() 0 4 1
A getArrivalTime() 0 4 1
A setArrivalTime() 0 4 1
A hasWaypoints() 0 4 1
A getWaypoints() 0 4 1
A setWaypoints() 0 5 1
A addWaypoints() 0 6 2
A hasWaypoint() 0 4 1
A addWaypoint() 0 6 2
A removeWaypoint() 0 5 2
A hasOptimizeWaypoints() 0 4 1
A getOptimizeWaypoints() 0 4 1
A setOptimizeWaypoints() 0 4 1
A hasTravelMode() 0 4 1
A getTravelMode() 0 4 1
A setTravelMode() 0 4 1
A hasAvoid() 0 4 1
A getAvoid() 0 4 1
A setAvoid() 0 4 1
A hasProvideRouteAlternatives() 0 4 1
A getProvideRouteAlternatives() 0 4 1
A setProvideRouteAlternatives() 0 4 1
A hasTrafficModel() 0 4 1
A getTrafficModel() 0 4 1
A setTrafficModel() 0 4 1
A hasTransitModes() 0 4 1
A getTransitModes() 0 4 1
A setTransitModes() 0 5 1
A addTransitModes() 0 6 2
A hasTransitMode() 0 4 1
A addTransitMode() 0 6 2
A removeTransitMode() 0 5 2
A hasTransitRoutingPreference() 0 4 1
A getTransitRoutingPreference() 0 4 1
A setTransitRoutingPreference() 0 4 1
A hasRegion() 0 4 1
A getRegion() 0 4 1
A setRegion() 0 4 1
A hasUnitSystem() 0 4 1
A getUnitSystem() 0 4 1
A setUnitSystem() 0 4 1
A hasLanguage() 0 4 1
A getLanguage() 0 4 1
A setLanguage() 0 4 1
F buildQuery() 0 67 17

How to fix   Complexity   

Complex Class

Complex classes like DirectionRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DirectionRequest, and based on these observations, apply Extract Interface, too.

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 192
    public function __construct(LocationInterface $origin, LocationInterface $destination)
99
    {
100 192
        $this->setOrigin($origin);
101 192
        $this->setDestination($destination);
102 192
    }
103
104
    /**
105
     * @return LocationInterface
106
     */
107 8
    public function getOrigin()
108
    {
109 8
        return $this->origin;
110
    }
111
112 192
    public function setOrigin(LocationInterface $origin)
113
    {
114 192
        $this->origin = $origin;
115 192
    }
116
117
    /**
118
     * @return LocationInterface
119
     */
120 8
    public function getDestination()
121
    {
122 8
        return $this->destination;
123
    }
124
125 192
    public function setDestination(LocationInterface $destination)
126
    {
127 192
        $this->destination = $destination;
128 192
    }
129
130
    /**
131
     * @return bool
132
     */
133 68
    public function hasDepartureTime()
134
    {
135 68
        return null !== $this->departureTime;
136
    }
137
138
    /**
139
     * @return \DateTime|null
140
     */
141 12
    public function getDepartureTime()
142
    {
143 12
        return $this->departureTime;
144
    }
145
146 12
    public function setDepartureTime(\DateTime $departureTime = null)
147
    {
148 12
        $this->departureTime = $departureTime;
149 12
    }
150
151
    /**
152
     * @return bool
153
     */
154 68
    public function hasArrivalTime()
155
    {
156 68
        return null !== $this->arrivalTime;
157
    }
158
159
    /**
160
     * @return \DateTime|null
161
     */
162 12
    public function getArrivalTime()
163
    {
164 12
        return $this->arrivalTime;
165
    }
166
167 12
    public function setArrivalTime(\DateTime $arrivalTime = null)
168
    {
169 12
        $this->arrivalTime = $arrivalTime;
170 12
    }
171
172
    /**
173
     * @return bool
174
     */
175 76
    public function hasWaypoints()
176
    {
177 76
        return !empty($this->waypoints);
178
    }
179
180
    /**
181
     * @return DirectionWaypoint[]
182
     */
183 20
    public function getWaypoints()
184
    {
185 20
        return $this->waypoints;
186
    }
187
188
    /**
189
     * @param DirectionWaypoint[] $waypoints
190
     */
191 8
    public function setWaypoints(array $waypoints)
192
    {
193 8
        $this->waypoints = [];
194 8
        $this->addWaypoints($waypoints);
195 8
    }
196
197
    /**
198
     * @param DirectionWaypoint[] $waypoints
199
     */
200 12
    public function addWaypoints(array $waypoints)
201
    {
202 12
        foreach ($waypoints as $waypoint) {
203 12
            $this->addWaypoint($waypoint);
204
        }
205 12
    }
206
207
    /**
208
     * @return bool
209
     */
210 24
    public function hasWaypoint(DirectionWaypoint $waypoint)
211
    {
212 24
        return in_array($waypoint, $this->waypoints, true);
213
    }
214
215 24
    public function addWaypoint(DirectionWaypoint $waypoint)
216
    {
217 24
        if (!$this->hasWaypoint($waypoint)) {
218 24
            $this->waypoints[] = $waypoint;
219
        }
220 24
    }
221
222 4
    public function removeWaypoint(DirectionWaypoint $waypoint)
223
    {
224 4
        unset($this->waypoints[array_search($waypoint, $this->waypoints, true)]);
225 4
        $this->waypoints = empty($this->waypoints) ? [] : array_values($this->waypoints);
226 4
    }
227
228
    /**
229
     * @return bool
230
     */
231 12
    public function hasOptimizeWaypoints()
232
    {
233 12
        return null !== $this->optimizeWaypoints;
234
    }
235
236
    /**
237
     * @return bool|null
238
     */
239 12
    public function getOptimizeWaypoints()
240
    {
241 12
        return $this->optimizeWaypoints;
242
    }
243
244
    /**
245
     * @param bool|null $optimizeWaypoints
246
     */
247 12
    public function setOptimizeWaypoints($optimizeWaypoints = null)
248
    {
249 12
        $this->optimizeWaypoints = $optimizeWaypoints;
250 12
    }
251
252
    /**
253
     * @return bool
254
     */
255 68
    public function hasTravelMode()
256
    {
257 68
        return null !== $this->travelMode;
258
    }
259
260
    /**
261
     * @return string|null
262
     */
263 12
    public function getTravelMode()
264
    {
265 12
        return $this->travelMode;
266
    }
267
268
    /**
269
     * @param string|null $travelMode
270
     */
271 12
    public function setTravelMode($travelMode = null)
272
    {
273 12
        $this->travelMode = $travelMode;
274 12
    }
275
276
    /**
277
     * @return bool
278
     */
279 68
    public function hasAvoid()
280
    {
281 68
        return null !== $this->avoid;
282
    }
283
284
    /**
285
     * @return string|null
286
     */
287 12
    public function getAvoid()
288
    {
289 12
        return $this->avoid;
290
    }
291
292
    /**
293
     * @param string|null $avoid
294
     */
295 12
    public function setAvoid($avoid = null)
296
    {
297 12
        $this->avoid = $avoid;
298 12
    }
299
300
    /**
301
     * @return bool
302
     */
303 68
    public function hasProvideRouteAlternatives()
304
    {
305 68
        return null !== $this->provideRouteAlternatives;
306
    }
307
308
    /**
309
     * @return bool|null
310
     */
311 12
    public function getProvideRouteAlternatives()
312
    {
313 12
        return $this->provideRouteAlternatives;
314
    }
315
316
    /**
317
     * @param bool|null $provideRouteAlternatives
318
     */
319 12
    public function setProvideRouteAlternatives($provideRouteAlternatives = null)
320
    {
321 12
        $this->provideRouteAlternatives = $provideRouteAlternatives;
322 12
    }
323
324
    /**
325
     * @return bool
326
     */
327 68
    public function hasTrafficModel()
328
    {
329 68
        return null !== $this->trafficModel;
330
    }
331
332
    /**
333
     * @return string|null
334
     */
335 12
    public function getTrafficModel()
336
    {
337 12
        return $this->trafficModel;
338
    }
339
340
    /**
341
     * @param string|null $trafficModel
342
     */
343 12
    public function setTrafficModel($trafficModel)
344
    {
345 12
        $this->trafficModel = $trafficModel;
346 12
    }
347
348
    /**
349
     * @return bool
350
     */
351 76
    public function hasTransitModes()
352
    {
353 76
        return !empty($this->transitModes);
354
    }
355
356
    /**
357
     * @return string[]
358
     */
359 20
    public function getTransitModes()
360
    {
361 20
        return $this->transitModes;
362
    }
363
364
    /**
365
     * @param string[] $transitModes
366
     */
367 12
    public function setTransitModes(array $transitModes)
368
    {
369 12
        $this->transitModes = [];
370 12
        $this->addTransitModes($transitModes);
371 12
    }
372
373
    /**
374
     * @param string[] $transitModes
375
     */
376 12
    public function addTransitModes(array $transitModes)
377
    {
378 12
        foreach ($transitModes as $transitMode) {
379 12
            $this->addTransitMode($transitMode);
380
        }
381 12
    }
382
383
    /**
384
     * @param string $transitMode
385
     *
386
     * @return bool
387
     */
388 20
    public function hasTransitMode($transitMode)
389
    {
390 20
        return in_array($transitMode, $this->transitModes, true);
391
    }
392
393
    /**
394
     * @param string $transitMode
395
     */
396 20
    public function addTransitMode($transitMode)
397
    {
398 20
        if (!$this->hasTransitMode($transitMode)) {
399 20
            $this->transitModes[] = $transitMode;
400
        }
401 20
    }
402
403
    /**
404
     * @param string $transitMode
405
     */
406 4
    public function removeTransitMode($transitMode)
407
    {
408 4
        unset($this->transitModes[array_search($transitMode, $this->transitModes, true)]);
409 4
        $this->transitModes = empty($this->transitModes) ? [] : array_values($this->transitModes);
410 4
    }
411
412
    /**
413
     * @return bool
414
     */
415 68
    public function hasTransitRoutingPreference()
416
    {
417 68
        return null !== $this->transitRoutingPreference;
418
    }
419
420
    /**
421
     * @return string|null
422
     */
423 12
    public function getTransitRoutingPreference()
424
    {
425 12
        return $this->transitRoutingPreference;
426
    }
427
428
    /**
429
     * @param string|null $transitRoutingPreference
430
     */
431 12
    public function setTransitRoutingPreference($transitRoutingPreference)
432
    {
433 12
        $this->transitRoutingPreference = $transitRoutingPreference;
434 12
    }
435
436
    /**
437
     * @return bool
438
     */
439 68
    public function hasRegion()
440
    {
441 68
        return null !== $this->region;
442
    }
443
444
    /**
445
     * @return string|null
446
     */
447 12
    public function getRegion()
448
    {
449 12
        return $this->region;
450
    }
451
452
    /**
453
     * @param string|null $region
454
     */
455 12
    public function setRegion($region = null)
456
    {
457 12
        $this->region = $region;
458 12
    }
459
460
    /**
461
     * @return bool
462
     */
463 68
    public function hasUnitSystem()
464
    {
465 68
        return null !== $this->unitSystem;
466
    }
467
468
    /**
469
     * @return string|null
470
     */
471 12
    public function getUnitSystem()
472
    {
473 12
        return $this->unitSystem;
474
    }
475
476
    /**
477
     * @param string|null $unitSystem
478
     */
479 12
    public function setUnitSystem($unitSystem = null)
480
    {
481 12
        $this->unitSystem = $unitSystem;
482 12
    }
483
484
    /**
485
     * @return bool
486
     */
487 68
    public function hasLanguage()
488
    {
489 68
        return null !== $this->language;
490
    }
491
492
    /**
493
     * @return string|null
494
     */
495 12
    public function getLanguage()
496
    {
497 12
        return $this->language;
498
    }
499
500
    /**
501
     * @param string|null $language
502
     */
503 12
    public function setLanguage($language = null)
504
    {
505 12
        $this->language = $language;
506 12
    }
507
508
    /**
509
     * {@inheritdoc}
510
     */
511 56
    public function buildQuery()
512
    {
513
        $query = [
514 56
            'origin'      => $this->origin->buildQuery(),
515 56
            'destination' => $this->destination->buildQuery(),
516
        ];
517
518 56
        if ($this->hasDepartureTime()) {
519 4
            $query['departure_time'] = $this->departureTime->getTimestamp();
520
        }
521
522 56
        if ($this->hasArrivalTime()) {
523 4
            $query['arrival_time'] = $this->arrivalTime->getTimestamp();
524
        }
525
526 56
        if ($this->hasWaypoints()) {
527 8
            $waypoints = [];
528
529 8
            if ($this->optimizeWaypoints) {
530 4
                $waypoints[] = 'optimize:true';
531
            }
532
533 8
            foreach ($this->waypoints as $waypoint) {
534 8
                $waypoints[] = ($waypoint->getStopover() ? 'via:' : '').$waypoint->getLocation()->buildQuery();
535
            }
536
537 8
            $query['waypoints'] = implode('|', $waypoints);
538
        }
539
540 56
        if ($this->hasTravelMode()) {
541 4
            $query['mode'] = strtolower($this->travelMode);
542
        }
543
544 56
        if ($this->hasAvoid()) {
545 4
            $query['avoid'] = $this->avoid;
546
        }
547
548 56
        if ($this->hasProvideRouteAlternatives()) {
549 4
            $query['alternatives'] = $this->provideRouteAlternatives ? 'true' : 'false';
550
        }
551
552 56
        if ($this->hasTrafficModel()) {
553 4
            $query['traffic_model'] = $this->trafficModel;
554
        }
555
556 56
        if ($this->hasTransitModes()) {
557 4
            $query['transit_mode'] = implode('|', $this->transitModes);
558
        }
559
560 56
        if ($this->hasTransitRoutingPreference()) {
561 4
            $query['transit_routing_preference'] = $this->transitRoutingPreference;
562
        }
563
564 56
        if ($this->hasRegion()) {
565 4
            $query['region'] = $this->region;
566
        }
567
568 56
        if ($this->hasUnitSystem()) {
569 4
            $query['units'] = strtolower($this->unitSystem);
570
        }
571
572 56
        if ($this->hasLanguage()) {
573 4
            $query['language'] = $this->language;
574
        }
575
576 56
        return $query;
577
    }
578
}
579