diff --git a/config/common.yml b/config/common.yml index 891cd9802..7123a9afd 100644 --- a/config/common.yml +++ b/config/common.yml @@ -717,6 +717,8 @@ services: - "@ps_checkout.context.shop" - "@ps_checkout.repository.prestashop.account" - "@ps_checkout.context.prestashop" + - "@ps_checkout.logger.configuration" + - "@ps_checkout.logger" ps_checkout.http.client.factory: class: 'PrestaShop\Module\PrestashopCheckout\Http\HttpClientFactory' 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; } /**