UpgradeData.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * This file is part of the Klarna Core module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Core\Setup;
  11. use Magento\Framework\Setup\ModuleContextInterface;
  12. use Magento\Framework\Setup\ModuleDataSetupInterface;
  13. use Magento\Framework\Setup\UpgradeDataInterface;
  14. /**
  15. * Class UpgradeData
  16. *
  17. * @package Klarna\Core\Setup
  18. */
  19. class UpgradeData implements UpgradeDataInterface
  20. {
  21. /**
  22. * Upgrades DB data for a module
  23. *
  24. * @param ModuleDataSetupInterface $installer
  25. * @param ModuleContextInterface $context
  26. * @return void
  27. */
  28. public function upgrade(ModuleDataSetupInterface $installer, ModuleContextInterface $context)
  29. {
  30. $installer->startSetup();
  31. if (version_compare($context->getVersion(), '2.0.0', '<')) {
  32. $this->updateKlarnaKcoApiPathKeys($installer);
  33. }
  34. if (version_compare($context->getVersion(), '4.0.7', '<')) {
  35. $this->updateKlarnaApiVersion($installer);
  36. }
  37. $installer->endSetup();
  38. }
  39. /**
  40. * Updating the klarna api version
  41. *
  42. * @param ModuleDataSetupInterface $installer
  43. */
  44. private function updateKlarnaApiVersion(ModuleDataSetupInterface $installer)
  45. {
  46. $configTable = $installer->getTable('core_config_data');
  47. $installer->getConnection()->forUpdate(
  48. "INSERT INTO ($configTable) (`scope`, `scope_id`, `path`, `value`) "
  49. . "SELECT 'websites', `scope_id`, `path`, `value` FROM `($configTable)` AS `b` "
  50. . "WHERE `path`='klarna/api/api_version' AND `scope`='stores' "
  51. . "ON DUPLICATE KEY UPDATE `value`=`b`.`value`;"
  52. );
  53. $installer->getConnection()->delete($configTable, "`path`='klarna/api/api_version' AND `scope`='stores'");
  54. }
  55. /**
  56. * Updating the klarna api path keys
  57. *
  58. * @param ModuleDataSetupInterface $installer
  59. */
  60. private function updateKlarnaKcoApiPathKeys(ModuleDataSetupInterface $installer)
  61. {
  62. $configTable = $installer->getTable('core_config_data');
  63. $oldKeys = [
  64. 'payment/klarna_kco/merchant_id',
  65. 'payment/klarna_kco/shared_secret',
  66. 'payment/klarna_kco/api_version',
  67. 'payment/klarna_kco/test_mode',
  68. 'payment/klarna_kco/debug',
  69. ];
  70. $newKeys = [
  71. 'klarna/api/merchant_id',
  72. 'klarna/api/shared_secret',
  73. 'klarna/api/api_version',
  74. 'klarna/api/test_mode',
  75. 'klarna/api/debug',
  76. ];
  77. foreach ($oldKeys as $id => $oldKey) {
  78. $newKey = $newKeys[$id];
  79. $installer->getConnection()->update($configTable, ['path' => $newKey], "`path`='{$oldKey}'");
  80. }
  81. $keys = '\'' . implode('\',\'', $oldKeys) . '\'';
  82. $keys = str_replace('klarna_kco', 'klarna_kp', $keys);
  83. $installer->getConnection()->delete($configTable, "`path` in ({$keys})");
  84. }
  85. }