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
← All changes | src/ai/http-request/application/request-handler.php +24 -42 28.028.5 View file →
@@ -19,16 +19,14 @@
19 19 use Yoast\WP\SEO\AI\HTTP_Request\Infrastructure\API_Client;
20 20
21 21 /**
22 22 * Class Request_Handler
23 - * Handles the request to Yoast AI API.
23 + * Handles the request to Yoast AI API for the legacy Token (JWT) auth path.
24 24 *
25 25 * @makePublic
26 26 */
27 27 class Request_Handler implements Request_Handler_Interface {
28 28
29 - private const TIMEOUT = 60;
30 -
31 29 /**
32 30 * The API client.
33 31 *
34 32 * @var API_Client
@@ -42,19 +40,28 @@
42 40 */
43 41 private $response_parser;
44 42
45 43 /**
44 + * The response validator.
45 + *
46 + * @var Response_Validator
47 + */
48 + private $response_validator;
49 +
50 + /**
46 51 * Request_Handler constructor.
47 52 *
48 - * @param API_Client $api_client The API client.
49 - * @param Response_Parser $response_parser The response parser.
53 + * @param API_Client $api_client The API client.
54 + * @param Response_Parser $response_parser The response parser.
55 + * @param Response_Validator $response_validator The response validator.
50 56 */
51 - public function __construct( API_Client $api_client, Response_Parser $response_parser ) {
52 - $this->api_client = $api_client;
53 - $this->response_parser = $response_parser;
57 + public function __construct( API_Client $api_client, Response_Parser $response_parser, Response_Validator $response_validator ) {
58 + $this->api_client = $api_client;
59 + $this->response_parser = $response_parser;
60 + $this->response_validator = $response_validator;
54 61 }
55 62
56 - // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods.
63 + // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- The @throws list documents the exception family Response_Validator produces.
57 64
58 65 /**
59 66 * Executes the request to the API.
60 67 *
@@ -70,44 +77,19 @@
70 77 * @throws Request_Timeout_Exception When the response code is 408.
71 78 * @throws Service_Unavailable_Exception When the response code is 503.
72 79 * @throws Too_Many_Requests_Exception When the response code is 429.
73 80 * @throws Unauthorized_Exception When the response code is 401.
74 - * @throws WP_Request_Exception When the request fails for any other reason.
81 + * @throws WP_Request_Exception When wp_remote_request returns an error.
75 82 */
76 83 public function handle( Request $request ): Response {
77 - $api_response = $this->api_client->perform_request(
78 - $request->get_action_path(),
79 - $request->get_body(),
80 - $request->get_headers(),
81 - $request->is_post(),
82 - );
84 + $api_response = $this->api_client->perform_request(
85 + $request->get_action_path(),
86 + $request->get_body(),
87 + $request->get_headers(),
88 + $request->get_http_method(),
89 + );
83 90
84 - $response = $this->response_parser->parse( $api_response );
85 -
86 - // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
87 - switch ( $response->get_response_code() ) {
88 - case 200:
89 - return $response;
90 - case 401:
91 - throw new Unauthorized_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
92 - case 402:
93 - throw new Payment_Required_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $response->get_missing_licenses() );
94 - case 403:
95 - throw new Forbidden_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
96 - case 404:
97 - throw new Not_Found_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
98 - case 408:
99 - throw new Request_Timeout_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
100 - case 429:
101 - throw new Too_Many_Requests_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $response->get_missing_licenses() );
102 - case 500:
103 - throw new Internal_Server_Error_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
104 - case 503:
105 - throw new Service_Unavailable_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
106 - default:
107 - throw new Bad_Request_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
108 - }
109 - // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
91 + return $this->response_validator->assert_success( $this->response_parser->parse( $api_response ) );
110 92 }
111 93
112 94 // phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
113 95 }