} catch (BadResponseException $e) {
$response = $e->getResponse();
- switch ($response->getStatusCode()) {
- case 400: // Bad Request
- case 401: // Unauthorized
- case 402: // Payment Required
- case 403: // Forbidden
- case 404: // Not Found
- case 406: // Not Acceptable
- $message = "Request failed with status code {$response->getStatusCode()}.";
-
- throw new UrlInaccessible($message, $response->getStatusCode(), $e);
- break;
-
- default:
- throw new DownloadFailed("Could not download content.", $response->getStatusCode(), $e);
+ if (self::isUrlInaccessible($response)) {
+ $message = "Request failed with status code {$response->getStatusCode()}.";
+
+ throw new UrlInaccessible($message, $response->getStatusCode(), $e);
+ } else {
+ throw new DownloadFailed("Could not download content.", $response->getStatusCode(), $e);
}
} catch (ClientExceptionInterface $e) {
throw new DownloadFailed("Could not download content.", 0, $e);
} catch (BadResponseException $e) {
$response = $e->getResponse();
- switch ($response->getStatusCode()) {
- case 400: // Bad Request
- case 401: // Unauthorized
- case 402: // Payment Required
- case 403: // Forbidden
- case 404: // Not Found
- case 406: // Not Acceptable
- $message = "Request failed with status code {$response->getStatusCode()}.";
-
- throw new UrlInaccessible($message, $response->getStatusCode(), $e);
- break;
+ if (self::isUrlInaccessible($response)) {
+ $message = "Request failed with status code {$response->getStatusCode()}.";
- default:
- throw new DownloadFailed("Could not download content.", $response->getStatusCode(), $e);
+ throw new UrlInaccessible($message, $response->getStatusCode(), $e);
+ } else {
+ throw new DownloadFailed("Could not download content.", $response->getStatusCode(), $e);
}
} catch (ClientExceptionInterface $e) {
throw new DownloadFailed("Could not download content.", 0, $e);
}
}
+ private static function isUrlInaccessible(Response $response): bool
+ {
+ switch ($response->getStatusCode()) {
+ case 400: // Bad Request
+ case 401: // Unauthorized
+ case 402: // Payment Required
+ case 403: // Forbidden
+ case 404: // Not Found
+ case 406: // Not Acceptable
+ return true;
+ break;
+ }
+
+ return false;
+ }
+
/**
* Returns a "static" instance of the HTTP client to use to allow
* for TCP connection reuse.