Passed
Push — feature/v4 ( e5e380...dac4aa )
by Samuel
11:37
created

DistanceMatrixRequest   F

Complexity

Total Complexity 70

Size/Duplication

Total Lines 373
Duplicated Lines 0 %

Test Coverage

Coverage 99.38%

Importance

Changes 0
Metric Value
wmc 70
eloc 104
c 0
b 0
f 0
dl 0
loc 373
ccs 161
cts 162
cp 0.9938
rs 2.8

50 Methods

Rating   Name   Duplication   Size   Complexity  
A setTravelMode() 0 3 1
A getAvoid() 0 3 1
A addTransitModes() 0 4 2
A getArrivalTime() 0 3 1
A setUnitSystem() 0 3 1
A getTransitRoutingPreference() 0 3 1
A setDepartureTime() 0 3 1
A hasDestination() 0 3 1
A setRegion() 0 3 1
A addOrigins() 0 4 2
F buildQuery() 0 58 12
A addTransitMode() 0 4 2
A hasDepartureTime() 0 3 1
A setLanguage() 0 3 1
A getUnitSystem() 0 3 1
A hasOrigin() 0 3 1
A hasUnitSystem() 0 3 1
A setTransitModes() 0 4 1
A getTransitModes() 0 3 1
A addDestinations() 0 4 2
A removeTransitMode() 0 4 2
A removeOrigin() 0 4 2
A getDestinations() 0 3 1
A hasTrafficModel() 0 3 1
A addDestination() 0 4 2
A setTransitRoutingPreference() 0 3 1
A hasRegion() 0 3 1
A getTravelMode() 0 3 1
A hasDestinations() 0 3 1
A hasOrigins() 0 3 1
A setAvoid() 0 3 1
A setArrivalTime() 0 3 1
A hasAvoid() 0 3 1
A getTrafficModel() 0 3 1
A hasTransitModes() 0 3 1
A setDestinations() 0 4 1
A getOrigins() 0 3 1
A getLanguage() 0 3 1
A __construct() 0 4 1
A getRegion() 0 3 1
A addOrigin() 0 4 2
A setTrafficModel() 0 3 1
A getDepartureTime() 0 3 1
A hasTransitMode() 0 3 1
A hasTransitRoutingPreference() 0 3 1
A setOrigins() 0 4 1
A removeDestination() 0 4 2
A hasTravelMode() 0 3 1
A hasArrivalTime() 0 3 1
A hasLanguage() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like DistanceMatrixRequest 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.

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 DistanceMatrixRequest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Ivory Google Map package.
7
 *
8
 * (c) Eric GELOEN <[email protected]>
9
 *
10
 * For the full copyright and license information, please read the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ivory\GoogleMap\Service\DistanceMatrix\Request;
15
16
use DateTime;
17
use Ivory\GoogleMap\Service\Base\Location\EncodedPolylineLocation;
18
use Ivory\GoogleMap\Service\Base\Location\LocationInterface;
19
20
/**
21
 * @see http://code.google.com/apis/maps/documentation/javascript/reference.html#DistanceMatrixRequest
22
 */
23
class DistanceMatrixRequest implements DistanceMatrixRequestInterface
24
{
25
    /** @var LocationInterface[] */
26
    private $origins = [];
27
28
    /** @var LocationInterface[] */
29
    private $destinations = [];
30
31
    /** @var DateTime|null */
32
    private $departureTime;
33
34
    /** @var DateTime|null */
35
    private $arrivalTime;
36
37
    /** @var string|null */
38
    private $travelMode;
39
40
    /** @var string|null */
41
    private $avoid;
42
43
    /** @var string|null */
44
    private $trafficModel;
45
46
    /** @var string[] */
47
    private $transitModes = [];
48
49
    /** @var string|null */
50
    private $transitRoutingPreference;
51
52
    /** @var string|null */
53
    private $region;
54
55
    /** @var string|null */
56
    private $unitSystem;
57
58
    /** @var string|null */
59
    private $language;
60
61
    /**
62
     * @param LocationInterface[] $origins
63
     * @param LocationInterface[] $destinations
64
     */
65 43
    public function __construct(array $origins, array $destinations)
66
    {
67 43
        $this->setOrigins($origins);
68 43
        $this->setDestinations($destinations);
69 43
    }
70
71 5
    public function hasOrigins(): bool
72
    {
73 5
        return !empty($this->origins);
74
    }
75
76
    /** @return LocationInterface[] */
77 5
    public function getOrigins(): array
78
    {
79 5
        return $this->origins;
80
    }
81
82
    /** @param LocationInterface[] $origins */
83 43
    public function setOrigins(array $origins): void
84
    {
85 43
        $this->origins = [];
86 43
        $this->addOrigins($origins);
87 43
    }
88
89
    /** @param LocationInterface[] $origins */
90 43
    public function addOrigins(array $origins): void
91
    {
92 43
        foreach ($origins as $origin) {
93 43
            $this->addOrigin($origin);
94
        }
95 43
    }
96
97 43
    public function hasOrigin(LocationInterface $origin): bool
98
    {
99 43
        return in_array($origin, $this->origins, true);
100
    }
101
102 43
    public function addOrigin(LocationInterface $origin): void
103
    {
104 43
        if (!$this->hasOrigin($origin)) {
105 43
            $this->origins[] = $origin;
106
        }
107 43
    }
108
109 1
    public function removeOrigin(LocationInterface $origin): void
110
    {
111 1
        unset($this->origins[array_search($origin, $this->origins, true)]);
112 1
        $this->origins = empty($this->origins) ? [] : array_values($this->origins);
113 1
    }
114
115 5
    public function hasDestinations(): bool
116
    {
117 5
        return !empty($this->destinations);
118
    }
119
120
    /** @return LocationInterface[] */
121 5
    public function getDestinations(): array
122
    {
123 5
        return $this->destinations;
124
    }
125
126
    /** @param LocationInterface[] $destinations */
127 43
    public function setDestinations(array $destinations): void
128
    {
129 43
        $this->destinations = [];
130 43
        $this->addDestinations($destinations);
131 43
    }
132
133
    /** @param LocationInterface[] $destinations */
134 43
    public function addDestinations(array $destinations): void
135
    {
136 43
        foreach ($destinations as $destination) {
137 43
            $this->addDestination($destination);
138
        }
139 43
    }
140
141 43
    public function hasDestination(LocationInterface $destination): bool
142
    {
143 43
        return in_array($destination, $this->destinations, true);
144
    }
145
146 43
    public function addDestination(LocationInterface $destination): void
147
    {
148 43
        if (!$this->hasDestination($destination)) {
149 43
            $this->destinations[] = $destination;
150
        }
151 43
    }
152
153 1
    public function removeDestination(LocationInterface $destination): void
154
    {
155 1
        unset($this->destinations[array_search($destination, $this->destinations, true)]);
156 1
        $this->destinations = empty($this->destinations) ? [] : array_values($this->destinations);
157 1
    }
158
159 14
    public function hasDepartureTime(): bool
160
    {
161 14
        return null !== $this->departureTime;
162
    }
163
164 3
    public function getDepartureTime(): ?DateTime
165
    {
166 3
        return $this->departureTime;
167
    }
168
169 3
    public function setDepartureTime(DateTime $departureTime = null): void
170
    {
171 3
        $this->departureTime = $departureTime;
172 3
    }
173
174 14
    public function hasArrivalTime(): bool
175
    {
176 14
        return null !== $this->arrivalTime;
177
    }
178
179 3
    public function getArrivalTime(): ?DateTime
180
    {
181 3
        return $this->arrivalTime;
182
    }
183
184 3
    public function setArrivalTime(DateTime $arrivalTime = null): void
185
    {
186 3
        $this->arrivalTime = $arrivalTime;
187 3
    }
188
189 14
    public function hasTravelMode(): bool
190
    {
191 14
        return null !== $this->travelMode;
192
    }
193
194 3
    public function getTravelMode(): ?string
195
    {
196 3
        return $this->travelMode;
197
    }
198
199 3
    public function setTravelMode(string $travelMode = null): void
200
    {
201 3
        $this->travelMode = $travelMode;
202 3
    }
203
204 14
    public function hasAvoid(): bool
205
    {
206 14
        return null !== $this->avoid;
207
    }
208
209 3
    public function getAvoid(): ?string
210
    {
211 3
        return $this->avoid;
212
    }
213
214 3
    public function setAvoid(string $avoid = null): void
215
    {
216 3
        $this->avoid = $avoid;
217 3
    }
218
219 14
    public function hasTrafficModel(): bool
220
    {
221 14
        return null !== $this->trafficModel;
222
    }
223
224 3
    public function getTrafficModel(): ?string
225
    {
226 3
        return $this->trafficModel;
227
    }
228
229 3
    public function setTrafficModel(?string $trafficModel): void
230
    {
231 3
        $this->trafficModel = $trafficModel;
232 3
    }
233
234 16
    public function hasTransitModes(): bool
235
    {
236 16
        return !empty($this->transitModes);
237
    }
238
239
    /** @return string[] */
240 5
    public function getTransitModes(): array
241
    {
242 5
        return $this->transitModes;
243
    }
244
245
    /** @param string[] $transitModes */
246 3
    public function setTransitModes(array $transitModes): void
247
    {
248 3
        $this->transitModes = [];
249 3
        $this->addTransitModes($transitModes);
250 3
    }
251
252
    /** @param string[] $transitModes */
253 3
    public function addTransitModes(array $transitModes): void
254
    {
255 3
        foreach ($transitModes as $transitMode) {
256 3
            $this->addTransitMode($transitMode);
257
        }
258 3
    }
259
260 5
    public function hasTransitMode(string $transitMode): bool
261
    {
262 5
        return in_array($transitMode, $this->transitModes, true);
263
    }
264
265 5
    public function addTransitMode(string $transitMode): void
266
    {
267 5
        if (!$this->hasTransitMode($transitMode)) {
268 5
            $this->transitModes[] = $transitMode;
269
        }
270 5
    }
271
272 1
    public function removeTransitMode(string $transitMode): void
273
    {
274 1
        unset($this->transitModes[array_search($transitMode, $this->transitModes, true)]);
275 1
        $this->transitModes = empty($this->transitModes) ? [] : array_values($this->transitModes);
276 1
    }
277
278 14
    public function hasTransitRoutingPreference(): bool
279
    {
280 14
        return null !== $this->transitRoutingPreference;
281
    }
282
283 3
    public function getTransitRoutingPreference(): ?string
284
    {
285 3
        return $this->transitRoutingPreference;
286
    }
287
288 3
    public function setTransitRoutingPreference(?string $transitRoutingPreference): void
289
    {
290 3
        $this->transitRoutingPreference = $transitRoutingPreference;
291 3
    }
292
293 14
    public function hasRegion(): bool
294
    {
295 14
        return null !== $this->region;
296
    }
297
298 3
    public function getRegion(): ?string
299
    {
300 3
        return $this->region;
301
    }
302
303 3
    public function setRegion(string $region = null): void
304
    {
305 3
        $this->region = $region;
306 3
    }
307
308 14
    public function hasUnitSystem(): bool
309
    {
310 14
        return null !== $this->unitSystem;
311
    }
312
313 3
    public function getUnitSystem(): ?string
314
    {
315 3
        return $this->unitSystem;
316
    }
317
318 3
    public function setUnitSystem(string $unitSystem = null): void
319
    {
320 3
        $this->unitSystem = $unitSystem;
321 3
    }
322
323 14
    public function hasLanguage(): bool
324
    {
325 14
        return null !== $this->language;
326
    }
327
328 3
    public function getLanguage(): ?string
329
    {
330 3
        return $this->language;
331
    }
332
333 3
    public function setLanguage(string $language = null): void
334
    {
335 3
        $this->language = $language;
336 3
    }
337
338 11
    public function buildQuery(): array
339
    {
340
        $locationBuilder = static function (LocationInterface $location) {
341 11
            $result = $location->buildQuery();
342
343 11
            if ($location instanceof EncodedPolylineLocation) {
344
                $result .= ':';
345
            }
346
347 11
            return $result;
348 11
        };
349
350
        $query = [
351 11
            'origins'      => implode('|', array_map($locationBuilder, $this->origins)),
352 11
            'destinations' => implode('|', array_map($locationBuilder, $this->destinations)),
353
        ];
354
355 11
        if ($this->hasDepartureTime()) {
356 1
            $query['departure_time'] = $this->departureTime->getTimestamp();
357
        }
358
359 11
        if ($this->hasArrivalTime()) {
360 1
            $query['arrival_time'] = $this->arrivalTime->getTimestamp();
361
        }
362
363 11
        if ($this->hasTravelMode()) {
364 1
            $query['mode'] = strtolower($this->travelMode);
365
        }
366
367 11
        if ($this->hasAvoid()) {
368 1
            $query['avoid'] = $this->avoid;
369
        }
370
371 11
        if ($this->hasTrafficModel()) {
372 1
            $query['traffic_model'] = $this->trafficModel;
373
        }
374
375 11
        if ($this->hasTransitModes()) {
376 1
            $query['transit_mode'] = implode('|', $this->transitModes);
377
        }
378
379 11
        if ($this->hasTransitRoutingPreference()) {
380 1
            $query['transit_routing_preference'] = $this->transitRoutingPreference;
381
        }
382
383 11
        if ($this->hasUnitSystem()) {
384 1
            $query['units'] = strtolower($this->unitSystem);
385
        }
386
387 11
        if ($this->hasRegion()) {
388 1
            $query['region'] = $this->region;
389
        }
390
391 11
        if ($this->hasLanguage()) {
392 1
            $query['language'] = $this->language;
393
        }
394
395 11
        return $query;
396
    }
397
}
398