From d20090661b43a18c07e48c25f640023423ac793b Mon Sep 17 00:00:00 2001 From: Matt75 <5262628+Matt75@users.noreply.github.com> Date: Wed, 28 Feb 2024 19:02:36 +0100 Subject: [PATCH] Restore http logger --- ...CheckoutHttpClientConfigurationBuilder.php | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Http/CheckoutHttpClientConfigurationBuilder.php b/src/Http/CheckoutHttpClientConfigurationBuilder.php index 03f5bb8b9..e5bd032fc 100644 --- a/src/Http/CheckoutHttpClientConfigurationBuilder.php +++ b/src/Http/CheckoutHttpClientConfigurationBuilder.php @@ -20,12 +20,19 @@ namespace PrestaShop\Module\PrestashopCheckout\Http; +use GuzzleHttp\Event\Emitter; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Subscriber\Log\Formatter; +use GuzzleHttp\Subscriber\Log\LogSubscriber; +use GuzzleLogMiddleware\LogMiddleware; use PrestaShop\Module\PrestashopCheckout\Context\PrestaShopContext; use PrestaShop\Module\PrestashopCheckout\Environment\PaymentEnv; +use PrestaShop\Module\PrestashopCheckout\Logger\LoggerConfiguration; use PrestaShop\Module\PrestashopCheckout\Repository\PsAccountRepository; use PrestaShop\Module\PrestashopCheckout\Routing\Router; use PrestaShop\Module\PrestashopCheckout\ShopContext; use Ps_checkout; +use Psr\Log\LoggerInterface; class CheckoutHttpClientConfigurationBuilder implements HttpClientConfigurationBuilderInterface { @@ -56,18 +63,32 @@ class CheckoutHttpClientConfigurationBuilder implements HttpClientConfigurationB */ private $prestaShopContext; + /** + * @var LoggerConfiguration + */ + private $loggerConfiguration; + + /** + * @var LoggerInterface + */ + private $logger; + public function __construct( PaymentEnv $paymentEnv, Router $router, ShopContext $shopContext, PsAccountRepository $psAccountRepository, - PrestaShopContext $prestaShopContext + PrestaShopContext $prestaShopContext, + LoggerConfiguration $loggerConfiguration, + LoggerInterface $logger ) { $this->paymentEnv = $paymentEnv; $this->router = $router; $this->shopContext = $shopContext; $this->psAccountRepository = $psAccountRepository; $this->prestaShopContext = $prestaShopContext; + $this->loggerConfiguration = $loggerConfiguration; + $this->logger = $logger; } /** @@ -75,7 +96,7 @@ public function __construct( */ public function build() { - return [ + $configuration = [ 'base_url' => $this->paymentEnv->getPaymentApiUrl(), 'verify' => $this->getVerify(), 'timeout' => static::TIMEOUT, @@ -90,6 +111,33 @@ public function build() 'Prestashop-Version' => _PS_VERSION_, // prestashop version ], ]; + + if ( + $this->loggerConfiguration->isHttpEnabled() + && defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION') + && class_exists(HandlerStack::class) + && class_exists(LogMiddleware::class) + ) { + $handlerStack = HandlerStack::create(); + $handlerStack->push(new LogMiddleware($this->logger)); + $configuration['handler'] = $handlerStack; + } elseif ( + $this->loggerConfiguration->isHttpEnabled() + && defined('\GuzzleHttp\ClientInterface::VERSION') + && class_exists(Emitter::class) + && class_exists(LogSubscriber::class) + && class_exists(Formatter::class) + ) { + $emitter = new Emitter(); + $emitter->attach(new LogSubscriber( + $this->logger, + Formatter::DEBUG + )); + + $configuration['emitter'] = $emitter; + } + + return $configuration; } /**