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