1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Geocoder package. |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Geocoder\Http\Provider; |
14
|
|
|
|
15
|
|
|
use Geocoder\Exception\InvalidCredentials; |
16
|
|
|
use Geocoder\Exception\InvalidServerResponse; |
17
|
|
|
use Geocoder\Exception\QuotaExceeded; |
18
|
|
|
use Geocoder\Provider\AbstractProvider; |
19
|
|
|
use Http\Message\MessageFactory; |
20
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
21
|
|
|
use Http\Client\HttpClient; |
22
|
|
|
use Psr\Http\Message\RequestInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author William Durand <[email protected]> |
26
|
|
|
* @author Tobias Nyholm <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractHttpProvider extends AbstractProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var HttpClient |
32
|
|
|
*/ |
33
|
|
|
private $client; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var MessageFactory |
37
|
|
|
*/ |
38
|
|
|
private $messageFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param HttpClient $client |
42
|
|
|
* @param MessageFactory|null $factory |
43
|
|
|
*/ |
44
|
1 |
|
public function __construct(HttpClient $client, MessageFactory $factory = null) |
45
|
|
|
{ |
46
|
1 |
|
$this->client = $client; |
47
|
1 |
|
$this->messageFactory = $factory ?: MessageFactoryDiscovery::find(); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get URL and return contents. If content is empty, an exception will be thrown. |
52
|
|
|
* |
53
|
|
|
* @param string $url |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
* |
57
|
|
|
* @throws InvalidServerResponse |
58
|
|
|
*/ |
59
|
|
|
protected function getUrlContents(string $url): string |
60
|
|
|
{ |
61
|
|
|
$request = $this->getRequest($url); |
62
|
|
|
|
63
|
|
|
return $this->getParsedResponse($request); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $url |
68
|
|
|
* |
69
|
|
|
* @return RequestInterface |
70
|
|
|
*/ |
71
|
|
|
protected function getRequest(string $url): RequestInterface |
72
|
|
|
{ |
73
|
|
|
return $this->getMessageFactory()->createRequest('GET', $url); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Send request and return contents. If content is empty, an exception will be thrown. |
78
|
|
|
* |
79
|
|
|
* @param RequestInterface $request |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
* |
83
|
|
|
* @throws InvalidServerResponse |
84
|
|
|
*/ |
85
|
|
|
protected function getParsedResponse(RequestInterface $request): string |
86
|
|
|
{ |
87
|
|
|
$response = $this->getHttpClient()->sendRequest($request); |
88
|
|
|
|
89
|
|
|
$statusCode = $response->getStatusCode(); |
90
|
|
|
if (401 === $statusCode || 403 === $statusCode) { |
91
|
|
|
throw new InvalidCredentials(); |
92
|
|
|
} elseif (429 === $statusCode) { |
93
|
|
|
throw new QuotaExceeded(); |
94
|
|
|
} elseif ($statusCode >= 300) { |
95
|
|
|
throw InvalidServerResponse::create((string) $request->getUri(), $statusCode); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$body = (string) $response->getBody(); |
99
|
|
|
if (empty($body)) { |
100
|
|
|
throw InvalidServerResponse::emptyResponse((string) $request->getUri()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $body; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the HTTP adapter. |
108
|
|
|
* |
109
|
|
|
* @return HttpClient |
110
|
|
|
*/ |
111
|
1 |
|
protected function getHttpClient(): HttpClient |
112
|
|
|
{ |
113
|
1 |
|
return $this->client; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return MessageFactory |
118
|
|
|
*/ |
119
|
|
|
protected function getMessageFactory(): MessageFactory |
120
|
|
|
{ |
121
|
|
|
return $this->messageFactory; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|