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 / request-handler.php

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

96 lines 3.4 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\Exceptions\WP_Request_Exception;
17 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Request;
18 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Response;
19 use Yoast\WP\SEO\AI\HTTP_Request\Infrastructure\API_Client;
20
21 /**
22 * Class Request_Handler
23 * Handles the request to Yoast AI API for the legacy Token (JWT) auth path.
24 *
25 * @makePublic
26 */
27 class Request_Handler implements Request_Handler_Interface {
28
29 /**
30 * The API client.
31 *
32 * @var API_Client
33 */
34 private $api_client;
35
36 /**
37 * The response parser.
38 *
39 * @var Response_Parser
40 */
41 private $response_parser;
42
43 /**
44 * The response validator.
45 *
46 * @var Response_Validator
47 */
48 private $response_validator;
49
50 /**
51 * Request_Handler constructor.
52 *
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.
56 */
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;
61 }
62
63 // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- The @throws list documents the exception family Response_Validator produces.
64
65 /**
66 * Executes the request to the API.
67 *
68 * @param Request $request The request to execute.
69 *
70 * @return Response The response from the API.
71 *
72 * @throws Bad_Request_Exception When the request fails for any other reason.
73 * @throws Forbidden_Exception When the response code is 403.
74 * @throws Internal_Server_Error_Exception When the response code is 500.
75 * @throws Not_Found_Exception When the response code is 404.
76 * @throws Payment_Required_Exception When the response code is 402.
77 * @throws Request_Timeout_Exception When the response code is 408.
78 * @throws Service_Unavailable_Exception When the response code is 503.
79 * @throws Too_Many_Requests_Exception When the response code is 429.
80 * @throws Unauthorized_Exception When the response code is 401.
81 * @throws WP_Request_Exception When wp_remote_request returns an error.
82 */
83 public function handle( Request $request ): Response {
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 );
90
91 return $this->response_validator->assert_success( $this->response_parser->parse( $api_response ) );
92 }
93
94 // phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
95 }
96