Add helper method for inaccessible urls
authorjoshuaruesweg <ruesweg@woltlab.com>
Thu, 4 Mar 2021 18:52:40 +0000 (19:52 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 16 Mar 2021 15:19:13 +0000 (16:19 +0100)
wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php

index a038713c3bdd7c2cf03160b26353e5a59657b466..fc0bd6bedc0f24c302729687db1edde4e0c50229 100644 (file)
@@ -88,20 +88,12 @@ final class UnfurlResponse
         } 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);
@@ -310,26 +302,34 @@ final class UnfurlResponse
         } 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.