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

Yoast / wordpress-seo / e57e01309ec9fc3afc714e6ce0fd243e7b6d1f90

27 May 2025 12:40PM UTC coverage: 45.733%. First build
e57e01309ec9fc3afc714e6ce0fd243e7b6d1f90

Pull #22275

github

web-flow
Merge f0a8d33c9 into 5f64dc203
Pull Request #22275: Move and refactor ai generator rest endpoints

0 of 392 new or added lines in 26 files covered. (0.0%)

15548 of 33997 relevant lines covered (45.73%)

3.64 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
         */
19
        public function parse( $response ): Response {
×
20
                $response_code    = ( \wp_remote_retrieve_response_code( $response ) !== '' ) ? \wp_remote_retrieve_response_code( $response ) : 0;
×
21
                $response_message = \esc_html( \wp_remote_retrieve_response_message( $response ) );
×
22
                $error_code       = '';
×
23
                $missing_licenses = [];
×
24

25
                if ( $response_code !== 200 && $response_code !== 0 ) {
×
26
                        $json_body = \json_decode( \wp_remote_retrieve_body( $response ) );
×
27
                        if ( $json_body !== null ) {
×
28
                                $response_message = ( $json_body->message ?? $response_message );
×
29
                                $error_code       = ( $json_body->error_code ?? $this->map_message_to_code( $response_message ) );
×
30
                                if ( $response_code === 402 ) {
×
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
         */
46
        private function map_message_to_code( string $message ): string {
×
47
                if ( \strpos( $message, 'must NOT have fewer than 1 characters' ) !== false ) {
×
48
                        return 'NOT_ENOUGH_CONTENT';
×
49
                }
50
                if ( \strpos( $message, 'Client timeout' ) !== false ) {
×
51
                        return 'CLIENT_TIMEOUT';
×
52
                }
53
                if ( \strpos( $message, 'Server timeout' ) !== false ) {
×
54
                        return 'SERVER_TIMEOUT';
×
55
                }
56

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

© 2025 Coveralls, Inc