Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SV-34 Implemented Logger interface #202

Merged
merged 17 commits into from
Oct 8, 2024
Merged
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
"phpunit/phpunit": "*",
"behat/behat": "*",
"symfony/translation": "*",
"prestashop/php-dev-tools": "^3.16",
"invertus/knapsack": "^10.0"
"prestashop/php-dev-tools": "^3.16"
MarijusCoding marked this conversation as resolved.
Show resolved Hide resolved
},
"scripts": {
"test-integration": "./vendor/bin/phpunit --configuration ./tests/Integration/phpunit.xml",
Expand Down
57 changes: 32 additions & 25 deletions controllers/admin/AdminSaferPayOfficialLogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
*@license SIX Payment Services
*/

use Invertus\SaferPay\Adapter\LegacyContext;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\Saferpay\Context\GlobalShopContext;
use Invertus\SaferPay\Controller\AbstractAdminSaferPayController;
use Invertus\SaferPay\Enum\PermissionType;
use Invertus\SaferPay\Logger\Formatter\LogFormatter;
use Invertus\SaferPay\Logger\LoggerInterface;
use Invertus\SaferPay\Repository\SaferPayLogRepository;
use Invertus\SaferPay\Utility\ExceptionUtility;
use Invertus\SaferPay\Utility\VersionUtility;
use Invertus\SaferPay\Logger\Logger;
use Invertus\SaferPay\Adapter\Tools;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down Expand Up @@ -150,7 +156,8 @@ public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);

$context = $this->module->getService(\Invertus\SaferPay\Adapter\LegacyContext::class);
/** @var LegacyContext $context */
$context = $this->module->getService(LegacyContext::class);

Media::addJsDef([
'saferpayofficial' => [
Expand Down Expand Up @@ -239,19 +246,19 @@ public function displayAjaxGetLog()
return;
}

/** @var \Invertus\SaferPay\Adapter\Tools $tools */
$tools = $this->module->getService(\Invertus\SaferPay\Adapter\Tools::class);
/** @var Tools $tools */
$tools = $this->module->getService(Tools::class);

/** @var \Invertus\SaferPay\Repository\SaferPayLogRepository $logRepository */
$logRepository = $this->module->getService(\Invertus\SaferPay\Repository\SaferPayLogRepository::class);
/** @var SaferPayLogRepository $logRepository */
$logRepository = $this->module->getService(SaferPayLogRepository::class);

/** @var \Invertus\SaferPay\Context\GlobalShopContext $shopContext */
$globalShopContext = $this->module->getService(\Invertus\SaferPay\Context\GlobalShopContext::class);
/** @var GlobalShopContext $shopContext */
$globalShopContext = $this->module->getService(GlobalShopContext::class);

$logId = $tools->getValueAsInt('log_id');

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

try {
/** @var \SaferPayLog|null $log */
Expand All @@ -260,13 +267,13 @@ public function displayAjaxGetLog()
'id_shop' => $globalShopContext->getShopId(),
]);
} catch (Exception $exception) {
// $logger->error('Failed to find log', [
// 'context' => [
// 'id_log' => $logId,
// 'id_shop' => $globalShopContext->getShopId(),
// ],
// 'exceptions' => ExceptionUtility::getExceptions($exception),
// ]);
$logger->error('Failed to find log', [
'context' => [
'id_log' => $logId,
'id_shop' => $globalShopContext->getShopId(),
],
'exceptions' => ExceptionUtility::getExceptions($exception),
]);

$this->ajaxResponse(json_encode([
'error' => true,
Expand All @@ -275,13 +282,13 @@ public function displayAjaxGetLog()
}

if (!isset($log)) {
// $logger->error('No log information found.', [
// 'context' => [
// 'id_log' => $logId,
// 'id_shop' => $globalShopContext->getShopId(),
// ],
// 'exceptions' => [],
// ]);
$logger->error('No log information found.', [
'context' => [
'id_log' => $logId,
'id_shop' => $globalShopContext->getShopId(),
],
'exceptions' => [],
]);

$this->ajaxRender(json_encode([
'error' => true,
Expand Down Expand Up @@ -316,8 +323,8 @@ public function processExport($textDelimiter = '"')
/** @var Configuration $configuration */
$configuration = $this->module->getService(Configuration::class);

/** @var \Invertus\SaferPay\Adapter\LegacyContext $context */
$context = $this->module->getService(\Invertus\SaferPay\Adapter\LegacyContext::class);
/** @var LegacyContext $context */
$context = $this->module->getService(LegacyContext::class);

$storeInfo = [
'PrestaShop Version' => _PS_VERSION_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function __construct()
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);

$this->addCSS("{$this->module->getPathUri()}views/css/admin/payment_method.css");
$this->addJS("{$this->module->getPathUri()}views/js/admin/chosen_countries.js");
$this->addJS("{$this->module->getPathUri()}views/js/admin/payment_method_all.js");
Expand Down
5 changes: 5 additions & 0 deletions src/Config/SaferPayConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ public static function isTestMode()
return (bool) Configuration::get(self::TEST_MODE);
}

public static function isDebugMode()
{
return (bool) Configuration::get(self::SAFERPAY_DEBUG_MODE);
}

public static function isVersion17()
{
return (bool) version_compare(_PS_VERSION_, '1.7', '>=');
Expand Down
4 changes: 2 additions & 2 deletions src/Context/GlobalShopContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*@license SIX Payment Services
*/

namespace Invertus\Saferpay\Context;
namespace Invertus\SaferPay\Context;

use Invertus\SaferPay\Adapter\LegacyContext;

Expand All @@ -32,7 +32,7 @@
* Gets shop context data
* NOTE: Done without interface because throwing error in the module
*/
class GlobalShopContext
class GlobalShopContext implements GlobalShopContextInterface
{
private $context;

Expand Down
50 changes: 50 additions & 0 deletions src/Context/GlobalShopContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
*NOTICE OF LICENSE
*
*This source file is subject to the Open Software License (OSL 3.0)
*that is bundled with this package in the file LICENSE.txt.
*It is also available through the world-wide-web at this URL:
*http://opensource.org/licenses/osl-3.0.php
*If you did not receive a copy of the license and are unable to
*obtain it through the world-wide-web, please send an email
*to [email protected] so we can send you a copy immediately.
*
*DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
*versions in the future. If you wish to customize PrestaShop for your
*needs please refer to http://www.prestashop.com for more information.
*
*@author INVERTUS UAB www.invertus.eu <[email protected]>
*@copyright SIX Payment Services
*@license SIX Payment Services
*/

namespace Invertus\SaferPay\Context;

if (!defined('_PS_VERSION_')) {
exit;
}

/**
* Gets shop context data
*/
interface GlobalShopContextInterface
{
public function getShopId();

public function getLanguageId();

public function getLanguageIso();

public function getCurrencyIso();

public function getCurrency();

public function getShopDomain();

public function getShopName();

public function isShopSingleShopContext();
}
51 changes: 51 additions & 0 deletions src/EntityManager/EntityManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
*NOTICE OF LICENSE
*
*This source file is subject to the Open Software License (OSL 3.0)
*that is bundled with this package in the file LICENSE.txt.
*It is also available through the world-wide-web at this URL:
*http://opensource.org/licenses/osl-3.0.php
*If you did not receive a copy of the license and are unable to
*obtain it through the world-wide-web, please send an email
*to [email protected] so we can send you a copy immediately.
*
*DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
*versions in the future. If you wish to customize PrestaShop for your
*needs please refer to http://www.prestashop.com for more information.
*
*@author INVERTUS UAB www.invertus.eu <[email protected]>
*@copyright SIX Payment Services
*@license SIX Payment Services
*/

namespace Invertus\SaferPay\EntityManager;

if (!defined('_PS_VERSION_')) {
exit;
}

interface EntityManagerInterface
{
/**
* @param \ObjectModel $model
* @param string $unitOfWorkType - @see ObjectModelUnitOfWork
* @param string|null $specificKey
*
* @return EntityManagerInterface
*/
public function persist(
\ObjectModel $model,
$unitOfWorkType,
$specificKey = null
);

/**
* @return array<\ObjectModel>
*
* @throws \PrestaShopException
*/
public function flush();
}
86 changes: 86 additions & 0 deletions src/EntityManager/ObjectModelEntityManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
*NOTICE OF LICENSE
*
*This source file is subject to the Open Software License (OSL 3.0)
*that is bundled with this package in the file LICENSE.txt.
*It is also available through the world-wide-web at this URL:
*http://opensource.org/licenses/osl-3.0.php
*If you did not receive a copy of the license and are unable to
*obtain it through the world-wide-web, please send an email
*to [email protected] so we can send you a copy immediately.
*
*DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
*versions in the future. If you wish to customize PrestaShop for your
*needs please refer to http://www.prestashop.com for more information.
*
*@author INVERTUS UAB www.invertus.eu <[email protected]>
*@copyright SIX Payment Services
*@license SIX Payment Services
*/

namespace Invertus\SaferPay\EntityManager;

if (!defined('_PS_VERSION_')) {
exit;
}

class ObjectModelEntityManager implements EntityManagerInterface
{
private $unitOfWork;

public function __construct(ObjectModelUnitOfWork $unitOfWork)
{
$this->unitOfWork = $unitOfWork;
}

/**
* @param \ObjectModel $model
* @param string $unitOfWorkType
* @param string|null $specificKey
* for example external_id key to make it easier to keep
* track of which object model is related to which external_id
*/
public function persist(
\ObjectModel $model,
$unitOfWorkType,
$specificKey = null
) {
$this->unitOfWork->setWork($model, $unitOfWorkType, $specificKey);

return $this;
}

/**
* @return array<\ObjectModel>
*
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function flush()
{
$persistenceModels = $this->unitOfWork->getWork();
$persistedModels = [];

foreach ($persistenceModels as $externalId => $persistenceModel) {
if ($persistenceModel['unit_of_work_type'] === ObjectModelUnitOfWork::UNIT_OF_WORK_SAVE) {
$persistenceModel['object']->save();
}

if ($persistenceModel['unit_of_work_type'] === ObjectModelUnitOfWork::UNIT_OF_WORK_DELETE) {
$persistenceModel['object']->delete();
}

if (!empty($externalId)) {
$persistedModels[$externalId] = $persistenceModel['object'];
} else {
$persistedModels[] = $persistenceModel['object'];
}
}
$this->unitOfWork->clearWork();

return $persistedModels;
}
}
Loading
Loading