Completed
Push — master ( 9097d5...0ce82e )
by Florian
06:13
created

UpgradeData::convertSerializedDataToJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Setup;
28
29
use Magento\Framework\Setup\ModuleContextInterface;
30
use Magento\Framework\Setup\ModuleDataSetupInterface;
31
use Magento\Framework\Setup\UpgradeDataInterface;
32
use Magento\Sales\Setup\SalesSetupFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Setup\SalesSetupFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
use Payone\Core\Helper\Shop;
34
35
/**
36
 * Class UpgradeData
37
 */
38
class UpgradeData implements UpgradeDataInterface
39
{
40
    /**
41
     * Sales setup factory
42
     *
43
     * @var SalesSetupFactory
44
     */
45
    protected $salesSetupFactory;
46
47
    /**
48
     * PAYONE shop helper object
49
     *
50
     * @var Shop
51
     */
52
    protected $shopHelper;
53
54
    /**
55
     * Constructor
56
     *
57
     * @param SalesSetupFactory $salesSetupFactory
58
     * @param Shop              $shopHelper
59
     */
60
    public function __construct(SalesSetupFactory $salesSetupFactory, Shop $shopHelper)
61
    {
62
        $this->salesSetupFactory = $salesSetupFactory;
63
        $this->shopHelper = $shopHelper;
64
    }
65
66
    /**
67
     * Upgrade method
68
     *
69
     * @param  ModuleDataSetupInterface $setup
70
     * @param  ModuleContextInterface   $context
71
     * @return void
72
     */
73
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
74
    {
75
        $setup->startSetup();
76
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_reference')) {
77
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
78
            $salesInstaller->addAttribute(
79
                'order',
80
                'payone_clearing_reference',
81
                ['type' => 'varchar', 'length' => 64, 'default' => '']
82
            );
83
        }
84
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_workorder_id')) {
85
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
86
            $salesInstaller->addAttribute(
87
                'order',
88
                'payone_workorder_id',
89
                ['type' => 'varchar', 'length' => 64, 'default' => '']
90
            );
91
        }
92
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_installment_duration')) {
93
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
94
            $salesInstaller->addAttribute(
95
                'order',
96
                'payone_installment_duration',
97
                ['type' => 'integer', 'length' => null]
98
            );
99
        }
100
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccountholder')) {
101
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
102
            $salesInstaller->addAttribute(
103
                'order',
104
                'payone_clearing_bankaccountholder',
105
                ['type' => 'varchar', 'length' => 64, 'default' => '']
106
            );
107
        }
108
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcountry')) {
109
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
110
            $salesInstaller->addAttribute(
111
                'order',
112
                'payone_clearing_bankcountry',
113
                ['type' => 'varchar', 'length' => 2, 'default' => '']
114
            );
115
        }
116
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccount')) {
117
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
118
            $salesInstaller->addAttribute(
119
                'order',
120
                'payone_clearing_bankaccount',
121
                ['type' => 'varchar', 'length' => 32, 'default' => '']
122
            );
123
        }
124
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcode')) {
125
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
126
            $salesInstaller->addAttribute(
127
                'order',
128
                'payone_clearing_bankcode',
129
                ['type' => 'varchar', 'length' => 32, 'default' => '']
130
            );
131
        }
132
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankiban')) {
133
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
134
            $salesInstaller->addAttribute(
135
                'order',
136
                'payone_clearing_bankiban',
137
                ['type' => 'varchar', 'length' => 64, 'default' => '']
138
            );
139
        }
140
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankbic')) {
141
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
142
            $salesInstaller->addAttribute(
143
                'order',
144
                'payone_clearing_bankbic',
145
                ['type' => 'varchar', 'length' => 32, 'default' => '']
146
            );
147
        }
148
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcity')) {
149
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
150
            $salesInstaller->addAttribute(
151
                'order',
152
                'payone_clearing_bankcity',
153
                ['type' => 'varchar', 'length' => 64, 'default' => '']
154
            );
155
        }
156
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankname')) {
157
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
158
            $salesInstaller->addAttribute(
159
                'order',
160
                'payone_clearing_bankname',
161
                ['type' => 'varchar', 'length' => 64, 'default' => '']
162
            );
163
        }
164
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_refund_iban')) {
165
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
166
            $salesInstaller->addAttribute(
167
                'order',
168
                'payone_refund_iban',
169
                ['type' => 'varchar', 'length' => 64, 'default' => '']
170
            );
171
        }
172
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_refund_bic')) {
173
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
174
            $salesInstaller->addAttribute(
175
                'order',
176
                'payone_refund_bic',
177
                ['type' => 'varchar', 'length' => 64, 'default' => '']
178
            );
179
        }
180
181
        $serializedRows = $this->getSerializedConfigRows($setup);
182
        if (!empty($serializedRows) && version_compare($this->shopHelper->getMagentoVersion(), '2.2.0', '>=')) {
183
            $this->convertSerializedDataToJson($setup, $serializedRows);
184
        }
185
        $setup->endSetup();
186
    }
187
188
    /**
189
     * Fetch all serialized config rows from the payone module from the DB
190
     *
191
     * @param  ModuleDataSetupInterface $setup
192
     * @return array
193
     */
194
    protected function getSerializedConfigRows(ModuleDataSetupInterface $setup)
195
    {
196
        $select = $setup->getConnection()
197
            ->select()
198
            ->from($setup->getTable('core_config_data'), ['config_id', 'value'])
0 ignored issues
show
Unused Code introduced by
The call to Magento\Framework\DB\Select::from() has too many arguments starting with $setup->getTable('core_config_data'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
            ->/** @scrutinizer ignore-call */ from($setup->getTable('core_config_data'), ['config_id', 'value'])

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
199
            ->where('path LIKE "%payone%"')
200
            ->where('value LIKE "a:%"');
201
202
        return $setup->getConnection()->fetchAssoc($select);
203
    }
204
205
    /**
206
     * Convert serialized data to json-encoded data in the core_config_data table
207
     *
208
     * @param ModuleDataSetupInterface $setup
209
     * @param array                    $serializedRows
210
     * @return void
211
     */
212
    protected function convertSerializedDataToJson(ModuleDataSetupInterface $setup, $serializedRows)
213
    {
214
        foreach ($serializedRows as $id => $serializedRow) {
215
            $aValue = unserialize($serializedRow['value']);
216
            $sNewValue = json_encode($aValue);
217
218
            $data = ['value' => $sNewValue];
219
            $where = ['config_id = ?' => $id];
220
            $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
221
        }
222
    }
223
}
224