• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / e69c2e79c97a57e26aa5c15ba3ca2a387f561b7d

02 Mar 2026 03:20PM UTC coverage: 53.204% (-0.7%) from 53.915%
e69c2e79c97a57e26aa5c15ba3ca2a387f561b7d

Pull #23031

github

web-flow
Merge 25ffa8e80 into 4ff1bc18e
Pull Request #23031: Refactor ai organization

5938 of 10669 branches covered (55.66%)

Branch coverage included in aggregate %.

718 of 759 new or added lines in 38 files covered. (94.6%)

718 existing lines in 33 files now uncovered.

21852 of 41564 relevant lines covered (52.57%)

71980.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ai-http-request/application/request-handler.php
1
<?php
2

3
namespace Yoast\WP\SEO\AI_HTTP_Request\Application;
4

5
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Bad_Request_Exception;
6
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Forbidden_Exception;
7
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Internal_Server_Error_Exception;
8
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Not_Found_Exception;
9
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Payment_Required_Exception;
10
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Request_Timeout_Exception;
11
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Service_Unavailable_Exception;
12
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception;
13
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Unauthorized_Exception;
14
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\WP_Request_Exception;
15
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Request;
16
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Response;
17
use Yoast\WP\SEO\AI_HTTP_Request\Infrastructure\API_Client;
18

19
/**
20
 * Class Request_Handler
21
 * Handles the request to Yoast AI API.
22
 *
23
 * @makePublic
24
 */
25
class Request_Handler implements Request_Handler_Interface {
26

27
        private const TIMEOUT = 60;
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
         * Request_Handler constructor.
45
         *
46
         * @param API_Client      $api_client      The API client.
47
         * @param Response_Parser $response_parser The response parser.
48
         */
UNCOV
49
        public function __construct( API_Client $api_client, Response_Parser $response_parser ) {
×
UNCOV
50
                $this->api_client      = $api_client;
×
UNCOV
51
                $this->response_parser = $response_parser;
×
52
        }
53

54
        // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods.
55

56
        /**
57
         * Executes the request to the API.
58
         *
59
         * @param Request $request The request to execute.
60
         *
61
         * @return Response The response from the API.
62
         *
63
         * @throws Bad_Request_Exception When the request fails for any other reason.
64
         * @throws Forbidden_Exception When the response code is 403.
65
         * @throws Internal_Server_Error_Exception When the response code is 500.
66
         * @throws Not_Found_Exception When the response code is 404.
67
         * @throws Payment_Required_Exception When the response code is 402.
68
         * @throws Request_Timeout_Exception When the response code is 408.
69
         * @throws Service_Unavailable_Exception When the response code is 503.
70
         * @throws Too_Many_Requests_Exception When the response code is 429.
71
         * @throws Unauthorized_Exception When the response code is 401.
72
         * @throws WP_Request_Exception When the request fails for any other reason.
73
         */
UNCOV
74
        public function handle( Request $request ): Response {
×
UNCOV
75
                                $api_response = $this->api_client->perform_request(
×
UNCOV
76
                                        $request->get_action_path(),
×
UNCOV
77
                                        $request->get_body(),
×
UNCOV
78
                                        $request->get_headers(),
×
UNCOV
79
                                        $request->is_post(),
×
UNCOV
80
                                );
×
81

UNCOV
82
                $response = $this->response_parser->parse( $api_response );
×
83

84
                // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
UNCOV
85
                switch ( $response->get_response_code() ) {
×
UNCOV
86
                        case 200:
×
UNCOV
87
                                return $response;
×
UNCOV
88
                        case 401:
×
UNCOV
89
                                throw new Unauthorized_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
×
UNCOV
90
                        case 402:
×
UNCOV
91
                                throw new Payment_Required_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $response->get_missing_licenses() );
×
UNCOV
92
                        case 403:
×
UNCOV
93
                                throw new Forbidden_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
×
UNCOV
94
                        case 404:
×
UNCOV
95
                                throw new Not_Found_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
×
UNCOV
96
                        case 408:
×
UNCOV
97
                                throw new Request_Timeout_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
×
UNCOV
98
                        case 429:
×
UNCOV
99
                                throw new Too_Many_Requests_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code(), null, $response->get_missing_licenses() );
×
UNCOV
100
                        case 500:
×
UNCOV
101
                                throw new Internal_Server_Error_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
×
UNCOV
102
                        case 503:
×
UNCOV
103
                                throw new Service_Unavailable_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
×
104
                        default:
UNCOV
105
                                throw new Bad_Request_Exception( $response->get_message(), $response->get_response_code(), $response->get_error_code() );
×
106
                }
107
                // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
108
        }
109

110
        // phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc