1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter; |
13
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
15
|
|
|
use Ivory\HttpAdapter\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author GeLo <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class HttpAdapterException extends \Exception |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var InternalRequestInterface|null |
24
|
|
|
*/ |
25
|
|
|
private $request; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ResponseInterface|null |
29
|
|
|
*/ |
30
|
|
|
private $response; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
228 |
|
public function hasRequest() |
36
|
|
|
{ |
37
|
228 |
|
return $this->request !== null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return InternalRequestInterface|null |
42
|
|
|
*/ |
43
|
246 |
|
public function getRequest() |
44
|
|
|
{ |
45
|
246 |
|
return $this->request; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param InternalRequestInterface|null $request |
50
|
|
|
*/ |
51
|
255 |
|
public function setRequest(InternalRequestInterface $request = null) |
52
|
|
|
{ |
53
|
255 |
|
$this->request = $request; |
54
|
255 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
27 |
|
public function hasResponse() |
60
|
|
|
{ |
61
|
27 |
|
return $this->response !== null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return ResponseInterface|null |
66
|
|
|
*/ |
67
|
18 |
|
public function getResponse() |
68
|
|
|
{ |
69
|
18 |
|
return $this->response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ResponseInterface|null $response |
74
|
|
|
*/ |
75
|
182 |
|
public function setResponse(ResponseInterface $response = null) |
76
|
|
|
{ |
77
|
182 |
|
$this->response = $response; |
78
|
182 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $uri |
82
|
|
|
* @param string $adapter |
83
|
|
|
* @param string $error |
84
|
|
|
* |
85
|
|
|
* @return HttpAdapterException |
86
|
|
|
*/ |
87
|
759 |
|
public static function cannotFetchUri($uri, $adapter, $error) |
88
|
|
|
{ |
89
|
759 |
|
return new self(sprintf( |
90
|
759 |
|
'An error occurred when fetching the URI "%s" with the adapter "%s" ("%s").', |
91
|
585 |
|
$uri, |
92
|
585 |
|
$adapter, |
93
|
|
|
$error |
94
|
585 |
|
)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $error |
99
|
|
|
* |
100
|
|
|
* @return HttpAdapterException |
101
|
|
|
*/ |
102
|
9 |
|
public static function cannotLoadCookieJar($error) |
103
|
|
|
{ |
104
|
9 |
|
return new self(sprintf('An error occurred when loading the cookie jar ("%s").', $error)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $error |
109
|
|
|
* |
110
|
|
|
* @return HttpAdapterException |
111
|
|
|
*/ |
112
|
9 |
|
public static function cannotSaveCookieJar($error) |
113
|
|
|
{ |
114
|
9 |
|
return new self(sprintf('An error occurred when saving the cookie jar ("%s").', $error)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
* |
120
|
|
|
* @return HttpAdapterException |
121
|
|
|
*/ |
122
|
9 |
|
public static function httpAdapterDoesNotExist($name) |
123
|
|
|
{ |
124
|
9 |
|
return new self(sprintf('The http adapter "%s" does not exist.', $name)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $name |
129
|
|
|
* |
130
|
|
|
* @return HttpAdapterException |
131
|
|
|
*/ |
132
|
9 |
|
public static function httpAdapterIsNotUsable($name) |
133
|
|
|
{ |
134
|
9 |
|
return new self(sprintf('The http adapter "%s" is not usable.', $name)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return HttpAdapterException |
139
|
|
|
*/ |
140
|
9 |
|
public static function httpAdaptersAreNotUsable() |
141
|
|
|
{ |
142
|
9 |
|
return new self('No http adapters are usable.'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $class |
147
|
|
|
* |
148
|
|
|
* @return HttpAdapterException |
149
|
|
|
*/ |
150
|
9 |
|
public static function httpAdapterMustImplementInterface($class) |
151
|
|
|
{ |
152
|
9 |
|
return new self(sprintf('The class "%s" must implement "Ivory\HttpAdapter\HttpAdapterInterface".', $class)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $adapter |
157
|
|
|
* @param string $subAdapter |
158
|
|
|
* |
159
|
|
|
* @return HttpAdapterException |
160
|
|
|
*/ |
161
|
9 |
|
public static function doesNotSupportSubAdapter($adapter, $subAdapter) |
162
|
|
|
{ |
163
|
9 |
|
return new self(sprintf('The adapter "%s" does not support the sub-adapter "%s".', $adapter, $subAdapter)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $extension |
168
|
|
|
* @param string $adapter |
169
|
|
|
* |
170
|
|
|
* @return HttpAdapterException |
171
|
|
|
*/ |
172
|
4 |
|
public static function extensionIsNotLoaded($extension, $adapter) |
173
|
|
|
{ |
174
|
4 |
|
return new self(sprintf('The adapter "%s" expects the PHP extension "%s" to be loaded.', $adapter, $extension)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $uri |
179
|
|
|
* @param int $maxRedirects |
180
|
|
|
* @param string $adapter |
181
|
|
|
* |
182
|
|
|
* @return HttpAdapterException |
183
|
|
|
*/ |
184
|
27 |
|
public static function maxRedirectsExceeded($uri, $maxRedirects, $adapter) |
185
|
|
|
{ |
186
|
27 |
|
return self::cannotFetchUri($uri, $adapter, sprintf('Max redirects exceeded (%d)', $maxRedirects)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param mixed $request |
191
|
|
|
* |
192
|
|
|
* @return HttpAdapterException |
193
|
|
|
*/ |
194
|
9 |
|
public static function requestIsNotValid($request) |
195
|
|
|
{ |
196
|
9 |
|
return new self(sprintf( |
197
|
9 |
|
'The request must be a string, an array or implement "Psr\Http\Message\RequestInterface" ("%s" given).', |
198
|
9 |
|
is_object($request) ? get_class($request) : gettype($request) |
199
|
7 |
|
)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param mixed $stream |
204
|
|
|
* @param string $wrapper |
205
|
|
|
* @param string $expected |
206
|
|
|
* |
207
|
|
|
* @return HttpAdapterException |
208
|
|
|
*/ |
209
|
|
|
public static function streamIsNotValid($stream, $wrapper, $expected) |
210
|
|
|
{ |
211
|
|
|
return new self(sprintf( |
212
|
|
|
'The stream "%s" only accepts a "%s" (current: "%s")', |
213
|
|
|
$wrapper, |
214
|
|
|
$expected, |
215
|
|
|
is_object($stream) ? get_class($stream) : gettype($stream) |
216
|
|
|
)); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $uri |
221
|
|
|
* @param float $timeout |
222
|
|
|
* @param string $adapter |
223
|
|
|
* |
224
|
|
|
* @return HttpAdapterException |
225
|
|
|
*/ |
226
|
18 |
|
public static function timeoutExceeded($uri, $timeout, $adapter) |
227
|
|
|
{ |
228
|
18 |
|
return self::cannotFetchUri($uri, $adapter, sprintf('Timeout exceeded (%.2f)', $timeout)); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|