• 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/response-parser.php
1
<?php
2
namespace Yoast\WP\SEO\AI_HTTP_Request\Application;
3

4
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Response;
5

6
/**
7
 * Class Response_Parser
8
 * Parses the response from the AI API and creates a Response object.
9
 */
10
class Response_Parser implements Response_Parser_Interface {
11

12
        /**
13
         * Parses the response from the API.
14
         *
15
         * @param array<int|string|array<string>> $response The response from the API.
16
         *
17
         * @return Response The parsed response.
18
         */
NEW
19
        public function parse( $response ): Response {
×
NEW
20
                $response_code    = ( \wp_remote_retrieve_response_code( $response ) !== '' ) ? \wp_remote_retrieve_response_code( $response ) : 0;
×
NEW
21
                $response_message = \esc_html( \wp_remote_retrieve_response_message( $response ) );
×
NEW
22
                $error_code       = '';
×
NEW
23
                $missing_licenses = [];
×
24

NEW
25
                if ( $response_code !== 200 && $response_code !== 0 ) {
×
NEW
26
                        $json_body = \json_decode( \wp_remote_retrieve_body( $response ) );
×
NEW
27
                        if ( $json_body !== null ) {
×
NEW
28
                                $response_message = ( $json_body->message ?? $response_message );
×
NEW
29
                                $error_code       = ( $json_body->error_code ?? $this->map_message_to_code( $response_message ) );
×
NEW
30
                                if ( $response_code === 402 ) {
×
NEW
31
                                        $missing_licenses = isset( $json_body->missing_licenses ) ? (array) $json_body->missing_licenses : [];
×
32
                                }
33
                        }
34
                }
35

NEW
36
                return new Response( $response['body'], $response_code, $response_message, $error_code, $missing_licenses );
×
37
        }
38

39
        /**
40
         * Maps the error message to a code.
41
         *
42
         * @param string $message The error message.
43
         *
44
         * @return string The mapped code.
45
         */
NEW
46
        private function map_message_to_code( string $message ): string {
×
NEW
47
                if ( \strpos( $message, 'must NOT have fewer than 1 characters' ) !== false ) {
×
NEW
48
                        return 'NOT_ENOUGH_CONTENT';
×
49
                }
NEW
50
                if ( \strpos( $message, 'Client timeout' ) !== false ) {
×
NEW
51
                        return 'CLIENT_TIMEOUT';
×
52
                }
NEW
53
                if ( \strpos( $message, 'Server timeout' ) !== false ) {
×
NEW
54
                        return 'SERVER_TIMEOUT';
×
55
                }
56

NEW
57
                return 'UNKNOWN';
×
58
        }
59
}
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