Completed
Push — master ( dc9ff4...224b77 )
by Florian
12s
created

UpgradeData   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 186
Duplicated Lines 43.01 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 1
dl 80
loc 186
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
F upgrade() 80 114 16
A getSerializedConfigRows() 0 10 1
A convertSerializedDataToJson() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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'])
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