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 — directions-departure-arrival-t... ( 2089f8 )
by Eric
03:08
created

Directions::buildStep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 1
eloc 10
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\Directions;
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
23
/**
24
 * @author GeLo <[email protected]>
25
 */
26
class Directions extends AbstractService
27
{
28
    /**
29
     * @param HttpClient     $client
30
     * @param MessageFactory $messageFactory
31
     */
32
    public function __construct(HttpClient $client, MessageFactory $messageFactory)
33
    {
34
        parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/directions');
35
    }
36
37
    /**
38
     * @param DirectionsRequest $request
39
     *
40
     * @return DirectionsResponse
41
     */
42 View Code Duplication
    public function route(DirectionsRequest $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...
43
    {
44
        $response = $this->getClient()->sendRequest($this->createRequest($request->buildQuery()));
45
        $data = $this->parse((string) $response->getBody());
46
47
        return $this->buildResponse($data);
48
    }
49
50
    /**
51
     * @param string $data
52
     *
53
     * @return mixed[]
54
     */
55
    private function parse($data)
56
    {
57
        if ($this->getFormat() === self::FORMAT_JSON) {
58
            return json_decode($data, true);
59
        }
60
61
        return $this->getXmlParser()->parse($data, [
62
            'leg'            => 'legs',
63
            'route'          => 'routes',
64
            'step'           => 'steps',
65
            'waypoint_index' => 'waypoint_order',
66
        ]);
67
    }
68
69
    /**
70
     * @param mixed[] $data
71
     *
72
     * @return DirectionsResponse
73
     */
74
    private function buildResponse(array $data)
75
    {
76
        $response = new DirectionsResponse();
77
        $response->setStatus($data['status']);
78
        $response->setRoutes(isset($data['routes']) ? $this->buildRoutes($data['routes']) : []);
79
80
        $response->setGeocodedWaypoints(
81
            isset($data['geocoded_waypoints']) ? $this->buildGeocodedWaypoints($data['geocoded_waypoints']) : []
82
        );
83
84
        $response->setAvailableTravelModes(
85
            isset($data['available_travel_modes']) ? $data['available_travel_modes'] : []
86
        );
87
88
        return $response;
89
    }
90
91
    /**
92
     * @param mixed[] $data
93
     *
94
     * @return DirectionsRoute[]
95
     */
96
    private function buildRoutes(array $data)
97
    {
98
        $routes = [];
99
        foreach ($data as $item) {
100
            $routes[] = $this->buildRoute($item);
101
        }
102
103
        return $routes;
104
    }
105
106
    /**
107
     * @param mixed[] $data
108
     *
109
     * @return DirectionsRoute
110
     */
111
    private function buildRoute(array $data)
112
    {
113
        $route = new DirectionsRoute();
114
        $route->setBound($this->buildBound($data['bounds']));
115
        $route->setCopyrights(isset($data['copyrights']) ? $data['copyrights'] : null);
116
        $route->setLegs($this->buildLegs($data['legs']));
117
        $route->setOverviewPolyline($this->buildEncodedPolyline($data['overview_polyline']));
118
        $route->setSummary(isset($data['summary']) ? $data['summary'] : null);
119
        $route->setFare(isset($data['fare']) ? $this->buildFare($data['fare']) : null);
120
        $route->setWarnings(isset($data['warnings']) ? $data['warnings'] : []);
121
        $route->setWaypointOrders(isset($data['waypoint_order']) ? $data['waypoint_order'] : []);
122
123
        return $route;
124
    }
125
126
    /**
127
     * @param mixed[] $data
128
     *
129
     * @return DirectionsGeocoded[]
130
     */
131
    private function buildGeocodedWaypoints(array $data)
132
    {
133
        $geocodedWaypoints = [];
134
        foreach ($data as $item) {
135
            $geocodedWaypoints[] = $this->buildGeocodedWaypoint($item);
136
        }
137
138
        return $geocodedWaypoints;
139
    }
140
141
    /**
142
     * @param mixed[] $data
143
     *
144
     * @return DirectionsGeocoded
145
     */
146
    private function buildGeocodedWaypoint(array $data)
147
    {
148
        $geocodedWaypoint = new DirectionsGeocoded();
149
        $geocodedWaypoint->setStatus($data['geocoder_status']);
150
        $geocodedWaypoint->setPartialMatch(isset($data['partial_match']) ? $data['partial_match'] : null);
151
        $geocodedWaypoint->setPlaceId(isset($data['place_id']) ? $data['place_id'] : null);
152
        $geocodedWaypoint->setTypes($data['types']);
153
154
        return $geocodedWaypoint;
155
    }
156
157
    /**
158
     * @param mixed[] $data
159
     *
160
     * @return DirectionsLeg[]
161
     */
162
    private function buildLegs(array $data)
163
    {
164
        $legs = [];
165
        foreach ($data as $item) {
166
            $legs[] = $this->buildLeg($item);
167
        }
168
169
        return $legs;
170
    }
171
172
    /**
173
     * @param mixed[] $data
174
     *
175
     * @return DirectionsLeg
176
     */
177
    private function buildLeg(array $data)
178
    {
179
        $leg = new DirectionsLeg();
180
        $leg->setDistance($this->buildDistance($data['distance']));
181
        $leg->setDuration($this->buildDuration($data['duration']));
182
        $leg->setDepartureTime(isset($data['departure_time']) ? $this->createDateTime($data['departure_time']) : null);
183
        $leg->setArrivalTime(isset($data['arrival_time']) ? $this->createDateTime($data['arrival_time']) : null);
184
        $leg->setEndAddress($data['end_address']);
185
        $leg->setEndLocation($this->buildCoordinate($data['end_location']));
186
        $leg->setStartAddress($data['start_address']);
187
        $leg->setStartLocation($this->buildCoordinate($data['start_location']));
188
        $leg->setSteps($this->buildSteps($data['steps']));
189
        $leg->setViaWaypoints(isset($data['via_waypoint']) ? $data['via_waypoint'] : []);
190
        $leg->setDurationInTraffic(
191
            isset($data['duration_in_traffic']) ? $this->buildDuration($data['duration_in_traffic']) : null
192
        );
193
194
        return $leg;
195
    }
196
197
    /**
198
     * @codeCoverageIgnore
199
     *
200
     * @param mixed[] $data
201
     *
202
     * @return DirectionsFare
203
     */
204
    private function buildFare(array $data)
205
    {
206
        $fare = new DirectionsFare();
207
        $fare->setCurrency($data['currency']);
208
        $fare->setValue($data['value']);
209
        $fare->setText($data['text']);
210
211
        return $fare;
212
    }
213
214
    /**
215
     * @param mixed[] $data
216
     *
217
     * @return DirectionsStep[]
218
     */
219
    private function buildSteps(array $data)
220
    {
221
        $steps = [];
222
        foreach ($data as $item) {
223
            $steps[] = $this->buildStep($item);
224
        }
225
226
        return $steps;
227
    }
228
229
    /**
230
     * @param mixed[] $data
231
     *
232
     * @return DirectionsStep
233
     */
234
    private function buildStep(array $data)
235
    {
236
        $step = new DirectionsStep();
237
        $step->setDistance($this->buildDistance($data['distance']));
238
        $step->setDuration($this->buildDuration($data['duration']));
239
        $step->setEndLocation($this->buildCoordinate($data['end_location']));
240
        $step->setInstructions($data['html_instructions']);
241
        $step->setEncodedPolyline($this->buildEncodedPolyline($data['polyline']));
242
        $step->setStartLocation($this->buildCoordinate($data['start_location']));
243
        $step->setTravelMode($data['travel_mode']);
244
245
        return $step;
246
    }
247
248
    /**
249
     * @param mixed[] $data
250
     *
251
     * @return \DateTime
252
     */
253
    private function createDateTime(array $data)
254
    {
255
        return new \DateTime($data['value'], new \DateTimeZone($data['time_zone']));
256
    }
257
258
    /**
259
     * @param mixed[] $data
260
     *
261
     * @return Bound
262
     */
263
    private function buildBound(array $data)
264
    {
265
        return new Bound(
266
            $this->buildCoordinate($data['southwest']),
267
            $this->buildCoordinate($data['northeast'])
268
        );
269
    }
270
271
    /**
272
     * @param mixed[] $data
273
     *
274
     * @return Coordinate
275
     */
276
    private function buildCoordinate(array $data)
277
    {
278
        return new Coordinate($data['lat'], $data['lng']);
279
    }
280
281
    /**
282
     * @param mixed[] $data
283
     *
284
     * @return Distance
285
     */
286
    private function buildDistance(array $data)
287
    {
288
        return new Distance($data['text'], $data['value']);
289
    }
290
291
    /**
292
     * @param mixed[] $data
293
     *
294
     * @return Duration
295
     */
296
    private function buildDuration(array $data)
297
    {
298
        return new Duration($data['text'], $data['value']);
299
    }
300
301
    /**
302
     * @param string[] $data
303
     *
304
     * @return EncodedPolyline
305
     */
306
    private function buildEncodedPolyline(array $data)
307
    {
308
        return new EncodedPolyline($data['points']);
309
    }
310
}
311