Passed
Push — master ( 7ec869...8ef4a4 )
by Damien
01:57
created

DistanceMatrixQuery::addTransitMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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