1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\NavigationBundle\Contract\Routing; |
4
|
|
|
|
5
|
|
|
use DH\NavigationBundle\Exception\ResponseException; |
6
|
|
|
use DH\NavigationBundle\Exception\WaypointException; |
7
|
|
|
use DH\NavigationBundle\Provider\ProviderInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractRoutingQuery implements RoutingQueryInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ProviderInterface |
14
|
|
|
*/ |
15
|
|
|
private $provider; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $waypoints; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \DateTime |
24
|
|
|
*/ |
25
|
|
|
private $departure_time; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \DateTime |
29
|
|
|
*/ |
30
|
|
|
private $arrival_time; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $language; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* RoutingQuery constructor. |
39
|
|
|
* |
40
|
|
|
* @param ProviderInterface $provider |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ProviderInterface $provider) |
43
|
|
|
{ |
44
|
|
|
$this->provider = $provider; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getProvider(): ProviderInterface |
48
|
|
|
{ |
49
|
|
|
return $this->provider; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return ?\DateTime |
54
|
|
|
*/ |
55
|
|
|
public function getDepartureTime(): ?\DateTime |
56
|
|
|
{ |
57
|
|
|
return $this->departure_time; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \DateTime $departureTime |
62
|
|
|
* |
63
|
|
|
* @return RoutingQueryInterface |
64
|
|
|
*/ |
65
|
|
|
public function setDepartureTime(\DateTime $departureTime): RoutingQueryInterface |
66
|
|
|
{ |
67
|
|
|
$this->departure_time = $departureTime; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return ?\DateTime |
74
|
|
|
*/ |
75
|
|
|
public function getArrivalTime(): ?\DateTime |
76
|
|
|
{ |
77
|
|
|
return $this->arrival_time; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param \DateTime $arrivalTime |
82
|
|
|
* |
83
|
|
|
* @return RoutingQueryInterface |
84
|
|
|
*/ |
85
|
|
|
public function setArrivalTime(\DateTime $arrivalTime): RoutingQueryInterface |
86
|
|
|
{ |
87
|
|
|
$this->arrival_time = $arrivalTime; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $language |
94
|
|
|
* |
95
|
|
|
* @return RoutingQueryInterface |
96
|
|
|
*/ |
97
|
|
|
public function setLanguage(string $language): RoutingQueryInterface |
98
|
|
|
{ |
99
|
|
|
$this->language = $language; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getLanguage(): string |
108
|
|
|
{ |
109
|
|
|
return $this->language ?? 'en-US'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $waypoint |
114
|
|
|
* |
115
|
|
|
* @return RoutingQueryInterface |
116
|
|
|
*/ |
117
|
|
|
public function addWaypoint($waypoint): RoutingQueryInterface |
118
|
|
|
{ |
119
|
|
|
$this->waypoints[] = $waypoint; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return ?array |
126
|
|
|
*/ |
127
|
|
|
public function getWaypoints(): ?array |
128
|
|
|
{ |
129
|
|
|
return $this->waypoints; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @throws OriginException |
134
|
|
|
* @throws ResponseException |
135
|
|
|
* @throws WaypointException |
136
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
137
|
|
|
* |
138
|
|
|
* @return RoutingResponseInterface |
139
|
|
|
*/ |
140
|
|
|
public function execute(): RoutingResponseInterface |
141
|
|
|
{ |
142
|
|
|
$this->validateRequest(); |
143
|
|
|
$request = $this->buildRequest(); |
144
|
|
|
$rawResponse = $this->request('GET', $request); |
145
|
|
|
$response = $this->buildResponse($rawResponse); |
146
|
|
|
$this->validateResponse($response); |
147
|
|
|
|
148
|
|
|
return $response; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
abstract protected function buildRequest(): string; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param ResponseInterface $response |
158
|
|
|
* |
159
|
|
|
* @return RoutingResponseInterface |
160
|
|
|
*/ |
161
|
|
|
abstract protected function buildResponse(ResponseInterface $response): RoutingResponseInterface; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $method |
165
|
|
|
* @param string $url |
166
|
|
|
* |
167
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
168
|
|
|
* |
169
|
|
|
* @return ResponseInterface |
170
|
|
|
*/ |
171
|
|
|
private function request(string $method, string $url): ResponseInterface |
172
|
|
|
{ |
173
|
|
|
$client = $this->getProvider()->getClient(); |
174
|
|
|
$response = $client->request($method, $url); |
175
|
|
|
|
176
|
|
|
if (200 !== $response->getStatusCode()) { |
177
|
|
|
throw new \Exception('Response with status code '.$response->getStatusCode()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $response; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param DistanceMatrixResponseInterface $response |
|
|
|
|
185
|
|
|
* |
186
|
|
|
* @throws ResponseException |
187
|
|
|
*/ |
188
|
|
|
private function validateResponse(RoutingResponseInterface $response): void |
189
|
|
|
{ |
190
|
|
|
switch ($response->getStatus()) { |
191
|
|
|
case RoutingResponseInterface::RESPONSE_STATUS_OK: |
192
|
|
|
break; |
193
|
|
|
case RoutingResponseInterface::RESPONSE_STATUS_INVALID_REQUEST: |
194
|
|
|
throw new ResponseException('Invalid request.', 1); |
195
|
|
|
|
196
|
|
|
break; |
197
|
|
|
case RoutingResponseInterface::RESPONSE_STATUS_MAX_ELEMENTS_EXCEEDED: |
198
|
|
|
throw new ResponseException('The product of the origin and destination exceeds the limit per request.', 2); |
199
|
|
|
|
200
|
|
|
break; |
201
|
|
|
case RoutingResponseInterface::RESPONSE_STATUS_OVER_QUERY_LIMIT: |
202
|
|
|
throw new ResponseException('The service has received too many requests from your application in the allowed time range.', 3); |
203
|
|
|
|
204
|
|
|
break; |
205
|
|
|
case RoutingResponseInterface::RESPONSE_STATUS_REQUEST_DENIED: |
206
|
|
|
throw new ResponseException('The service denied the use of the Distance Matrix API service by your application.', 4); |
207
|
|
|
|
208
|
|
|
break; |
209
|
|
|
case RoutingResponseInterface::RESPONSE_STATUS_UNKNOWN_ERROR: |
210
|
|
|
throw new ResponseException('Unknown error.', 5); |
211
|
|
|
|
212
|
|
|
break; |
213
|
|
|
default: |
214
|
|
|
throw new ResponseException(sprintf('Unknown status code: %s', $response->getStatus()), 6); |
215
|
|
|
|
216
|
|
|
break; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @throws WaypointException |
222
|
|
|
*/ |
223
|
|
|
private function validateRequest(): void |
224
|
|
|
{ |
225
|
|
|
if (empty($this->getWaypoints()) || \count($this->getWaypoints()) < 2) { |
226
|
|
|
throw new WaypointException('At least two waypoints must be set.'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (null !== $this->getDepartureTime() && null !== $this->getArrivalTime()) { |
230
|
|
|
throw new WaypointException('departure_time and arrival_time cannot be both specified at the same time.'); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths