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 — parser ( 8d42bf )
by Eric
05:27
created

DirectionService::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
nop 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;
13
14
use Http\Client\HttpClient;
15
use Http\Message\MessageFactory;
16
use Ivory\GoogleMap\Base\Bound;
17
use Ivory\GoogleMap\Base\Coordinate;
18
use Ivory\GoogleMap\Overlay\EncodedPolyline;
19
use Ivory\GoogleMap\Service\AbstractService;
20
use Ivory\GoogleMap\Service\Base\Distance;
21
use Ivory\GoogleMap\Service\Base\Duration;
22
use Ivory\GoogleMap\Service\Base\Fare;
23
use Ivory\GoogleMap\Service\Base\Time;
24
use Ivory\GoogleMap\Service\Direction\Request\DirectionRequestInterface;
25
use Ivory\GoogleMap\Service\Direction\Response\DirectionGeocoded;
26
use Ivory\GoogleMap\Service\Direction\Response\DirectionLeg;
27
use Ivory\GoogleMap\Service\Direction\Response\DirectionResponse;
28
use Ivory\GoogleMap\Service\Direction\Response\DirectionRoute;
29
use Ivory\GoogleMap\Service\Direction\Response\DirectionStep;
30
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitAgency;
31
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitDetails;
32
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitLine;
33
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitStop;
34
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitVehicle;
35
use Ivory\GoogleMap\Service\Utility\Parser;
36
37
/**
38
 * @author GeLo <[email protected]>
39
 */
40
class DirectionService extends AbstractService
41
{
42
    /**
43
     * @param HttpClient     $client
44
     * @param MessageFactory $messageFactory
45
     * @param Parser|null    $parser
46
     */
47
    public function __construct(HttpClient $client, MessageFactory $messageFactory, Parser $parser = null)
48
    {
49
        parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/directions', $parser);
50
    }
51
52
    /**
53
     * @param DirectionRequestInterface $request
54
     *
55
     * @return DirectionResponse
56
     */
57 View Code Duplication
    public function route(DirectionRequestInterface $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $response = $this->getClient()->sendRequest($this->createRequest($request->build()));
60
        $data = $this->parse((string) $response->getBody(), [
61
            'pluralization_rules' => [
62
                'leg'            => 'legs',
63
                'route'          => 'routes',
64
                'step'           => 'steps',
65
                'waypoint_index' => 'waypoint_order',
66
            ],
67
        ]);
68
69
        return $this->buildResponse($data);
70
    }
71
72
    /**
73
     * @param mixed[] $data
74
     *
75
     * @return DirectionResponse
76
     */
77
    private function buildResponse(array $data)
78
    {
79
        $response = new DirectionResponse();
80
        $response->setStatus($data['status']);
81
82
        if (isset($data['routes'])) {
83
            $response->setRoutes($this->buildRoutes($data['routes']));
84
        }
85
86
        if (isset($data['geocoded_waypoints'])) {
87
            $response->setGeocodedWaypoints($this->buildGeocodedWaypoints($data['geocoded_waypoints']));
88
        }
89
90
        if (isset($data['available_travel_modes'])) {
91
            $response->setAvailableTravelModes($data['available_travel_modes']);
92
        }
93
94
        return $response;
95
    }
96
97
    /**
98
     * @param mixed[] $data
99
     *
100
     * @return DirectionRoute[]
101
     */
102
    private function buildRoutes(array $data)
103
    {
104
        $routes = [];
105
106
        foreach ($data as $route) {
107
            $routes[] = $this->buildRoute($route);
108
        }
109
110
        return $routes;
111
    }
112
113
    /**
114
     * @param mixed[] $data
115
     *
116
     * @return DirectionRoute
117
     */
118
    private function buildRoute(array $data)
119
    {
120
        $route = new DirectionRoute();
121
        $route->setBound($this->buildBound($data['bounds']));
122
        $route->setLegs($this->buildLegs($data['legs']));
123
        $route->setOverviewPolyline($this->buildEncodedPolyline($data['overview_polyline']));
124
125
        if (isset($data['copyrights'])) {
126
            $route->setCopyrights($data['copyrights']);
127
        }
128
129
        if (isset($data['summary'])) {
130
            $route->setSummary($data['summary']);
131
        }
132
133
        if (isset($data['fare'])) {
134
            $route->setFare($this->buildFare($data['fare']));
135
        }
136
137
        if (isset($data['warnings'])) {
138
            $route->setWarnings($data['warnings']);
139
        }
140
141
        if (isset($data['waypoint_order'])) {
142
            $route->setWaypointOrders($data['waypoint_order']);
143
        }
144
145
        return $route;
146
    }
147
148
    /**
149
     * @param mixed[] $data
150
     *
151
     * @return DirectionGeocoded[]
152
     */
153
    private function buildGeocodedWaypoints(array $data)
154
    {
155
        $waypoints = [];
156
157
        foreach ($data as $waypoint) {
158
            $waypoints[] = $this->buildGeocodedWaypoint($waypoint);
159
        }
160
161
        return $waypoints;
162
    }
163
164
    /**
165
     * @param mixed[] $data
166
     *
167
     * @return DirectionGeocoded
168
     */
169
    private function buildGeocodedWaypoint(array $data)
170
    {
171
        $geocodedWaypoint = new DirectionGeocoded();
172
        $geocodedWaypoint->setStatus($data['geocoder_status']);
173
        $geocodedWaypoint->setTypes($data['types']);
174
175
        if (isset($data['place_id'])) {
176
            $geocodedWaypoint->setPlaceId($data['place_id']);
177
        }
178
179
        if (isset($data['partial_match'])) {
180
            $geocodedWaypoint->setPartialMatch($data['partial_match']);
181
        }
182
183
        return $geocodedWaypoint;
184
    }
185
186
    /**
187
     * @param mixed[] $data
188
     *
189
     * @return DirectionLeg[]
190
     */
191
    private function buildLegs(array $data)
192
    {
193
        $legs = [];
194
195
        foreach ($data as $leg) {
196
            $legs[] = $this->buildLeg($leg);
197
        }
198
199
        return $legs;
200
    }
201
202
    /**
203
     * @param mixed[] $data
204
     *
205
     * @return DirectionLeg
206
     */
207
    private function buildLeg(array $data)
208
    {
209
        $leg = new DirectionLeg();
210
        $leg->setDistance($this->buildDistance($data['distance']));
211
        $leg->setDuration($this->buildDuration($data['duration']));
212
        $leg->setStartLocation($this->buildCoordinate($data['start_location']));
213
        $leg->setEndLocation($this->buildCoordinate($data['end_location']));
214
        $leg->setStartAddress($data['start_address']);
215
        $leg->setEndAddress($data['end_address']);
216
        $leg->setSteps($this->buildSteps($data['steps']));
217
218
        if (isset($data['departure_time'])) {
219
            $leg->setDepartureTime($this->buildTime($data['departure_time']));
220
        }
221
222
        if (isset($data['arrival_time'])) {
223
            $leg->setArrivalTime($this->buildTime($data['arrival_time']));
224
        }
225
226
        if (isset($data['via_waypoint'])) {
227
            $leg->setViaWaypoints($data['via_waypoint']);
228
        }
229
230
        if (isset($data['duration_in_traffic'])) {
231
            $leg->setDurationInTraffic($this->buildDuration($data['duration_in_traffic']));
232
        }
233
234
        return $leg;
235
    }
236
237
    /**
238
     * @param mixed[] $data
239
     *
240
     * @return DirectionStep[]
241
     */
242
    private function buildSteps(array $data)
243
    {
244
        $steps = [];
245
246
        foreach ($data as $step) {
247
            $steps[] = $this->buildStep($step);
248
        }
249
250
        return $steps;
251
    }
252
253
    /**
254
     * @param mixed[] $data
255
     *
256
     * @return DirectionStep
257
     */
258
    private function buildStep(array $data)
259
    {
260
        $step = new DirectionStep();
261
        $step->setDistance($this->buildDistance($data['distance']));
262
        $step->setDuration($this->buildDuration($data['duration']));
263
        $step->setEndLocation($this->buildCoordinate($data['end_location']));
264
        $step->setEncodedPolyline($this->buildEncodedPolyline($data['polyline']));
265
        $step->setStartLocation($this->buildCoordinate($data['start_location']));
266
        $step->setInstructions($data['html_instructions']);
267
        $step->setTravelMode($data['travel_mode']);
268
269
        if (isset($data['transit_details'])) {
270
            $step->setTransitDetails($this->buildTransitDetails($data['transit_details']));
271
        }
272
273
        return $step;
274
    }
275
276
    /**
277
     * @param mixed[] $data
278
     *
279
     * @return DirectionTransitDetails
280
     */
281
    private function buildTransitDetails(array $data)
282
    {
283
        $transitDetails = new DirectionTransitDetails();
284
        $transitDetails->setDepartureStop($this->buildTransitStop($data['departure_stop']));
285
        $transitDetails->setArrivalStop($this->buildTransitStop($data['arrival_stop']));
286
        $transitDetails->setDepartureTime($this->buildTime($data['departure_time']));
287
        $transitDetails->setArrivalTime($this->buildTime($data['arrival_time']));
288
        $transitDetails->setLine($this->buildTransitLine($data['line']));
289
        $transitDetails->setHeadSign($data['headsign']);
290
        $transitDetails->setNumStops($data['num_stops']);
291
292
        if (isset($data['headway'])) {
293
            $transitDetails->setHeadWay($data['headway']);
294
        }
295
296
        return $transitDetails;
297
    }
298
299
    /**
300
     * @param mixed[] $data
301
     *
302
     * @return DirectionTransitLine
303
     */
304
    private function buildTransitLine(array $data)
305
    {
306
        $transitLine = new DirectionTransitLine();
307
        $transitLine->setName($data['name']);
308
        $transitLine->setShortName($data['short_name']);
309
        $transitLine->setVehicle($this->buildTransitVehicle($data['vehicle']));
310
        $transitLine->setAgencies($this->buildTransitAgencies($data['agencies']));
311
312
        if (isset($data['color'])) {
313
            $transitLine->setColor($data['color']);
314
        }
315
316
        if (isset($data['url'])) {
317
            $transitLine->setUrl($data['url']);
318
        }
319
320
        if (isset($data['icon'])) {
321
            $transitLine->setIcon($data['icon']);
322
        }
323
324
        if (isset($data['text_color'])) {
325
            $transitLine->setTextColor($data['text_color']);
326
        }
327
328
        return $transitLine;
329
    }
330
331
    /**
332
     * @param mixed[] $data
333
     *
334
     * @return DirectionTransitAgency[]
335
     */
336
    private function buildTransitAgencies(array $data)
337
    {
338
        $agencies = [];
339
340
        foreach ($data as $agency) {
341
            $agencies[] = $this->buildTransitAgency($agency);
342
        }
343
344
        return $agencies;
345
    }
346
347
    /**
348
     * @param mixed[] $data
349
     *
350
     * @return DirectionTransitAgency
351
     */
352
    private function buildTransitAgency(array $data)
353
    {
354
        $transitAgency = new DirectionTransitAgency();
355
        $transitAgency->setName($data['name']);
356
        $transitAgency->setPhone($data['phone']);
357
        $transitAgency->setUrl($data['url']);
358
359
        return $transitAgency;
360
    }
361
362
    /**
363
     * @param mixed[] $data
364
     *
365
     * @return DirectionTransitStop
366
     */
367
    private function buildTransitStop(array $data)
368
    {
369
        $transitStop = new DirectionTransitStop();
370
        $transitStop->setName($data['name']);
371
        $transitStop->setLocation($this->buildCoordinate($data['location']));
372
373
        return $transitStop;
374
    }
375
376
    /**
377
     * @param mixed[] $data
378
     *
379
     * @return DirectionTransitVehicle
380
     */
381
    private function buildTransitVehicle(array $data)
382
    {
383
        $transitVehicle = new DirectionTransitVehicle();
384
        $transitVehicle->setName($data['name']);
385
        $transitVehicle->setIcon($data['icon']);
386
        $transitVehicle->setType($data['type']);
387
388
        return $transitVehicle;
389
    }
390
391
    /**
392
     * @param mixed[] $data
393
     *
394
     * @return Bound
395
     */
396
    private function buildBound(array $data)
397
    {
398
        return new Bound(
399
            $this->buildCoordinate($data['southwest']),
400
            $this->buildCoordinate($data['northeast'])
401
        );
402
    }
403
404
    /**
405
     * @param mixed[] $data
406
     *
407
     * @return Coordinate
408
     */
409
    private function buildCoordinate(array $data)
410
    {
411
        return new Coordinate($data['lat'], $data['lng']);
412
    }
413
414
    /**
415
     * @param mixed[] $data
416
     *
417
     * @return Distance
418
     */
419
    private function buildDistance(array $data)
420
    {
421
        return new Distance($data['value'], $data['text']);
422
    }
423
424
    /**
425
     * @param mixed[] $data
426
     *
427
     * @return Duration
428
     */
429
    private function buildDuration(array $data)
430
    {
431
        return new Duration($data['value'], $data['text']);
432
    }
433
434
    /**
435
     * @param string[] $data
436
     *
437
     * @return EncodedPolyline
438
     */
439
    private function buildEncodedPolyline(array $data)
440
    {
441
        return new EncodedPolyline($data['points']);
442
    }
443
444
    /**
445
     * @param mixed[] $data
446
     *
447
     * @return Fare
448
     */
449
    private function buildFare(array $data)
450
    {
451
        return new Fare($data['value'], $data['currency'], $data['text']);
452
    }
453
454
    /**
455
     * @param mixed[] $data
456
     *
457
     * @return Time
458
     */
459
    private function buildTime(array $data)
460
    {
461
        return new Time(
462
            new \DateTime('@'.$data['value'], new \DateTimeZone($data['time_zone'])),
463
            $data['time_zone'],
464
            $data['text']
465
        );
466
    }
467
}
468