From ffbe9c08d5d8ce33f50aa0141b2e24c903063988 Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Thu, 27 Feb 2025 13:58:44 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8A=20Add=20logging=20to=20the=20inter?=
=?UTF-8?q?nal=20REST=20service?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
modules/ppcp-settings/services.php | 4 +-
.../src/Service/InternalRestService.php | 41 +++++++++++++++++++
2 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/modules/ppcp-settings/services.php b/modules/ppcp-settings/services.php
index 47b7622eb..51426fa77 100644
--- a/modules/ppcp-settings/services.php
+++ b/modules/ppcp-settings/services.php
@@ -318,7 +318,9 @@ return array(
);
},
'settings.service.rest-service' => static function ( ContainerInterface $container ) : InternalRestService {
- return new InternalRestService();
+ return new InternalRestService(
+ $container->get( 'woocommerce.logger.woocommerce' )
+ );
},
'settings.service.sanitizer' => static function ( ContainerInterface $container ) : DataSanitizer {
return new DataSanitizer();
diff --git a/modules/ppcp-settings/src/Service/InternalRestService.php b/modules/ppcp-settings/src/Service/InternalRestService.php
index a83e5fdcf..286cfc5d6 100644
--- a/modules/ppcp-settings/src/Service/InternalRestService.php
+++ b/modules/ppcp-settings/src/Service/InternalRestService.php
@@ -10,15 +10,42 @@ declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\Settings\Service;
use Throwable;
+use Psr\Log\LoggerInterface;
class InternalRestService {
+
+ /**
+ * Logger instance.
+ *
+ * In this case, the logger is quite important for debugging, because the main
+ * functionality of this class cannot be step-debugged using Xdebug: While
+ * a Xdebug session is active, the remote call to the current server is also
+ * blocked and will end in a timeout.
+ *
+ * @var LoggerInterface
+ */
+ private LoggerInterface $logger;
+
+ /**
+ * Constructor.
+ *
+ * @param LoggerInterface $logger Logger instance.
+ */
+ public function __construct( LoggerInterface $logger ) {
+ $this->logger = $logger;
+ }
+
public function get_data( string $endpoint ) : array {
$token = $this->generate_token( $endpoint );
$rest_url = rest_url( $endpoint );
$response = wp_remote_get(
+ $this->logger->info( "Calling internal REST endpoint: $rest_url" );
+
+ $response = wp_remote_request(
$rest_url,
array(
+ 'method' => 'GET',
'headers' => array(
'X-Internal-Token' => $token,
'Content-Type' => 'application/json',
@@ -27,6 +54,8 @@ class InternalRestService {
);
if ( is_wp_error( $response ) ) {
+ $this->logger->error( 'Internal REST error', array( 'response' => $response ) );
+
return array();
}
@@ -35,13 +64,25 @@ class InternalRestService {
try {
$json = json_decode( $body, true, 512, JSON_THROW_ON_ERROR );
} catch ( Throwable $exception ) {
+ $this->logger->error(
+ 'Internal REST error: Invalid JSON response',
+ array(
+ 'error' => $exception->getMessage(),
+ 'response_body' => $body,
+ )
+ );
+
return array();
}
if ( ! $json || empty( $json['success'] ) ) {
+ $this->logger->error( 'Internal REST error: Invalid response', array( 'json' => $json ) );
+
return array();
}
+ $this->logger->info( 'Internal REST success', array( 'data' => $json['data'] ) );
+
return $json['data'];
}