PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / ai / http-request / application / response-validator.php

response-validator.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/ai/http-request/application/response-validator.php

78 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4
5 namespace Yoast\WP\SEO\AI\HTTP_Request\Application;
6
7 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Bad_Request_Exception;
8 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Forbidden_Exception;
9 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Internal_Server_Error_Exception;
10 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Not_Found_Exception;
11 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Payment_Required_Exception;
12 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Request_Timeout_Exception;
13 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Service_Unavailable_Exception;
14 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception;
15 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Unauthorized_Exception;
16 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Response;
17
18 /**
19 * Maps a parsed Response to the matching typed exception for non-200 responses.
20 *
21 * Centralises the status-code switch used by every auth strategy after dispatch, so the OAuth
22 * (MyYoast_Client-backed) and Token (legacy JWT) paths translate yoast-ai responses identically.
23 *
24 * @makePublic
25 */
26 class Response_Validator {
27
28 // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- The @throws list documents the exception family produced by this method.
29
30 /**
31 * Returns the response on success (HTTP 200); throws the matching typed exception otherwise.
32 *
33 * @param Response $response The parsed response.
34 *
35 * @return Response The same response, on HTTP 200.
36 *
37 * @throws Bad_Request_Exception When the response code is anything other than the codes below (including transport failure, where code is 0).
38 * @throws Forbidden_Exception When the response code is 403.
39 * @throws Internal_Server_Error_Exception When the response code is 500.
40 * @throws Not_Found_Exception When the response code is 404.
41 * @throws Payment_Required_Exception When the response code is 402.
42 * @throws Request_Timeout_Exception When the response code is 408.
43 * @throws Service_Unavailable_Exception When the response code is 503.
44 * @throws Too_Many_Requests_Exception When the response code is 429.
45 * @throws Unauthorized_Exception When the response code is 401.
46 */
47 public function assert_success( Response $response ): Response {
48 $headers = $response->get_headers();
49
50 // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception data is internal, not output.
51 switch ( $response->get_response_code() ) {
52 case 200:
53 return $response;
54 case 401:
55 throw new Unauthorized_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $headers );
56 case 402:
57 throw new Payment_Required_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $response->get_missing_licenses(), $headers );
58 case 403:
59 throw new Forbidden_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $headers );
60 case 404:
61 throw new Not_Found_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $headers );
62 case 408:
63 throw new Request_Timeout_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $headers );
64 case 429:
65 throw new Too_Many_Requests_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $response->get_missing_licenses(), $headers );
66 case 500:
67 throw new Internal_Server_Error_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $headers );
68 case 503:
69 throw new Service_Unavailable_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $headers );
70 default:
71 throw new Bad_Request_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $headers );
72 }
73 // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
74 }
75
76 // phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
77 }
78