DistanceMatrixQuery::getMode()   A
last analyzed

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 DateTime;
6
use DH\NavigationBundle\Contract\DistanceMatrix\AbstractDistanceMatrixQuery;
7
use DH\NavigationBundle\Contract\DistanceMatrix\DistanceMatrixResponseInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
class DistanceMatrixQuery extends AbstractDistanceMatrixQuery
11
{
12
    /**
13
     * @var string
14
     */
15
    private $units;
16
17
    /**
18
     * @var string
19
     */
20
    private $mode;
21
22
    /**
23
     * @var string
24
     */
25
    private $avoid;
26
27
    /**
28
     * @var DateTime
29
     */
30
    private $arrival_time;
31
32
    /**
33
     * @var string
34
     */
35
    private $traffic_model;
36
37
    /**
38
     * @var array
39
     */
40
    private $transit_modes;
41
42
    /**
43
     * @var string
44
     */
45
    private $transit_routing_preference;
46
47
    /**
48
     * URL for API.
49
     */
50
    public const ENDPOINT_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json';
51
52
    public const MODE_DRIVING = 'driving';
53
    public const MODE_WALKING = 'walking';
54
    public const MODE_BICYCLING = 'bicycling';
55
    public const MODE_TRANSIT = 'transit';
56
57
    public const UNITS_METRIC = 'metric';
58
    public const UNITS_IMPERIAL = 'imperial';
59
60
    public const AVOID_TOLLS = 'tolls';
61
    public const AVOID_HIGHWAYS = 'highways';
62
    public const AVOID_FERRIES = 'ferries';
63
    public const AVOID_INDOOR = 'indoor';
64
65
    public const TRAFFIC_MODE_BEST_GUESS = 'best_guess';
66
    public const TRAFFIC_MODE_PESSIMISTIC = 'pessimistic';
67
    public const TRAFFIC_MODE_OPTIMISTIC = 'optimistic';
68
69
    public const TRANSIT_MODE_BUS = 'bus';
70
    public const TRANSIT_MODE_SUBWAY = 'subway';
71
    public const TRANSIT_MODE_TRAIN = 'train';
72
    public const TRANSIT_MODE_TRAM = 'tram';
73
    public const TRANSIT_MODE_RAIL = 'rail';
74
75
    public const ROUTING_LESS_WALKING = 'less_walking';
76
    public const ROUTING_FEWER_TRANSFERS = 'fewer_transfers';
77
78
    public function getTransitRoutingPreference(): string
79
    {
80
        return $this->transit_routing_preference;
81
    }
82
83
    /**
84
     * @return DistanceMatrixQuery
85
     */
86
    public function setTransitRoutingPreference(string $transit_routing_preference): self
87
    {
88
        $this->transit_routing_preference = $transit_routing_preference;
89
90
        return $this;
91
    }
92
93
    public function getTransitModes(): array
94
    {
95
        return $this->transit_modes;
96
    }
97
98
    /**
99
     * @param array $transit_modes
100
     *
101
     * @return DistanceMatrixQuery
102
     */
103
    public function setTransitModes($transit_modes): self
104
    {
105
        $this->transit_modes = [$transit_modes];
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param $transit_mode
112
     *
113
     * @return DistanceMatrixQuery
114
     */
115
    public function addTransitMode($transit_mode): self
116
    {
117
        $this->transit_modes[] = $transit_mode;
118
119
        return $this;
120
    }
121
122
    public function getTrafficModel(): string
123
    {
124
        return $this->traffic_model;
125
    }
126
127
    /**
128
     * @return DistanceMatrixQuery
129
     */
130
    public function setTrafficModel(string $traffic_model = self::TRAFFIC_MODE_BEST_GUESS): self
131
    {
132
        $this->traffic_model = $traffic_model;
133
134
        return $this;
135
    }
136
137
    public function getArrivalTime(): DateTime
138
    {
139
        return $this->arrival_time;
140
    }
141
142
    /**
143
     * @return DistanceMatrixQuery
144
     */
145
    public function setArrivalTime(DateTime $arrival_time): self
146
    {
147
        $this->arrival_time = $arrival_time;
148
149
        return $this;
150
    }
151
152
    /**
153
     * @param string $units
154
     *
155
     * @return DistanceMatrixQuery
156
     */
157
    public function setUnits($units = self::UNITS_METRIC): self
158
    {
159
        $this->units = $units;
160
161
        return $this;
162
    }
163
164
    public function getUnits(): string
165
    {
166
        return $this->units;
167
    }
168
169
    /**
170
     * @param string $mode
171
     *
172
     * @return DistanceMatrixQuery
173
     */
174
    public function setMode($mode = self::MODE_DRIVING): self
175
    {
176
        $this->mode = $mode;
177
178
        return $this;
179
    }
180
181
    public function getMode(): string
182
    {
183
        return $this->mode;
184
    }
185
186
    /**
187
     * @param string $avoid (for more values use | as separator)
188
     *
189
     * @return DistanceMatrixQuery
190
     */
191
    public function setAvoid(string $avoid): self
192
    {
193
        $this->avoid = $avoid;
194
195
        return $this;
196
    }
197
198
    public function getAvoid(): string
199
    {
200
        return $this->avoid;
201
    }
202
203
    /**
204
     * @see https://developers.google.com/maps/documentation/distance-matrix/intro
205
     *
206
     * {@inheritdoc}
207
     */
208
    protected function buildRequest(): string
209
    {
210
        $data = array_merge(
211
            $this->getProvider()->getCredentials(),
212
            [
213
                'region' => $this->getProvider()->getRegion(),
0 ignored issues
show
Bug introduced by
The method getRegion() does not exist on DH\NavigationBundle\Provider\ProviderInterface. It seems like you code against a sub-type of DH\NavigationBundle\Provider\ProviderInterface such as DH\NavigationBundle\Provider\GoogleMaps\GoogleMaps. ( Ignorable by Annotation )

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

213
                'region' => $this->getProvider()->/** @scrutinizer ignore-call */ getRegion(),
Loading history...
214
                'language' => $this->getLanguage(),
215
                'origins' => \count($this->origins) > 1 ? implode('|', $this->origins) : $this->origins[0],
216
                'destinations' => \count($this->destinations) > 1 ? implode('|', $this->destinations) : $this->destinations[0],
217
                'mode' => $this->mode,
218
                'avoid' => $this->avoid,
219
                'units' => $this->units,
220
                'traffic_model' => $this->traffic_model,
221
                'transit_mode' => $this->transit_modes ? implode('|', $this->transit_modes) : ($this->transit_modes[0] ?? null),
222
                'transit_routing_preference' => $this->transit_routing_preference,
223
            ]
224
        );
225
226
        if (null !== $this->arrival_time) {
227
            $data['arrival_time'] = $this->arrival_time->getTimestamp();
228
        }
229
230
        if (null !== $this->getDepartureTime()) {
231
            $data['departure_time'] = $this->getDepartureTime()->getTimestamp();
232
        }
233
234
        $data = array_filter($data, static function ($value) {
235
            return null !== $value;
236
        });
237
238
        $parameters = http_build_query($data);
239
240
        return self::ENDPOINT_URL.'?'.$parameters;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246
    protected function buildResponse(ResponseInterface $response): DistanceMatrixResponseInterface
247
    {
248
        return new DistanceMatrixResponse($response);
249
    }
250
}
251