DistanceMatrixRequest::buildQuery()   F
last analyzed

Complexity

Conditions 12
Paths 1024

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 12.0059

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 28
cts 29
cp 0.9655
rs 3.1345
c 0
b 0
f 0
cc 12
nc 1024
nop 0
crap 12.0059

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