Skip to content

Commit

Permalink
Merge pull request #970 from mollie/PIPRES-462/front_controllers_logg…
Browse files Browse the repository at this point in the history
…ing_refactoring

PIPRES-462/ front controllers logging refactoring
  • Loading branch information
GytisZum authored Sep 17, 2024
2 parents 0ad2305 + 27c7b9c commit 3f2bd2c
Show file tree
Hide file tree
Showing 19 changed files with 252 additions and 76 deletions.
12 changes: 7 additions & 5 deletions controllers/admin/AdminMollieLogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,23 @@ public function processExport($textDelimiter = '"')

$fd = fopen('php://output', 'wb');

/** @var $configuration */
/** @var ConfigurationAdapter $configuration */
$configuration = $this->module->getService(ConfigurationAdapter::class);

/** @var Context $context */
/** @var Mollie\Adapter\Context $context */
$context = $this->module->getService(Mollie\Adapter\Context::class);

$storeInfo = [
'PrestaShop Version' => _PS_VERSION_,
'PHP Version' => phpversion(),
'Module Version' => $this->module->version,
'MySQL Version' => \DB::getInstance()->getVersion(),
'MySQL Version' => \Db::getInstance()->getVersion(),
'Shop URL' => $context->getShopDomain(),
'Shop Name' => $context->getShopName(),
];

$moduleConfigurations = [
'Environment' => $configuration->get(Config::MOLLIE_ENVIRONMENT) ? "Production" : "Sandbox",
'Environment' => $configuration->get(Config::MOLLIE_ENVIRONMENT) ? 'Production' : 'Sandbox',
'Components' => $configuration->get(Config::MOLLIE_IFRAME),
'OCP' => $configuration->get(Config::MOLLIE_SINGLE_CLICK_PAYMENT),
'Locale Webshop' => $configuration->get(Config::MOLLIE_PAYMENTSCREEN_LOCALE),
Expand Down Expand Up @@ -416,6 +416,8 @@ public function processExport($textDelimiter = '"')
$result = \Db::getInstance()->executeS($query);

$firstRow = $result[0];
$headers = [];

foreach ($firstRow as $key => $value) {
$headers[] = strtoupper($key);
}
Expand All @@ -436,6 +438,6 @@ public function processExport($textDelimiter = '"')
}

@fclose($fd);
die;
exit;
}
}
34 changes: 24 additions & 10 deletions controllers/admin/AdminMollieSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

declare(strict_types=1);

use Mollie\Logger\Logger;
use Mollie\Logger\LoggerInterface;
use Mollie\Utility\ExceptionUtility;

if (!defined('_PS_VERSION_')) {
Expand Down Expand Up @@ -45,10 +47,18 @@ private function initCloudSyncAndPsAccounts(): void
$this->context->smarty->assign('module_dir', $this->module->getPathUri());
$moduleManager = PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder::getInstance()->build();

/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

try {
$accountsFacade = $this->module->getService('Mollie.PsAccountsFacade');
$accountsService = $accountsFacade->getPsAccountsService();
} catch (PrestaShop\PsAccountsInstaller\Installer\Exception\InstallerException $e) {
$logger->error('Failed to initiate ps_accounts', [
'context' => [],
'exceptions' => ExceptionUtility::getExceptions($e),
]);

try {
$accountsInstaller = $this->module->getService('Mollie.PsAccountsInstaller');
$accountsInstaller->install();
Expand All @@ -57,9 +67,15 @@ private function initCloudSyncAndPsAccounts(): void
} catch (Exception $e) {
$this->context->controller->errors[] = $e->getMessage();

$logger->error('Failed to install ps_accounts', [
'context' => [],
'exceptions' => ExceptionUtility::getExceptions($e),
]);

return;
}
}

try {
Media::addJsDef([
'contextPsAccounts' => $accountsFacade->getPsAccountsPresenter()
Expand All @@ -70,6 +86,11 @@ private function initCloudSyncAndPsAccounts(): void
$this->context->smarty->assign('urlAccountsCdn', $accountsService->getAccountsCdn());
} catch (Exception $e) {
$this->context->controller->errors[] = $e->getMessage();

$logger->error('Failed to load ps accounts CDN', [
'context' => [],
'exceptions' => ExceptionUtility::getExceptions($e),
]);
}

if ($moduleManager->isInstalled('ps_eventbus')) {
Expand All @@ -93,16 +114,6 @@ private function initCloudSyncAndPsAccounts(): void

public function postProcess()
{
try {
/** @var \Mollie\Logger\LoggerInterface $logger */
$logger = $this->module->getService(\Mollie\Logger\LoggerInterface::class);

$logger->error('Failed to present payment option assets.', [
'context' => [],
'exceptions' => ExceptionUtility::getExceptions(new Exception('Failed to present payment option assets.')),
]);
} catch (PrestaShopException $e) {
}
/** @var \Mollie\Service\Content\TemplateParserInterface $templateParser */
$templateParser = $this->module->getService(\Mollie\Service\Content\TemplateParserInterface::class);

Expand Down Expand Up @@ -194,6 +205,9 @@ public function postProcess()
/** @var \Mollie\Builder\FormBuilder $settingsFormBuilder */
$settingsFormBuilder = $this->module->getService(\Mollie\Builder\FormBuilder::class);

/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

try {
$html .= $settingsFormBuilder->buildSettingsForm();
} catch (PrestaShopDatabaseException $e) {
Expand Down
26 changes: 26 additions & 0 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
use Mollie\Errors\Http\HttpStatusCode;
use Mollie\Exception\FailedToProvidePaymentFeeException;
use Mollie\Infrastructure\Response\JsonResponse;
use Mollie\Logger\Logger;
use Mollie\Logger\LoggerInterface;
use Mollie\Provider\PaymentFeeProviderInterface;
use Mollie\Shared\Core\Shared\Repository\CurrencyRepositoryInterface;
use Mollie\Subscription\Exception\ExceptionCode;
use Mollie\Subscription\Validator\CanProductBeAddedToCartValidator;
use Mollie\Utility\ExceptionUtility;
use Mollie\Utility\NumberUtility;

if (!defined('_PS_VERSION_')) {
Expand All @@ -35,6 +38,11 @@ class MollieAjaxModuleFrontController extends AbstractMollieController

public function postProcess(): void
{
/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$action = Tools::getValue('action');

switch ($action) {
Expand All @@ -47,6 +55,8 @@ public function postProcess(): void
case 'validateProduct':
$this->validateProduct();
}

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));
}

private function getTotalCartPrice(): void
Expand Down Expand Up @@ -92,6 +102,9 @@ private function getTotalCartPrice(): void
/** @var ConfigurationAdapter $configuration */
$configuration = $this->module->getService(ConfigurationAdapter::class);

/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

try {
$paymentFeeData = $paymentFeeProvider->getPaymentFee($molPaymentMethod, (float) $cart->getOrderTotal());
} catch (FailedToProvidePaymentFeeException $exception) {
Expand All @@ -100,6 +113,11 @@ private function getTotalCartPrice(): void
'message' => 'Failed to get payment fee data.',
];

$logger->error('Failed to get payment fee data.', [
'context' => [],
'exceptions' => ExceptionUtility::getExceptions($exception),
]);

$this->returnDefaultOrderSummaryBlock($cart, $errorData);

exit;
Expand Down Expand Up @@ -190,6 +208,9 @@ private function validateProduct(): void
/** @var CanProductBeAddedToCartValidator $canProductBeAddedToCartValidator */
$canProductBeAddedToCartValidator = $this->module->getService(CanProductBeAddedToCartValidator::class);

/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

$product = Tools::getValue('product');

try {
Expand All @@ -213,6 +234,11 @@ private function validateProduct(): void
$this->module->l('Unknown error. Try again or change the attribute to Subscription: none.', self::FILE_NAME),
HttpStatusCode::HTTP_BAD_REQUEST
));

$logger->error('Unknown error.', [
'context' => [],
'exceptions' => ExceptionUtility::getExceptions($exception),
]);
}

$this->ajaxResponse(JsonResponse::success([]));
Expand Down
10 changes: 10 additions & 0 deletions controllers/front/applePayDirectAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Mollie\Application\CommandHandler\UpdateApplePayShippingMethodHandler;
use Mollie\Builder\ApplePayDirect\ApplePayOrderBuilder;
use Mollie\Builder\ApplePayDirect\ApplePayProductBuilder;
use Mollie\Logger\Logger;
use Mollie\Logger\LoggerInterface;
use Mollie\Utility\OrderRecoverUtility;

if (!defined('_PS_VERSION_')) {
Expand All @@ -28,11 +30,17 @@

class MollieApplePayDirectAjaxModuleFrontController extends ModuleFrontController
{
private const FILE_NAME = 'applePayDirectAjax';
/** @var Mollie */
public $module;

public function postProcess()
{
/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$action = Tools::getValue('action');
switch ($action) {
case 'mollie_apple_pay_validation':
Expand All @@ -50,6 +58,8 @@ public function postProcess()
case 'mollie_apple_pay_get_total_price':
$this->getTotalApplePayCartPrice();
}

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));
}

private function getApplePaySession()
Expand Down
11 changes: 11 additions & 0 deletions controllers/front/bancontactAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Mollie\Exception\OrderCreationException;
use Mollie\Exception\RetryOverException;
use Mollie\Handler\RetryHandlerInterface;
use Mollie\Logger\Logger;
use Mollie\Logger\LoggerInterface;
use Mollie\Repository\PaymentMethodRepositoryInterface;
use Mollie\Service\PaymentMethodService;
use Mollie\Utility\OrderNumberUtility;
Expand All @@ -26,11 +28,18 @@

class MollieBancontactAjaxModuleFrontController extends ModuleFrontController
{
private const FILE_NAME = 'bancontactAjax';

/** @var Mollie */
public $module;

public function postProcess()
{
/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$action = Tools::getValue('action');
switch ($action) {
case 'createTransaction':
Expand All @@ -39,6 +48,8 @@ public function postProcess()
case 'checkForPaidTransaction':
$this->checkForPaidTransaction();
}

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));
}

private function createTransaction()
Expand Down
14 changes: 14 additions & 0 deletions controllers/front/fail.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* @codingStandardsIgnoreStart
*/

use Mollie\Logger\Logger;
use Mollie\Logger\LoggerInterface;
use PrestaShop\PrestaShop\Adapter\Order\OrderPresenter;

if (!defined('_PS_VERSION_')) {
Expand All @@ -18,6 +20,8 @@

class MollieFailModuleFrontController extends ModuleFrontController
{
public const FILE_NAME = 'fail';

/**
* ID Order Variable Declaration.
*
Expand Down Expand Up @@ -48,10 +52,18 @@ class MollieFailModuleFrontController extends ModuleFrontController
*/
private $order_presenter;

/** @var Mollie */
public $module;

public function init()
{
parent::init();

/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$this->id_cart = (int) Tools::getValue('cartId', 0);

$redirectLink = 'index.php?controller=history';
Expand All @@ -78,6 +90,8 @@ public function init()
}
/* @phpstan-ignore-next-line */
$this->order_presenter = new OrderPresenter();

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));
}

public function initContent()
Expand Down
11 changes: 11 additions & 0 deletions controllers/front/payScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/

use Mollie\Api\Types\PaymentMethod;
use Mollie\Logger\Logger;
use Mollie\Logger\LoggerInterface;
use Mollie\Provider\ProfileIdProviderInterface;

if (!defined('_PS_VERSION_')) {
Expand All @@ -19,11 +21,18 @@

class MolliePayScreenModuleFrontController extends ModuleFrontController
{
public const FILE_NAME = 'payScreen';

/** @var Mollie */
public $module;

public function postProcess()
{
/** @var Logger $logger * */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$cardToken = Tools::getValue('mollieCardToken');
$isSaveCard = (bool) Tools::getValue('mollieSaveCard');
$useSavedCard = (bool) Tools::getValue('mollieUseSavedCard');
Expand All @@ -41,6 +50,8 @@ public function postProcess()
true
);

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));

Tools::redirect($validateUrl);
}

Expand Down
Loading

0 comments on commit 3f2bd2c

Please sign in to comment.