diff --git a/modules/ppcp-api-client/src/Endpoint/class-requesttrait.php b/modules/ppcp-api-client/src/Endpoint/class-requesttrait.php index c43166b7d..de49dd8fa 100644 --- a/modules/ppcp-api-client/src/Endpoint/class-requesttrait.php +++ b/modules/ppcp-api-client/src/Endpoint/class-requesttrait.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint; use WooCommerce\WooCommerce\Logging\Logger\WooCommerceLogger; +use WP_Error; /** * Trait RequestTrait @@ -40,12 +41,52 @@ trait RequestTrait { $response = wp_remote_get( $url, $args ); - if ( $this->logger instanceof WooCommerceLogger ) { - $this->logger->logRequestResponse( $url, $args, $response, $this->host ); + if ( is_wp_error( $response ) ) { + $this->logger->error( $response->get_error_code() . ' ' . $response->get_error_message() ); + return $response; } + $this->logger->info( $this->request_response_string( $url, $args, $response ) ); return $response; } + /** + * Returns request and response information as string. + * + * @param string $url The request URL. + * @param array $args The request arguments. + * @param array $response The response. + * @return string + */ + private function request_response_string( string $url, array $args, array $response ): string { + $method = $args['method'] ?? ''; + $output = $method . ' ' . $url . "\n"; + if ( isset( $args['body'] ) ) { + if ( ! in_array( + $url, + array( + trailingslashit( $this->host ) . 'v1/oauth2/token/', + trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials', + ), + true + ) ) { + $output .= 'Request Body: ' . wc_print_r( $args['body'], true ) . "\n"; + } + } + if ( isset( $response['headers']->getAll()['paypal-debug-id'] ) ) { + $output .= 'Response Debug ID: ' . $response['headers']->getAll()['paypal-debug-id'] . "\n"; + } + if ( isset( $response['response'] ) ) { + $output .= 'Response: ' . wc_print_r( $response['response'], true ) . "\n"; + + if ( isset( $response['body'] ) + && isset( $response['response']['code'] ) + && ! in_array( $response['response']['code'], array( 200, 201, 202, 204 ), true ) ) { + $output .= 'Response Body: ' . wc_print_r( $response['body'], true ) . "\n"; + } + } + + return $output; + } } diff --git a/modules/woocommerce-logging/src/Logger/class-woocommercelogger.php b/modules/woocommerce-logging/src/Logger/class-woocommercelogger.php index 1ae4f2f76..247f1aa55 100644 --- a/modules/woocommerce-logging/src/Logger/class-woocommercelogger.php +++ b/modules/woocommerce-logging/src/Logger/class-woocommercelogger.php @@ -62,53 +62,4 @@ class WooCommerceLogger implements LoggerInterface { } $this->wc_logger->log( $level, $message, $context ); } - - /** - * Logs request and response information. - * - * @param string $url The request URL. - * @param array $args The request arguments. - * @param array|WP_Error $response The response or WP_Error on failure. - * @param string $host The host. - * @return void - */ - public function logRequestResponse( string $url, array $args, $response, string $host ) { - - if ( is_wp_error( $response ) ) { - $this->error( $response->get_error_code() . ' ' . $response->get_error_message() ); - return; - } - - $method = $args['method'] ?? ''; - $output = $method . ' ' . $url . "\n"; - if ( isset( $args['body'] ) ) { - if ( ! in_array( - $url, - array( - trailingslashit( $host ) . 'v1/oauth2/token/', - trailingslashit( $host ) . 'v1/oauth2/token?grant_type=client_credentials', - ), - true - ) ) { - $output .= 'Request Body: ' . wc_print_r( $args['body'], true ) . "\n"; - } - } - - if ( is_array( $response ) ) { - if ( isset( $response['headers']->getAll()['paypal-debug-id'] ) ) { - $output .= 'Response Debug ID: ' . $response['headers']->getAll()['paypal-debug-id'] . "\n"; - } - if ( isset( $response['response'] ) ) { - $output .= 'Response: ' . wc_print_r( $response['response'], true ) . "\n"; - - if ( isset( $response['body'] ) - && isset( $response['response']['code'] ) - && ! in_array( $response['response']['code'], array( 200, 201, 202, 204 ), true ) ) { - $output .= 'Response Body: ' . wc_print_r( $response['body'], true ) . "\n"; - } - } - } - - $this->info( $output ); - } }