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

Yoast / wordpress-seo / 8902429d51b3f7824505257cd9f756866d74786b

29 May 2025 07:09AM UTC coverage: 51.845% (-5.5%) from 57.298%
8902429d51b3f7824505257cd9f756866d74786b

push

github

web-flow
Merge pull request #22264 from Yoast/move-and-refactor-ai-generator-helper

Move and refactor ai generator helper

8230 of 14678 branches covered (56.07%)

Branch coverage included in aggregate %.

0 of 741 new or added lines in 38 files covered. (0.0%)

29750 of 58579 relevant lines covered (50.79%)

40756.97 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
class Request_Handler implements Request_Handler_Interface {
24

25
        private const TIMEOUT = 60;
26

27
        /**
28
         * The API client.
29
         *
30
         * @var API_Client
31
         */
32
        private $api_client;
33

34
        /**
35
         * The response parser.
36
         *
37
         * @var Response_Parser
38
         */
39
        private $response_parser;
40

41
        /**
42
         * Request_Handler constructor.
43
         *
44
         * @param API_Client      $api_client      The API client.
45
         * @param Response_Parser $response_parser The response parser.
46
         */
NEW
47
        public function __construct( API_Client $api_client, Response_Parser $response_parser ) {
×
NEW
48
                $this->api_client      = $api_client;
×
NEW
49
                $this->response_parser = $response_parser;
×
50
        }
51

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

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

NEW
80
                $response = $this->response_parser->parse( $api_response );
×
81

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

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

© 2026 Coveralls, Inc