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..8f08c61 --- /dev/null +++ b/Setup/UpgradeData.php @@ -0,0 +1,115 @@ +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(); + } +}