From c2af20dddf6e9fbcc0e505249312ed61dc9b791c Mon Sep 17 00:00:00 2001 From: mandan2 <61560082+mandan2@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:38:48 +0200 Subject: [PATCH] PIPRES-349: Update subscription action (#859) * PIPRES-349: Subscription carrier data provider * renamed service and added missing DTO * fixed return type * renamed service * PIPRES-349: Update subscription action * updated exception code --- mollie.php | 3 +- src/Logger/PrestaLogger.php | 1 + src/ServiceProvider/BaseServiceProvider.php | 3 - .../Action/CreateRecurringOrderAction.php | 6 +- .../CreateRecurringOrdersProductAction.php | 6 +- .../Action/UpdateSubscriptionAction.php | 55 ++++++++++ .../Exception/CouldNotUpdateSubscription.php | 32 ++++++ subscription/Exception/ExceptionCode.php | 1 + subscription/Install/AttributeInstaller.php | 6 +- subscription/Install/AttributeUninstaller.php | 6 +- subscription/Logger/Logger.php | 102 ------------------ subscription/Logger/LoggerInterface.php | 21 ---- subscription/Logger/NullLogger.php | 95 ---------------- subscription/Logger/index.php | 20 ---- 14 files changed, 102 insertions(+), 255 deletions(-) create mode 100644 subscription/Action/UpdateSubscriptionAction.php create mode 100644 subscription/Exception/CouldNotUpdateSubscription.php delete mode 100644 subscription/Logger/Logger.php delete mode 100644 subscription/Logger/LoggerInterface.php delete mode 100644 subscription/Logger/NullLogger.php delete mode 100644 subscription/Logger/index.php diff --git a/mollie.php b/mollie.php index 1e6a1d8fe..aa1affb7f 100755 --- a/mollie.php +++ b/mollie.php @@ -31,7 +31,6 @@ use Mollie\Subscription\Install\DatabaseTableInstaller; use Mollie\Subscription\Install\HookInstaller; use Mollie\Subscription\Install\Installer; -use Mollie\Subscription\Logger\NullLogger; use Mollie\Subscription\Provider\SubscriptionProductProvider; use Mollie\Subscription\Repository\LanguageRepository as LanguageAdapter; use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface; @@ -179,7 +178,7 @@ public function install() $subscriptionInstaller = new Installer( new DatabaseTableInstaller(), new AttributeInstaller( - new NullLogger(), + $this->getService(PrestaLoggerInterface::class), $this->getService(ConfigurationAdapter::class), $this, new LanguageAdapter(), diff --git a/src/Logger/PrestaLogger.php b/src/Logger/PrestaLogger.php index 950dd4cbc..2f603e7e6 100644 --- a/src/Logger/PrestaLogger.php +++ b/src/Logger/PrestaLogger.php @@ -22,6 +22,7 @@ class PrestaLogger implements PrestaLoggerInterface { + // TODO move this as a shared service for subscriptions and main source // TODO refactor whole logger logic and implement leftover methods /** @var ConfigurationAdapter */ diff --git a/src/ServiceProvider/BaseServiceProvider.php b/src/ServiceProvider/BaseServiceProvider.php index 04cd7bd93..a876a7d06 100644 --- a/src/ServiceProvider/BaseServiceProvider.php +++ b/src/ServiceProvider/BaseServiceProvider.php @@ -108,8 +108,6 @@ use Mollie\Subscription\Grid\Accessibility\SubscriptionCancelAccessibility; use Mollie\Subscription\Install\Installer; use Mollie\Subscription\Install\InstallerInterface; -use Mollie\Subscription\Logger\Logger; -use Mollie\Subscription\Logger\LoggerInterface; use Mollie\Subscription\Repository\CombinationRepository; use Mollie\Subscription\Repository\CombinationRepositoryInterface; use Mollie\Subscription\Repository\OrderDetailRepository; @@ -149,7 +147,6 @@ public function __construct($extendedServices) public function register(Container $container) { /* Logger */ - $this->addService($container, LoggerInterface::class, $container->get(Logger::class)); $this->addService($container, PrestaLoggerInterface::class, $container->get(PrestaLogger::class)); /* Utility */ diff --git a/subscription/Action/CreateRecurringOrderAction.php b/subscription/Action/CreateRecurringOrderAction.php index 09aa62f18..a609b8d25 100644 --- a/subscription/Action/CreateRecurringOrderAction.php +++ b/subscription/Action/CreateRecurringOrderAction.php @@ -12,10 +12,10 @@ namespace Mollie\Subscription\Action; +use Mollie\Logger\PrestaLoggerInterface; use Mollie\Subscription\DTO\CreateRecurringOrderData; use Mollie\Subscription\Exception\CouldNotCreateRecurringOrder; use Mollie\Subscription\Exception\MollieSubscriptionException; -use Mollie\Subscription\Logger\LoggerInterface; use Mollie\Subscription\Utility\ClockInterface; if (!defined('_PS_VERSION_')) { @@ -24,13 +24,13 @@ class CreateRecurringOrderAction { - /** @var LoggerInterface */ + /** @var PrestaLoggerInterface */ private $logger; /** @var ClockInterface */ private $clock; public function __construct( - LoggerInterface $logger, + PrestaLoggerInterface $logger, ClockInterface $clock ) { $this->logger = $logger; diff --git a/subscription/Action/CreateRecurringOrdersProductAction.php b/subscription/Action/CreateRecurringOrdersProductAction.php index af529907e..d3d312a72 100644 --- a/subscription/Action/CreateRecurringOrdersProductAction.php +++ b/subscription/Action/CreateRecurringOrdersProductAction.php @@ -12,10 +12,10 @@ namespace Mollie\Subscription\Action; +use Mollie\Logger\PrestaLoggerInterface; use Mollie\Subscription\DTO\CreateRecurringOrdersProductData; use Mollie\Subscription\Exception\CouldNotCreateRecurringOrdersProduct; use Mollie\Subscription\Exception\MollieSubscriptionException; -use Mollie\Subscription\Logger\LoggerInterface; if (!defined('_PS_VERSION_')) { exit; @@ -23,11 +23,11 @@ class CreateRecurringOrdersProductAction { - /** @var LoggerInterface */ + /** @var PrestaLoggerInterface */ private $logger; public function __construct( - LoggerInterface $logger + PrestaLoggerInterface $logger ) { $this->logger = $logger; } diff --git a/subscription/Action/UpdateSubscriptionAction.php b/subscription/Action/UpdateSubscriptionAction.php new file mode 100644 index 000000000..e85aa26cf --- /dev/null +++ b/subscription/Action/UpdateSubscriptionAction.php @@ -0,0 +1,55 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Subscription\Action; + +use Mollie\Logger\PrestaLoggerInterface; +use Mollie\Subscription\Api\SubscriptionApi; +use Mollie\Subscription\DTO\UpdateSubscriptionData; +use Mollie\Subscription\Exception\CouldNotUpdateSubscription; +use Mollie\Subscription\Exception\MollieSubscriptionException; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class UpdateSubscriptionAction +{ + /** @var SubscriptionApi */ + private $subscriptionApi; + /** @var PrestaLoggerInterface */ + private $logger; + + public function __construct( + SubscriptionApi $subscriptionApi, + PrestaLoggerInterface $logger + ) { + $this->subscriptionApi = $subscriptionApi; + $this->logger = $logger; + } + + /** + * @throws MollieSubscriptionException + */ + public function run(UpdateSubscriptionData $data): void + { + $this->logger->info(sprintf('%s - Function called', __METHOD__)); + + try { + $this->subscriptionApi->updateSubscription($data); + } catch (\Throwable $exception) { + throw CouldNotUpdateSubscription::failedToUpdateSubscription($exception, $data->getSubscriptionId()); + } + + $this->logger->info(sprintf('%s - Function ended', __METHOD__)); + } +} diff --git a/subscription/Exception/CouldNotUpdateSubscription.php b/subscription/Exception/CouldNotUpdateSubscription.php new file mode 100644 index 000000000..78bac85bb --- /dev/null +++ b/subscription/Exception/CouldNotUpdateSubscription.php @@ -0,0 +1,32 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Subscription\Exception; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class CouldNotUpdateSubscription extends MollieSubscriptionException +{ + public static function failedToUpdateSubscription(\Throwable $exception, string $subscriptionId): self + { + return new self( + sprintf( + 'Failed to update subscription. Subscription ID: (%s)', + $subscriptionId + ), + ExceptionCode::ORDER_FAILED_TO_UPDATE_SUBSCRIPTION, + $exception + ); + } +} diff --git a/subscription/Exception/ExceptionCode.php b/subscription/Exception/ExceptionCode.php index cb0fb2e10..cbf7b032d 100644 --- a/subscription/Exception/ExceptionCode.php +++ b/subscription/Exception/ExceptionCode.php @@ -43,6 +43,7 @@ class ExceptionCode public const ORDER_FAILED_TO_CREATE_RECURRING_ORDERS_PRODUCT = 1021; public const ORDER_FAILED_TO_CREATE_RECURRING_ORDER = 1022; public const ORDER_FAILED_TO_PROVIDE_SUBSCRIPTION_ORDER_AMOUNT = 1023; + public const ORDER_FAILED_TO_UPDATE_SUBSCRIPTION = 1024; //Cart error codes starts from 2000 diff --git a/subscription/Install/AttributeInstaller.php b/subscription/Install/AttributeInstaller.php index ebf541dba..4fdd6c64b 100644 --- a/subscription/Install/AttributeInstaller.php +++ b/subscription/Install/AttributeInstaller.php @@ -18,8 +18,8 @@ use Mollie; use Mollie\Adapter\ConfigurationAdapter; use Mollie\Adapter\ProductAttributeAdapter; +use Mollie\Logger\PrestaLoggerInterface; use Mollie\Subscription\Config\Config; -use Mollie\Subscription\Logger\LoggerInterface; use Mollie\Subscription\Repository\LanguageRepository; use PrestaShopDatabaseException; use PrestaShopException; @@ -43,14 +43,14 @@ class AttributeInstaller extends AbstractInstaller /** @var LanguageRepository */ private $language; - /** @var LoggerInterface */ + /** @var PrestaLoggerInterface */ private $logger; /** @var ProductAttributeAdapter */ private $productAttributeAdapter; public function __construct( - LoggerInterface $logger, + PrestaLoggerInterface $logger, ConfigurationAdapter $configuration, Mollie $module, LanguageRepository $language, diff --git a/subscription/Install/AttributeUninstaller.php b/subscription/Install/AttributeUninstaller.php index 7765bcc42..e5dd017a3 100644 --- a/subscription/Install/AttributeUninstaller.php +++ b/subscription/Install/AttributeUninstaller.php @@ -17,8 +17,8 @@ use Mollie; use Mollie\Adapter\ConfigurationAdapter; use Mollie\Adapter\ProductAttributeAdapter; +use Mollie\Logger\PrestaLoggerInterface; use Mollie\Subscription\Config\Config; -use Mollie\Subscription\Logger\LoggerInterface; use PrestaShopException; use Psr\Log\LogLevel; @@ -36,13 +36,13 @@ class AttributeUninstaller extends AbstractUninstaller /** @var Mollie */ private $module; - /** @var LoggerInterface */ + /** @var PrestaLoggerInterface */ private $logger; /** @var ProductAttributeAdapter */ private $productAttributeAdapter; public function __construct( - LoggerInterface $logger, + PrestaLoggerInterface $logger, ConfigurationAdapter $configuration, Mollie $module, ProductAttributeAdapter $productAttributeAdapter diff --git a/subscription/Logger/Logger.php b/subscription/Logger/Logger.php deleted file mode 100644 index 97bb1f21c..000000000 --- a/subscription/Logger/Logger.php +++ /dev/null @@ -1,102 +0,0 @@ - - * @copyright Mollie B.V. - * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md - * - * @see https://github.com/mollie/PrestaShop - * @codingStandardsIgnoreStart - */ - -declare(strict_types=1); - -namespace Mollie\Subscription\Logger; - -if (!defined('_PS_VERSION_')) { - exit; -} - -class Logger implements LoggerInterface -{ - const FILE_NAME = 'Logger'; - - const LOG_OBJECT_TYPE = 'mollie_sub_log'; - - // TODO fix this logger - - /** - * @return null - */ - public function emergency($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function alert($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function critical($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function error($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function warning($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function notice($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function info($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function debug($message, array $context = []) - { - return null; - } - - public function log($level, $message, array $context = []): void - { - \PrestaShopLogger::addLog( - $message, - $level, - null, - self::LOG_OBJECT_TYPE - ); - } -} diff --git a/subscription/Logger/LoggerInterface.php b/subscription/Logger/LoggerInterface.php deleted file mode 100644 index 1d8f0b5a3..000000000 --- a/subscription/Logger/LoggerInterface.php +++ /dev/null @@ -1,21 +0,0 @@ - - * @copyright Mollie B.V. - * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md - * - * @see https://github.com/mollie/PrestaShop - * @codingStandardsIgnoreStart - */ - -namespace Mollie\Subscription\Logger; - -if (!defined('_PS_VERSION_')) { - exit; -} - -interface LoggerInterface extends \Psr\Log\LoggerInterface -{ -} diff --git a/subscription/Logger/NullLogger.php b/subscription/Logger/NullLogger.php deleted file mode 100644 index bdfee6fe0..000000000 --- a/subscription/Logger/NullLogger.php +++ /dev/null @@ -1,95 +0,0 @@ - - * @copyright Mollie B.V. - * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md - * - * @see https://github.com/mollie/PrestaShop - * @codingStandardsIgnoreStart - */ - -declare(strict_types=1); - -namespace Mollie\Subscription\Logger; - -if (!defined('_PS_VERSION_')) { - exit; -} - -//NOTE only should be used for tests -final class NullLogger implements LoggerInterface -{ - /** - * @return null - */ - public function emergency($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function alert($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function critical($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function error($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function warning($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function notice($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function info($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function debug($message, array $context = []) - { - return null; - } - - /** - * @return null - */ - public function log($level, $message, array $context = []) - { - return null; - } -} diff --git a/subscription/Logger/index.php b/subscription/Logger/index.php deleted file mode 100644 index d3343995f..000000000 --- a/subscription/Logger/index.php +++ /dev/null @@ -1,20 +0,0 @@ - - * @copyright Mollie B.V. - * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md - * - * @see https://github.com/mollie/PrestaShop - * @codingStandardsIgnoreStart - */ -header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); -header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); - -header('Cache-Control: no-store, no-cache, must-revalidate'); -header('Cache-Control: post-check=0, pre-check=0', false); -header('Pragma: no-cache'); - -header('Location: ../'); -exit;