Passed
Push — master ( 4b5c0b...5361e0 )
by Damien
02:17
created

DistanceMatrixQuery::getLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace DH\NavigationBundle\Provider\GoogleMaps\DistanceMatrix;
4
5
use DH\NavigationBundle\Contract\DistanceMatrix\AbstractDistanceMatrixQuery;
6
use DH\NavigationBundle\Contract\DistanceMatrix\DistanceMatrixResponseInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
class DistanceMatrixQuery extends AbstractDistanceMatrixQuery
10
{
11
    /**
12
     * @var string
13
     */
14
    private $units;
15
16
    /**
17
     * @var string
18
     */
19
    private $mode;
20
21
    /**
22
     * @var string
23
     */
24
    private $avoid;
25
26
    /**
27
     * @var \DateTime
28
     */
29
    private $arrival_time;
30
31
    /**
32
     * @var string
33
     */
34
    private $traffic_model;
35
36
    /**
37
     * @var array
38
     */
39
    private $transit_modes;
40
41
    /**
42
     * @var string
43
     */
44
    private $transit_routing_preference;
45
46
    /**
47
     * URL for API.
48
     */
49
    const ENDPOINT_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json';
50
51
    const MODE_DRIVING = 'driving';
52
    const MODE_WALKING = 'walking';
53
    const MODE_BICYCLING = 'bicycling';
54
    const MODE_TRANSIT = 'transit';
55
56
    const UNITS_METRIC = 'metric';
57
    const UNITS_IMPERIAL = 'imperial';
58
59
    const AVOID_TOLLS = 'tolls';
60
    const AVOID_HIGHWAYS = 'highways';
61
    const AVOID_FERRIES = 'ferries';
62
    const AVOID_INDOOR = 'indoor';
63
64
    const TRAFFIC_MODE_BEST_GUESS = 'best_guess';
65
    const TRAFFIC_MODE_PESSIMISTIC = 'pessimistic';
66
    const TRAFFIC_MODE_OPTIMISTIC = 'optimistic';
67
68
    const TRANSIT_MODE_BUS = 'bus';
69
    const TRANSIT_MODE_SUBWAY = 'subway';
70
    const TRANSIT_MODE_TRAIN = 'train';
71
    const TRANSIT_MODE_TRAM = 'tram';
72
    const TRANSIT_MODE_RAIL = 'rail';
73
74
    const ROUTING_LESS_WALKING = 'less_walking';
75
    const ROUTING_FEWER_TRANSFERS = 'fewer_transfers';
76
77
    /**
78
     * @return string
79
     */
80
    public function getTransitRoutingPreference(): string
81
    {
82
        return $this->transit_routing_preference;
83
    }
84
85
    /**
86
     * @param string $transit_routing_preference
87
     *
88
     * @return DistanceMatrixQuery
89
     */
90
    public function setTransitRoutingPreference(string $transit_routing_preference): self
91
    {
92
        $this->transit_routing_preference = $transit_routing_preference;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    public function getTransitModes(): array
101
    {
102
        return $this->transit_modes;
103
    }
104
105
    /**
106
     * @param array $transit_modes
107
     *
108
     * @return DistanceMatrixQuery
109
     */
110
    public function setTransitModes($transit_modes): self
111
    {
112
        $this->transit_modes = [$transit_modes];
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param $transit_mode
119
     *
120
     * @return DistanceMatrixQuery
121
     */
122
    public function addTransitMode($transit_mode): self
123
    {
124
        $this->transit_modes[] = $transit_mode;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getTrafficModel(): string
133
    {
134
        return $this->traffic_model;
135
    }
136
137
    /**
138
     * @param string $traffic_model
139
     *
140
     * @return DistanceMatrixQuery
141
     */
142
    public function setTrafficModel(string $traffic_model = self::TRAFFIC_MODE_BEST_GUESS): self
143
    {
144
        $this->traffic_model = $traffic_model;
145
146
        return $this;
147
    }
148
149
    /**
150
     * @return \DateTime
151
     */
152
    public function getArrivalTime(): \DateTime
153
    {
154
        return $this->arrival_time;
155
    }
156
157
    /**
158
     * @param \DateTime $arrival_time
159
     *
160
     * @return DistanceMatrixQuery
161
     */
162
    public function setArrivalTime(\DateTime $arrival_time): self
163
    {
164
        $this->arrival_time = $arrival_time;
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param string $units
171
     *
172
     * @return DistanceMatrixQuery
173
     */
174
    public function setUnits($units = self::UNITS_METRIC): self
175
    {
176
        $this->units = $units;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getUnits(): string
185
    {
186
        return $this->units;
187
    }
188
189
    /**
190
     * @param string $mode
191
     *
192
     * @return DistanceMatrixQuery
193
     */
194
    public function setMode($mode = self::MODE_DRIVING): self
195
    {
196
        $this->mode = $mode;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getMode(): string
205
    {
206
        return $this->mode;
207
    }
208
209
    /**
210
     * @param string $avoid (for more values use | as separator)
211
     *
212
     * @return DistanceMatrixQuery
213
     */
214
    public function setAvoid(string $avoid): self
215
    {
216
        $this->avoid = $avoid;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    public function getAvoid(): string
225
    {
226
        return $this->avoid;
227
    }
228
229
    /**
230
     * @see https://developers.google.com/maps/documentation/distance-matrix/intro
231
     *
232
     * @return string
233
     */
234
    protected function buildRequest(): string
235
    {
236
        $data = array_merge(
237
            $this->getProvider()->getCredentials(),
238
            [
239
                'language' => $this->getLanguage(),
240
                'origins' => \count($this->origins) > 1 ? implode('|', $this->origins) : $this->origins[0],
241
                'destinations' => \count($this->destinations) > 1 ? implode('|', $this->destinations) : $this->destinations[0],
242
                'mode' => $this->mode,
243
                'avoid' => $this->avoid,
244
                'units' => $this->units,
245
                'traffic_model' => $this->traffic_model,
246
                'transit_mode' => $this->transit_modes ? implode('|', $this->transit_modes) : ($this->transit_modes[0] ?? null),
247
                'transit_routing_preference' => $this->transit_routing_preference,
248
            ]
249
        );
250
251
        if (null !== $this->arrival_time) {
252
            $data['arrival_time'] = $this->arrival_time
253
                ->format('Y-m-d\TH:i:s')
254
            ;
255
        }
256
257
        if (null !== $this->getDepartureTime()) {
258
            $data['departure_time'] = $this->getDepartureTime()
259
                ->format('Y-m-d\TH:i:s')
260
            ;
261
        }
262
263
        $data = array_filter($data, function ($value) {
264
            return null !== $value;
265
        });
266
267
        $parameters = http_build_query($data);
268
269
        return self::ENDPOINT_URL.'?'.$parameters;
270
    }
271
272
    /**
273
     * @param ResponseInterface $response
274
     *
275
     * @return DistanceMatrixResponseInterface
276
     */
277
    protected function buildResponse(ResponseInterface $response): DistanceMatrixResponseInterface
278
    {
279
        return new DistanceMatrixResponse($response, $this->getOrigins(), $this->getDestinations());
0 ignored issues
show
Unused Code introduced by
The call to DH\NavigationBundle\Prov...Response::__construct() has too many arguments starting with $this->getOrigins(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

279
        return /** @scrutinizer ignore-call */ new DistanceMatrixResponse($response, $this->getOrigins(), $this->getDestinations());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
280
    }
281
}
282