From 49783746175b2bf542cd5a3a780665de7f7b7841 Mon Sep 17 00:00:00 2001 From: mandan2 Date: Tue, 13 Jun 2023 10:10:06 +0300 Subject: [PATCH] PIPRES-180: Order status soft delete on uninstall --- src/Config/Config.php | 2 -- src/Factory/ModuleFactory.php | 10 ++++++++++ src/Install/DatabaseTableUninstaller.php | 17 ++++++++++++++-- src/Install/Uninstall.php | 22 ++++++++++++++++++++- src/ServiceProvider/BaseServiceProvider.php | 3 ++- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/Config/Config.php b/src/Config/Config.php index e1ce8c3fa..f821a56e0 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -128,10 +128,8 @@ class Config const MOLLIE_MAIL_WHEN_CANCELED = 'MOLLIE_MAIL_WHEN_CANCELED'; const MOLLIE_MAIL_WHEN_EXPIRED = 'MOLLIE_MAIL_WHEN_EXPIRED'; const MOLLIE_MAIL_WHEN_REFUNDED = 'MOLLIE_MAIL_WHEN_REFUNDED'; - const MOLLIE_MAIL_WHEN_PARTIAL_REFUND = 'MOLLIE_MAIL_WHEN_PARTIAL_REFUND'; const MOLLIE_MAIL_WHEN_CHARGEBACK = 'MOLLIE_MAIL_WHEN_CHARGEBACK'; const PARTIAL_REFUND_CODE = 'partial_refund'; - const MOLLIE_STATUS_INITIATED = 'MOLLIE_STATUS_INITIATED'; const MOLLIE_STATUS_PARTIALLY_SHIPPED = 'MOLLIE_PARTIALLY_SHIPPED'; const MOLLIE_STATUS_ORDER_COMPLETED = 'MOLLIE_STATUS_ORDER_COMPLETED'; const MOLLIE_STATUS_DEFAULT = 'MOLLIE_STATUS_DEFAULT'; diff --git a/src/Factory/ModuleFactory.php b/src/Factory/ModuleFactory.php index 6f062a6fc..8f6ae872d 100644 --- a/src/Factory/ModuleFactory.php +++ b/src/Factory/ModuleFactory.php @@ -57,4 +57,14 @@ public function getModule(): ?Mollie return $module; } + public function getModuleName(): ?string + { + $module = $this->getModule(); + + if (!$module) { + return null; + } + + return $module->name ?? null; + } } diff --git a/src/Install/DatabaseTableUninstaller.php b/src/Install/DatabaseTableUninstaller.php index 6f5ee4e5a..256b2a2d6 100644 --- a/src/Install/DatabaseTableUninstaller.php +++ b/src/Install/DatabaseTableUninstaller.php @@ -13,10 +13,19 @@ namespace Mollie\Install; use Db; +use Mollie\Factory\ModuleFactory; final class DatabaseTableUninstaller implements UninstallerInterface { - public function uninstall() + /** @var ModuleFactory */ + private $moduleFactory; + + public function __construct(ModuleFactory $moduleFactory) + { + $this->moduleFactory = $moduleFactory; + } + + public function uninstall(): bool { foreach ($this->getCommands() as $query) { if (false == Db::getInstance()->execute($query)) { @@ -27,7 +36,7 @@ public function uninstall() return true; } - private function getCommands() + private function getCommands(): array { $sql = []; @@ -41,6 +50,10 @@ private function getCommands() $sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_pending_order_cart_rule`;'; $sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_payment_method_order_total_restriction`;'; + if ($moduleName = $this->moduleFactory->getModuleName()) { + $sql[] = 'UPDATE ' . _DB_PREFIX_ . 'order_state SET deleted = 1 WHERE module_name = "' . pSQL($moduleName) . '";'; + } + return $sql; } } diff --git a/src/Install/Uninstall.php b/src/Install/Uninstall.php index e741c39cc..526dd4274 100644 --- a/src/Install/Uninstall.php +++ b/src/Install/Uninstall.php @@ -91,7 +91,27 @@ private function deleteConfig() Config::MOLLIE_API_KEY_TEST, ]; - $this->deleteConfigurations($configurations); + $orderStateConfigurations = [ + Config::MOLLIE_STATUS_PARTIAL_REFUND, + Config::MOLLIE_STATUS_AWAITING, + Config::MOLLIE_STATUS_PARTIALLY_SHIPPED, + Config::MOLLIE_STATUS_ORDER_COMPLETED, + Config::MOLLIE_STATUS_KLARNA_AUTHORIZED, + Config::MOLLIE_STATUS_KLARNA_SHIPPED, + Config::MOLLIE_STATUS_CHARGEBACK, + Config::MOLLIE_STATUS_OPEN, + Config::MOLLIE_STATUS_PAID, + Config::MOLLIE_STATUS_COMPLETED, + Config::MOLLIE_STATUS_CANCELED, + Config::MOLLIE_STATUS_EXPIRED, + Config::MOLLIE_STATUS_REFUNDED, + Config::MOLLIE_STATUS_SHIPPING, + Config::MOLLIE_STATUS_DEFAULT, + ]; + + $this->deleteConfigurations( + array_merge($configurations, $orderStateConfigurations) + ); } private function deleteConfigurations(array $configurations) diff --git a/src/ServiceProvider/BaseServiceProvider.php b/src/ServiceProvider/BaseServiceProvider.php index 807ca1605..6c86d31c4 100644 --- a/src/ServiceProvider/BaseServiceProvider.php +++ b/src/ServiceProvider/BaseServiceProvider.php @@ -106,7 +106,8 @@ public function register(Container $container) $this->addService($container, MolCustomerRepository::class, MolCustomerRepository::class) ->withArgument('MolCustomer'); - $this->addService($container, UninstallerInterface::class, Mollie\Install\DatabaseTableUninstaller::class); + $this->addService($container, UninstallerInterface::class, Mollie\Install\DatabaseTableUninstaller::class) + ->withArgument($container->get(ModuleFactory::class)); $this->addService($container, InstallerInterface::class, Installer::class) ->withArguments([