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-fare ( 5be71a )
by Eric
02:21
created

Directions::buildFare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
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
        return $response;
81
    }
82
83
    /**
84
     * @param mixed[] $data
85
     *
86
     * @return DirectionsRoute[]
87
     */
88
    private function buildRoutes(array $data)
89
    {
90
        $routes = [];
91
        foreach ($data as $item) {
92
            $routes[] = $this->buildRoute($item);
93
        }
94
95
        return $routes;
96
    }
97
98
    /**
99
     * @param mixed[] $data
100
     *
101
     * @return DirectionsRoute
102
     */
103
    private function buildRoute(array $data)
104
    {
105
        $route = new DirectionsRoute();
106
        $route->setCopyrights(isset($data['copyrights']) ? $data['copyrights'] : null);
107
        $route->setLegs($this->buildLegs($data['legs']));
108
        $route->setOverviewPolyline(new EncodedPolyline($data['overview_polyline']['points']));
109
        $route->setSummary(isset($data['summary']) ? $data['summary'] : null);
110
        $route->setFare(isset($data['fare']) ? $this->buildFare($data['fare']) : null);
111
        $route->setWarnings(isset($data['warnings']) ? $data['warnings'] : []);
112
        $route->setWaypointOrders(isset($data['waypoint_order']) ? $data['waypoint_order'] : []);
113
114
        $route->setBound(new Bound(
115
            new Coordinate(
116
                $data['bounds']['southwest']['lat'],
117
                $data['bounds']['southwest']['lng']
118
            ),
119
            new Coordinate(
120
                $data['bounds']['northeast']['lat'],
121
                $data['bounds']['northeast']['lng']
122
            )
123
        ));
124
125
        return $route;
126
    }
127
128
    /**
129
     * @param mixed[] $data
130
     *
131
     * @return DirectionsLeg[]
132
     */
133
    private function buildLegs(array $data)
134
    {
135
        $legs = [];
136
        foreach ($data as $item) {
137
            $legs[] = $this->buildLeg($item);
138
        }
139
140
        return $legs;
141
    }
142
143
    /**
144
     * @param mixed[] $data
145
     *
146
     * @return DirectionsLeg
147
     */
148
    private function buildLeg(array $data)
149
    {
150
        $leg = new DirectionsLeg();
151
        $leg->setDistance(new Distance($data['distance']['text'], $data['distance']['value']));
152
        $leg->setDuration(new Duration($data['duration']['text'], $data['duration']['value']));
153
        $leg->setEndAddress($data['end_address']);
154
        $leg->setEndLocation(new Coordinate($data['end_location']['lat'], $data['end_location']['lng']));
155
        $leg->setStartAddress($data['start_address']);
156
        $leg->setStartLocation(new Coordinate($data['start_location']['lat'], $data['start_location']['lng']));
157
        $leg->setSteps($this->buildSteps($data['steps']));
158
        $leg->setViaWaypoints(isset($data['via_waypoint']) ? $data['via_waypoint'] : []);
159
160
        return $leg;
161
    }
162
163
    /**
164
     * @param mixed[] $data
165
     *
166
     * @return DirectionsFare
167
     */
168
    private function buildFare(array $data)
169
    {
170
        $fare = new DirectionsFare();
171
        $fare->setCurrency($data['currency']);
172
        $fare->setValue($data['value']);
173
        $fare->setText(['text']);
0 ignored issues
show
Documentation introduced by
array('text') is of type array<integer,string,{"0":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
174
175
        return $fare;
176
    }
177
178
    /**
179
     * @param mixed[] $data
180
     *
181
     * @return DirectionsStep[]
182
     */
183
    private function buildSteps(array $data)
184
    {
185
        $steps = [];
186
        foreach ($data as $item) {
187
            $steps[] = $this->buildStep($item);
188
        }
189
190
        return $steps;
191
    }
192
193
    /**
194
     * @param mixed[] $data
195
     *
196
     * @return DirectionsStep
197
     */
198
    private function buildStep(array $data)
199
    {
200
        $step = new DirectionsStep();
201
        $step->setDistance(new Distance($data['distance']['text'], $data['distance']['value']));
202
        $step->setDuration(new Duration($data['duration']['text'], $data['duration']['value']));
203
        $step->setEndLocation(new Coordinate($data['end_location']['lat'], $data['end_location']['lng']));
204
        $step->setInstructions($data['html_instructions']);
205
        $step->setEncodedPolyline(new EncodedPolyline($data['polyline']['points']));
206
        $step->setStartLocation(new Coordinate($data['start_location']['lat'], $data['start_location']['lng']));
207
        $step->setTravelMode($data['travel_mode']);
208
209
        return $step;
210
    }
211
}
212