Skip to content

Commit

Permalink
Added logger to new API client
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Mar 6, 2024
1 parent e02044c commit 345a7cb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,8 @@ services:
- '@ps_checkout.context.shop'
- '@ps_checkout.repository.prestashop.account'
- '@ps_checkout.context.prestashop'
- "@ps_checkout.logger.configuration"
- "@ps_checkout.logger"

PrestaShop\Module\PrestashopCheckout\Api\Payment\Client\PayPalOrderHttpClient:
class: 'PrestaShop\Module\PrestashopCheckout\Api\Payment\Client\PayPalOrderHttpClient'
Expand Down
50 changes: 48 additions & 2 deletions src/Builder/Configuration/PaymentClientConfigurationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@

namespace PrestaShop\Module\PrestashopCheckout\Builder\Configuration;

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 PaymentClientConfigurationBuilder implements ConfigurationBuilderInterface
{
Expand All @@ -47,27 +54,39 @@ class PaymentClientConfigurationBuilder implements ConfigurationBuilderInterface
* @var PrestaShopContext
*/
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;
}

/**
* @return array
*/
public function build()
{
return [
$configuration = [
'base_url' => $this->paymentEnv->getPaymentApiUrl(),
'verify' => $this->getVerify(),
'timeout' => static::TIMEOUT,
Expand All @@ -82,6 +101,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;
}

/**
Expand Down

0 comments on commit 345a7cb

Please sign in to comment.