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
Pull Request — master (#169)
by Eric
04:50 queued 02:19
created

Direction::buildStep()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 12
nc 1
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\Direction\Request\DirectionRequestInterface;
24
use Ivory\GoogleMap\Service\Direction\Response\DirectionGeocoded;
25
use Ivory\GoogleMap\Service\Direction\Response\DirectionLeg;
26
use Ivory\GoogleMap\Service\Direction\Response\DirectionResponse;
27
use Ivory\GoogleMap\Service\Direction\Response\DirectionRoute;
28
use Ivory\GoogleMap\Service\Direction\Response\DirectionStep;
29
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitAgency;
30
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitDetails;
31
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitLine;
32
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitStop;
33
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitVehicle;
34
35
/**
36
 * @author GeLo <[email protected]>
37
 */
38
class Direction extends AbstractService
39
{
40
    /**
41
     * @param HttpClient     $client
42
     * @param MessageFactory $messageFactory
43
     */
44
    public function __construct(HttpClient $client, MessageFactory $messageFactory)
45
    {
46
        parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/directions');
47
    }
48
49
    /**
50
     * @param DirectionRequestInterface $request
51
     *
52
     * @return DirectionResponse
53
     */
54 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...
55
    {
56
        $response = $this->getClient()->sendRequest($this->createRequest($request->build()));
57
        $data = $this->parse((string) $response->getBody());
58
59
        return $this->buildResponse($data);
60
    }
61
62
    /**
63
     * @param string $data
64
     *
65
     * @return mixed[]
66
     */
67
    private function parse($data)
68
    {
69
        if ($this->getFormat() === self::FORMAT_JSON) {
70
            return json_decode($data, true);
71
        }
72
73
        return $this->getXmlParser()->parse($data, [
74
            'leg'            => 'legs',
75
            'route'          => 'routes',
76
            'step'           => 'steps',
77
            'waypoint_index' => 'waypoint_order',
78
        ]);
79
    }
80
81
    /**
82
     * @param mixed[] $data
83
     *
84
     * @return DirectionResponse
85
     */
86
    private function buildResponse(array $data)
87
    {
88
        $response = new DirectionResponse();
89
        $response->setStatus($data['status']);
90
        $response->setRoutes(isset($data['routes']) ? $this->buildRoutes($data['routes']) : []);
91
92
        $response->setGeocodedWaypoints(
93
            isset($data['geocoded_waypoints']) ? $this->buildGeocodedWaypoints($data['geocoded_waypoints']) : []
94
        );
95
96
        $response->setAvailableTravelModes(
97
            isset($data['available_travel_modes']) ? $data['available_travel_modes'] : []
98
        );
99
100
        return $response;
101
    }
102
103
    /**
104
     * @param mixed[] $data
105
     *
106
     * @return DirectionRoute[]
107
     */
108
    private function buildRoutes(array $data)
109
    {
110
        $routes = [];
111
        foreach ($data as $item) {
112
            $routes[] = $this->buildRoute($item);
113
        }
114
115
        return $routes;
116
    }
117
118
    /**
119
     * @param mixed[] $data
120
     *
121
     * @return DirectionRoute
122
     */
123
    private function buildRoute(array $data)
124
    {
125
        $route = new DirectionRoute();
126
        $route->setBound($this->buildBound($data['bounds']));
127
        $route->setCopyrights(isset($data['copyrights']) ? $data['copyrights'] : null);
128
        $route->setLegs($this->buildLegs($data['legs']));
129
        $route->setOverviewPolyline($this->buildEncodedPolyline($data['overview_polyline']));
130
        $route->setSummary(isset($data['summary']) ? $data['summary'] : null);
131
        $route->setFare(isset($data['fare']) ? $this->buildFare($data['fare']) : null);
132
        $route->setWarnings(isset($data['warnings']) ? $data['warnings'] : []);
133
        $route->setWaypointOrders(isset($data['waypoint_order']) ? $data['waypoint_order'] : []);
134
135
        return $route;
136
    }
137
138
    /**
139
     * @param mixed[] $data
140
     *
141
     * @return DirectionGeocoded[]
142
     */
143
    private function buildGeocodedWaypoints(array $data)
144
    {
145
        $geocodedWaypoints = [];
146
        foreach ($data as $item) {
147
            $geocodedWaypoints[] = $this->buildGeocodedWaypoint($item);
148
        }
149
150
        return $geocodedWaypoints;
151
    }
152
153
    /**
154
     * @param mixed[] $data
155
     *
156
     * @return DirectionGeocoded
157
     */
158
    private function buildGeocodedWaypoint(array $data)
159
    {
160
        $geocodedWaypoint = new DirectionGeocoded();
161
        $geocodedWaypoint->setStatus($data['geocoder_status']);
162
        $geocodedWaypoint->setPartialMatch(isset($data['partial_match']) ? $data['partial_match'] : null);
163
        $geocodedWaypoint->setPlaceId(isset($data['place_id']) ? $data['place_id'] : null);
164
        $geocodedWaypoint->setTypes($data['types']);
165
166
        return $geocodedWaypoint;
167
    }
168
169
    /**
170
     * @param mixed[] $data
171
     *
172
     * @return DirectionLeg[]
173
     */
174
    private function buildLegs(array $data)
175
    {
176
        $legs = [];
177
        foreach ($data as $item) {
178
            $legs[] = $this->buildLeg($item);
179
        }
180
181
        return $legs;
182
    }
183
184
    /**
185
     * @param mixed[] $data
186
     *
187
     * @return DirectionLeg
188
     */
189
    private function buildLeg(array $data)
190
    {
191
        $leg = new DirectionLeg();
192
        $leg->setDistance($this->buildDistance($data['distance']));
193
        $leg->setDuration($this->buildDuration($data['duration']));
194
        $leg->setDepartureTime(isset($data['departure_time']) ? $this->buildDateTime($data['departure_time']) : null);
195
        $leg->setArrivalTime(isset($data['arrival_time']) ? $this->buildDateTime($data['arrival_time']) : null);
196
        $leg->setEndAddress($data['end_address']);
197
        $leg->setEndLocation($this->buildCoordinate($data['end_location']));
198
        $leg->setStartAddress($data['start_address']);
199
        $leg->setStartLocation($this->buildCoordinate($data['start_location']));
200
        $leg->setSteps($this->buildSteps($data['steps']));
201
        $leg->setViaWaypoints(isset($data['via_waypoint']) ? $data['via_waypoint'] : []);
202
        $leg->setDurationInTraffic(
203
            isset($data['duration_in_traffic']) ? $this->buildDuration($data['duration_in_traffic']) : null
204
        );
205
206
        return $leg;
207
    }
208
209
    /**
210
     * @codeCoverageIgnore
211
     *
212
     * @param mixed[] $data
213
     *
214
     * @return Fare
215
     */
216 View Code Duplication
    private function buildFare(array $data)
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...
217
    {
218
        $fare = new Fare();
219
        $fare->setCurrency($data['currency']);
220
        $fare->setValue($data['value']);
221
        $fare->setText($data['text']);
222
223
        return $fare;
224
    }
225
226
    /**
227
     * @param mixed[] $data
228
     *
229
     * @return DirectionStep[]
230
     */
231
    private function buildSteps(array $data)
232
    {
233
        $steps = [];
234
        foreach ($data as $item) {
235
            $steps[] = $this->buildStep($item);
236
        }
237
238
        return $steps;
239
    }
240
241
    /**
242
     * @param mixed[] $data
243
     *
244
     * @return DirectionStep
245
     */
246
    private function buildStep(array $data)
247
    {
248
        $step = new DirectionStep();
249
        $step->setDistance($this->buildDistance($data['distance']));
250
        $step->setDuration($this->buildDuration($data['duration']));
251
        $step->setEndLocation($this->buildCoordinate($data['end_location']));
252
        $step->setInstructions($data['html_instructions']);
253
        $step->setEncodedPolyline($this->buildEncodedPolyline($data['polyline']));
254
        $step->setStartLocation($this->buildCoordinate($data['start_location']));
255
        $step->setTravelMode($data['travel_mode']);
256
        $step->setTransitDetails(
257
            isset($data['transit_details']) ? $this->buildTransitDetails($data['transit_details']) : null
258
        );
259
260
        return $step;
261
    }
262
263
    /**
264
     * @param mixed[] $data
265
     *
266
     * @return DirectionTransitDetails
267
     */
268
    private function buildTransitDetails(array $data)
269
    {
270
        $transitDetails = new DirectionTransitDetails();
271
        $transitDetails->setDepartureStop($this->buildTransitStop($data['departure_stop']));
272
        $transitDetails->setArrivalStop($this->buildTransitStop($data['arrival_stop']));
273
        $transitDetails->setDepartureTime($this->buildDateTime($data['departure_time']));
274
        $transitDetails->setArrivalTime($this->buildDateTime($data['arrival_time']));
275
        $transitDetails->setHeadSign($data['headsign']);
276
        $transitDetails->setHeadWay(isset($data['headway']) ? $data['headway'] : null);
277
        $transitDetails->setLine($this->buildTransitLine($data['line']));
278
        $transitDetails->setNumStops($data['num_stops']);
279
280
        return $transitDetails;
281
    }
282
283
    /**
284
     * @param mixed[] $data
285
     *
286
     * @return DirectionTransitLine
287
     */
288
    private function buildTransitLine(array $data)
289
    {
290
        $transitLine = new DirectionTransitLine();
291
        $transitLine->setName($data['name']);
292
        $transitLine->setShortName($data['short_name']);
293
        $transitLine->setColor(isset($data['color']) ? $data['color'] : null);
294
        $transitLine->setUrl(isset($data['url']) ? $data['url'] : null);
295
        $transitLine->setIcon(isset($data['icon']) ? $data['icon'] : null);
296
        $transitLine->setTextColor(isset($data['text_color']) ? $data['text_color'] : null);
297
        $transitLine->setVehicle($this->buildTransitVehicle($data['vehicle']));
298
        $transitLine->setAgencies($this->buildTransitAgencies($data['agencies']));
299
300
        return $transitLine;
301
    }
302
303
    /**
304
     * @param mixed[] $data
305
     *
306
     * @return DirectionTransitAgency[]
307
     */
308
    private function buildTransitAgencies(array $data)
309
    {
310
        $transitAgencies = [];
311
312
        foreach ($data as $item) {
313
            $transitAgencies[] = $this->buildTransitAgency($item);
314
        }
315
316
        return $transitAgencies;
317
    }
318
319
    /**
320
     * @param mixed[] $data
321
     *
322
     * @return DirectionTransitAgency
323
     */
324
    private function buildTransitAgency(array $data)
325
    {
326
        $transitAgency = new DirectionTransitAgency();
327
        $transitAgency->setName($data['name']);
328
        $transitAgency->setPhone($data['phone']);
329
        $transitAgency->setUrl($data['url']);
330
331
        return $transitAgency;
332
    }
333
334
    /**
335
     * @param mixed[] $data
336
     *
337
     * @return DirectionTransitStop
338
     */
339
    private function buildTransitStop(array $data)
340
    {
341
        $transitStop = new DirectionTransitStop();
342
        $transitStop->setName($data['name']);
343
        $transitStop->setLocation($this->buildCoordinate($data['location']));
344
345
        return $transitStop;
346
    }
347
348
    /**
349
     * @param mixed[] $data
350
     *
351
     * @return DirectionTransitVehicle
352
     */
353
    private function buildTransitVehicle(array $data)
354
    {
355
        $transitVehicle = new DirectionTransitVehicle();
356
        $transitVehicle->setName($data['name']);
357
        $transitVehicle->setIcon($data['icon']);
358
        $transitVehicle->setType($data['type']);
359
360
        return $transitVehicle;
361
    }
362
363
    /**
364
     * @param mixed[] $data
365
     *
366
     * @return \DateTime
367
     */
368
    private function buildDateTime(array $data)
369
    {
370
        return new \DateTime('@'.$data['value'], new \DateTimeZone($data['time_zone']));
371
    }
372
373
    /**
374
     * @param mixed[] $data
375
     *
376
     * @return Bound
377
     */
378
    private function buildBound(array $data)
379
    {
380
        return new Bound(
381
            $this->buildCoordinate($data['southwest']),
382
            $this->buildCoordinate($data['northeast'])
383
        );
384
    }
385
386
    /**
387
     * @param mixed[] $data
388
     *
389
     * @return Coordinate
390
     */
391
    private function buildCoordinate(array $data)
392
    {
393
        return new Coordinate($data['lat'], $data['lng']);
394
    }
395
396
    /**
397
     * @param mixed[] $data
398
     *
399
     * @return Distance
400
     */
401
    private function buildDistance(array $data)
402
    {
403
        return new Distance($data['text'], $data['value']);
404
    }
405
406
    /**
407
     * @param mixed[] $data
408
     *
409
     * @return Duration
410
     */
411
    private function buildDuration(array $data)
412
    {
413
        return new Duration($data['text'], $data['value']);
414
    }
415
416
    /**
417
     * @param string[] $data
418
     *
419
     * @return EncodedPolyline
420
     */
421
    private function buildEncodedPolyline(array $data)
422
    {
423
        return new EncodedPolyline($data['points']);
424
    }
425
}
426