From 2148b10413645e64e31b65831acc2cb52f4c2d02 Mon Sep 17 00:00:00 2001 From: Md Shahriar Siraj Date: Tue, 4 Feb 2020 14:52:26 +0100 Subject: [PATCH 1/2] Adds support for Magento 2.2 or up --- .../Data/AddSwedbankPayOrderStatuses.php | 76 ------------ Setup/UpgradeData.php | 114 ++++++++++++++++++ 2 files changed, 114 insertions(+), 76 deletions(-) delete mode 100644 Setup/Patch/Data/AddSwedbankPayOrderStatuses.php create mode 100644 Setup/UpgradeData.php diff --git a/Setup/Patch/Data/AddSwedbankPayOrderStatuses.php b/Setup/Patch/Data/AddSwedbankPayOrderStatuses.php deleted file mode 100644 index e3cf560..0000000 --- a/Setup/Patch/Data/AddSwedbankPayOrderStatuses.php +++ /dev/null @@ -1,76 +0,0 @@ -moduleDataSetup = $moduleDataSetup; - } - - /** - * {@inheritdoc} - */ - public function apply() - { - /** - * Prepare database for install - */ - $this->moduleDataSetup->getConnection()->startSetup(); - - $data = []; - $statuses = [ - 'swedbank_pay_pending' => __('SwedbankPay Pending'), - 'swedbank_pay_reversed' => __('SwedbankPay Reversed'), - 'swedbank_pay_cancelled_reversal' => __('SwedbankPay Cancelled Reversal'), - ]; - - foreach ($statuses as $code => $info) { - $data[] = ['status' => $code, 'label' => $info]; - } - - $this->moduleDataSetup->getConnection()->insertArray( - $this->moduleDataSetup->getTable('sales_order_status'), - ['status', 'label'], - $data - ); - - /** - * Prepare database after install - */ - $this->moduleDataSetup->getConnection()->endSetup(); - } - - /** - * {@inheritdoc} - */ - public static function getDependencies() - { - return []; - } - - /** - * {@inheritdoc} - */ - public function getAliases() - { - return []; - } -} diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php new file mode 100644 index 0000000..9a8e89d --- /dev/null +++ b/Setup/UpgradeData.php @@ -0,0 +1,114 @@ +statusFactory = $statusFactory; + $this->statusResourceModel = $statusResourceModel; + $this->statusCollectionFactory = $statusCollectionFactory; + } + + /** + * Upgrades DB for a module + * + * @param ModuleDataSetupInterface $setup + * @param ModuleContextInterface $context + * @return void + * @throws AlreadyExistsException + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $setup->startSetup(); + + $this->createCustomStatuses(); + + $setup->endSetup(); + } + + /** + * @throws AlreadyExistsException + * @throws Exception + */ + protected function createCustomStatuses() + { + $existingStatuses = $this->getExistingStatuses(); + + foreach ($this->getStatuses() as $status => $label) { + if (!array_key_exists($status, $existingStatuses)) { + /** @var \Magento\Sales\Model\Order\Status $status */ + $newStatus = $this->statusFactory->create(); + + $newStatus->setData(['label' => $label, 'status' => $status]); + + $this->statusResourceModel->save($newStatus); + } + } + } + + /** + * Get custom order statuses + * + * @return array + */ + protected function getStatuses() + { + return [ + 'swedbank_pay_pending' => __('SwedbankPay Pending'), + 'swedbank_pay_reversed' => __('SwedbankPay Reversed'), + 'swedbank_pay_cancelled_reversal' => __('SwedbankPay Cancelled Reversal'), + ]; + } + + /** + * Get existing order statuses + * + * @return array + */ + public function getExistingStatuses() + { + return $this->statusCollectionFactory->create()->getItems(); + } +} From cdc18000332e41702ffcd05399f65ca3c76963b1 Mon Sep 17 00:00:00 2001 From: Shahriar Siraj Date: Tue, 4 Feb 2020 15:17:49 +0100 Subject: [PATCH 2/2] Adds class comment --- Setup/UpgradeData.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php index 9a8e89d..8f08c61 100644 --- a/Setup/UpgradeData.php +++ b/Setup/UpgradeData.php @@ -13,6 +13,7 @@ /** * Class UpgradeData + * Creates custom order statuses * * @SuppressWarnings(PHPMD.LongVariable) */