Completed
Push — master ( beb410...dc9ff4 )
by Florian
13s
created

UpgradeData::upgrade()   F

Complexity

Conditions 16
Paths 16384

Size

Total Lines 113
Code Lines 84

Duplication

Lines 80
Ratio 70.8 %

Importance

Changes 0
Metric Value
dl 80
loc 113
rs 2
c 0
b 0
f 0
cc 16
eloc 84
nc 16384
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_reference')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_workorder_id')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccountholder')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcountry')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccount')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcode')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankiban')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankbic')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcity')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankname')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        if (version_compare($this->shopHelper->getMagentoVersion(), '2.2.0', '>=') &&
181
            version_compare($context->getVersion(), '2.0.2', '<')) {
182
            $this->convertSerializedDataToJson($setup);
183
        }
184
        $setup->endSetup();
185
    }
186
187
    /**
188
     * Convert serialized data to json-encoded data in the core_config_data table
189
     *
190
     * @param ModuleDataSetupInterface $setup
191
     * @return void
192
     */
193
    private function convertSerializedDataToJson(ModuleDataSetupInterface $setup)
194
    {
195
        $sTable = $setup->getTable('core_config_data');
196
197
        $select = $setup->getConnection()
198
            ->select()
199
            ->from($sTable, ['config_id', 'value'])
200
            ->where('path LIKE "%payone%"')
201
            ->where('value LIKE "a:%"');
202
203
        $serializedRows = $setup->getConnection()->fetchAssoc($select);
204
        foreach ($serializedRows as $id => $serializedRow) {
205
            $aValue = unserialize($serializedRow['value']);
206
            $sNewValue = json_encode($aValue);
207
208
            $data = ['value' => $sNewValue];
209
            $where = ['config_id = ?' => $id];
210
            $setup->getConnection()->update($sTable, $data, $where);
211
        }
212
    }
213
}
214