|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the 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 Http\Adapter; |
| 13 | + |
| 14 | +use Http\Adapter\Message\InternalRequestInterface; |
| 15 | +use Http\Adapter\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 | + * Returns the request |
| 34 | + * |
| 35 | + * @return InternalRequestInterface|null |
| 36 | + */ |
| 37 | + public function getRequest() |
| 38 | + { |
| 39 | + return $this->request; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Checks if there is a request |
| 44 | + * |
| 45 | + * @return boolean |
| 46 | + */ |
| 47 | + public function hasRequest() |
| 48 | + { |
| 49 | + return isset($this->request); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Sets the request |
| 54 | + * |
| 55 | + * @param InternalRequestInterface|null $request |
| 56 | + */ |
| 57 | + public function setRequest(InternalRequestInterface $request = null) |
| 58 | + { |
| 59 | + $this->request = $request; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the response |
| 64 | + * |
| 65 | + * @return ResponseInterface|null |
| 66 | + */ |
| 67 | + public function getResponse() |
| 68 | + { |
| 69 | + return $this->response; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Checks if there is a response |
| 74 | + * |
| 75 | + * @return boolean |
| 76 | + */ |
| 77 | + public function hasResponse() |
| 78 | + { |
| 79 | + return isset($this->response); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Sets the response |
| 84 | + * |
| 85 | + * @param ResponseInterface|null $response |
| 86 | + */ |
| 87 | + public function setResponse(ResponseInterface $response = null) |
| 88 | + { |
| 89 | + $this->response = $response; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns a "CANNOT FETCH URI" exception |
| 94 | + * |
| 95 | + * @param string $uri |
| 96 | + * @param string $adapter |
| 97 | + * @param string $error |
| 98 | + * |
| 99 | + * @return self |
| 100 | + */ |
| 101 | + public static function cannotFetchUri($uri, $adapter, $error) |
| 102 | + { |
| 103 | + return new self(sprintf( |
| 104 | + 'An error occurred when fetching the URI "%s" with the adapter "%s" ("%s").', |
| 105 | + $uri, |
| 106 | + $adapter, |
| 107 | + $error |
| 108 | + )); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns a "CANNOT LOAD COOKIE JAR" exception |
| 113 | + * |
| 114 | + * @param string $error |
| 115 | + * |
| 116 | + * @return self |
| 117 | + */ |
| 118 | + public static function cannotLoadCookieJar($error) |
| 119 | + { |
| 120 | + return new self(sprintf('An error occurred when loading the cookie jar ("%s").', $error)); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns a "CANNOT SAVE COOKIE JAR" exception |
| 125 | + * |
| 126 | + * @param string $error |
| 127 | + * |
| 128 | + * @return self |
| 129 | + */ |
| 130 | + public static function cannotSaveCookieJar($error) |
| 131 | + { |
| 132 | + return new self(sprintf('An error occurred when saving the cookie jar ("%s").', $error)); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns a "HTTP ADAPTER DOES NOT EXIST" exception |
| 137 | + * |
| 138 | + * @param string $name |
| 139 | + * |
| 140 | + * @return self |
| 141 | + */ |
| 142 | + public static function httpAdapterDoesNotExist($name) |
| 143 | + { |
| 144 | + return new self(sprintf('The http adapter "%s" does not exist.', $name)); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns a "HTTP ADAPTER IS NOT USABLE" exception |
| 149 | + * |
| 150 | + * @param string $name |
| 151 | + * |
| 152 | + * @return self |
| 153 | + */ |
| 154 | + public static function httpAdapterIsNotUsable($name) |
| 155 | + { |
| 156 | + return new self(sprintf('The http adapter "%s" is not usable.', $name)); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Returns a "HTTP ADAPTERS ARE NOT USABLE" exception |
| 161 | + * |
| 162 | + * @return self |
| 163 | + */ |
| 164 | + public static function httpAdaptersAreNotUsable() |
| 165 | + { |
| 166 | + return new self('No http adapters are usable.'); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns a "HTTP ADAPTER MUST IMPLEMENT INTERFACE" exception |
| 171 | + * |
| 172 | + * @param string $class |
| 173 | + * |
| 174 | + * @return self |
| 175 | + */ |
| 176 | + public static function httpAdapterMustImplementInterface($class) |
| 177 | + { |
| 178 | + return new self(sprintf('The class "%s" must implement "Ivory\HttpAdapter\HttpAdapterInterface".', $class)); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Returns a "DOES NOT SUPPORT SUB ADAPTER" exception |
| 183 | + * |
| 184 | + * @param string $adapter |
| 185 | + * @param string $subAdapter |
| 186 | + * |
| 187 | + * @return self |
| 188 | + */ |
| 189 | + public static function doesNotSupportSubAdapter($adapter, $subAdapter) |
| 190 | + { |
| 191 | + return new self(sprintf('The adapter "%s" does not support the sub-adapter "%s".', $adapter, $subAdapter)); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Returns a "EXTENSION IS NOT LOADED" exception |
| 196 | + * |
| 197 | + * @param string $extension |
| 198 | + * @param string $adapter |
| 199 | + * |
| 200 | + * @return self |
| 201 | + */ |
| 202 | + public static function extensionIsNotLoaded($extension, $adapter) |
| 203 | + { |
| 204 | + return new self(sprintf('The adapter "%s" expects the PHP extension "%s" to be loaded.', $adapter, $extension)); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Returns a "MAX REDIRECTS EXCEEDED" exception |
| 209 | + * |
| 210 | + * @param string $uri |
| 211 | + * @param integer $maxRedirects |
| 212 | + * @param string $adapter |
| 213 | + * |
| 214 | + * @return self |
| 215 | + */ |
| 216 | + public static function maxRedirectsExceeded($uri, $maxRedirects, $adapter) |
| 217 | + { |
| 218 | + return self::cannotFetchUri($uri, $adapter, sprintf('Max redirects exceeded (%d)', $maxRedirects)); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Returns a "REQUEST IS NOT VALID" exception |
| 223 | + * |
| 224 | + * @param mixed $request |
| 225 | + * |
| 226 | + * @return self |
| 227 | + */ |
| 228 | + public static function requestIsNotValid($request) |
| 229 | + { |
| 230 | + return new self(sprintf( |
| 231 | + 'The request must be a string, an array or implement "Psr\Http\Message\RequestInterface" ("%s" given).', |
| 232 | + is_object($request) ? get_class($request) : gettype($request) |
| 233 | + )); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Returns a "STREAM IS NOT VALID" exception |
| 238 | + * |
| 239 | + * @param mixed $stream |
| 240 | + * @param string $wrapper |
| 241 | + * @param string $expected |
| 242 | + * |
| 243 | + * @return self |
| 244 | + */ |
| 245 | + public static function streamIsNotValid($stream, $wrapper, $expected) |
| 246 | + { |
| 247 | + return new self(sprintf( |
| 248 | + 'The stream "%s" only accepts a "%s" (current: "%s")', |
| 249 | + $wrapper, |
| 250 | + $expected, |
| 251 | + is_object($stream) ? get_class($stream) : gettype($stream) |
| 252 | + )); |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Returns a "TIMEOUT EXCEEDED" exception |
| 257 | + * |
| 258 | + * @param string $uri |
| 259 | + * @param float $timeout |
| 260 | + * @param string $adapter |
| 261 | + * |
| 262 | + * @return self |
| 263 | + */ |
| 264 | + public static function timeoutExceeded($uri, $timeout, $adapter) |
| 265 | + { |
| 266 | + return self::cannotFetchUri($uri, $adapter, sprintf('Timeout exceeded (%.2f)', $timeout)); |
| 267 | + } |
| 268 | +} |
0 commit comments