UpgradeData::upgrade()   F
last analyzed

Complexity

Conditions 22
Paths > 20000

Size

Total Lines 158
Code Lines 112

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 112
nc 1048576
nop 2
dl 0
loc 158
rs 0
c 0
b 0
f 0

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;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Setup\ModuleContextInterface 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...
30
use Magento\Framework\Setup\ModuleDataSetupInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Setup\ModuleDataSetupInterface 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...
31
use Magento\Framework\Setup\UpgradeDataInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Setup\UpgradeDataInterface 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...
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 Magento\Framework\App\Config\Storage\WriterInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Co...Storage\WriterInterface 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...
34
use Magento\Store\Model\StoreManagerInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface 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...
35
use Payone\Core\Helper\Shop;
36
use Magento\Customer\Setup\CustomerSetupFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Setup\CustomerSetupFactory 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...
37
use Payone\Core\Helper\Payment;
38
use Magento\Store\Model\ScopeInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\ScopeInterface 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...
39
use Payone\Core\Model\PayoneConfig;
40
use Payone\Core\Model\Source\CreditcardTypes;
41
42
/**
43
 * Class UpgradeData
44
 */
45
class UpgradeData implements UpgradeDataInterface
46
{
47
    /**
48
     * Sales setup factory
49
     *
50
     * @var SalesSetupFactory
51
     */
52
    protected $salesSetupFactory;
53
54
    /**
55
     * Config writer resource
56
     *
57
     * @var WriterInterface
58
     */
59
    protected $configWriter;
60
61
    /**
62
     * Store manager object
63
     *
64
     * @var StoreManagerInterface
65
     */
66
    protected $storeManager;
67
68
    /**
69
     * PAYONE shop helper object
70
     *
71
     * @var Shop
72
     */
73
    protected $shopHelper;
74
75
    /**
76
     * Eav setup factory
77
     *
78
     * @var CustomerSetupFactory
79
     */
80
    protected $customerSetupFactory;
81
82
    /**
83
     * PAYONE payment helper object
84
     *
85
     * @var Payment
86
     */
87
    protected $paymentHelper;
88
89
    /**
90
     * Constructor
91
     *
92
     * @param SalesSetupFactory     $salesSetupFactory
93
     * @param Shop                  $shopHelper
94
     * @param Payment               $paymentHelper
95
     * @param WriterInterface       $configWriter
96
     * @param StoreManagerInterface $storeManager
97
     * @param CustomerSetupFactory  $customerSetupFactory
98
     */
99
    public function __construct(
100
        SalesSetupFactory $salesSetupFactory,
101
        Shop $shopHelper,
102
        Payment $paymentHelper,
103
        WriterInterface $configWriter,
104
        StoreManagerInterface $storeManager,
105
        CustomerSetupFactory $customerSetupFactory
106
    ) {
107
        $this->salesSetupFactory = $salesSetupFactory;
108
        $this->shopHelper = $shopHelper;
109
        $this->paymentHelper = $paymentHelper;
110
        $this->configWriter = $configWriter;
111
        $this->storeManager = $storeManager;
112
        $this->customerSetupFactory = $customerSetupFactory;
113
    }
114
115
    /**
116
     * Upgrade method
117
     *
118
     * @param  ModuleDataSetupInterface $setup
119
     * @param  ModuleContextInterface   $context
120
     * @return void
121
     */
122
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
123
    {
124
        $setup->startSetup();
125
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_reference')) {
126
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
127
            $salesInstaller->addAttribute(
128
                'order',
129
                'payone_clearing_reference',
130
                ['type' => 'varchar', 'length' => 64, 'default' => '']
131
            );
132
        }
133
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_workorder_id')) {
134
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
135
            $salesInstaller->addAttribute(
136
                'order',
137
                'payone_workorder_id',
138
                ['type' => 'varchar', 'length' => 64, 'default' => '']
139
            );
140
        }
141
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_installment_duration')) {
142
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
143
            $salesInstaller->addAttribute(
144
                'order',
145
                'payone_installment_duration',
146
                ['type' => 'integer', 'length' => null]
147
            );
148
        }
149
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccountholder')) {
150
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
151
            $salesInstaller->addAttribute(
152
                'order',
153
                'payone_clearing_bankaccountholder',
154
                ['type' => 'varchar', 'length' => 64, 'default' => '']
155
            );
156
        }
157
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcountry')) {
158
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
159
            $salesInstaller->addAttribute(
160
                'order',
161
                'payone_clearing_bankcountry',
162
                ['type' => 'varchar', 'length' => 2, 'default' => '']
163
            );
164
        }
165
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccount')) {
166
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
167
            $salesInstaller->addAttribute(
168
                'order',
169
                'payone_clearing_bankaccount',
170
                ['type' => 'varchar', 'length' => 32, 'default' => '']
171
            );
172
        }
173
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcode')) {
174
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
175
            $salesInstaller->addAttribute(
176
                'order',
177
                'payone_clearing_bankcode',
178
                ['type' => 'varchar', 'length' => 32, 'default' => '']
179
            );
180
        }
181
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankiban')) {
182
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
183
            $salesInstaller->addAttribute(
184
                'order',
185
                'payone_clearing_bankiban',
186
                ['type' => 'varchar', 'length' => 64, 'default' => '']
187
            );
188
        }
189
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankbic')) {
190
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
191
            $salesInstaller->addAttribute(
192
                'order',
193
                'payone_clearing_bankbic',
194
                ['type' => 'varchar', 'length' => 32, 'default' => '']
195
            );
196
        }
197
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcity')) {
198
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
199
            $salesInstaller->addAttribute(
200
                'order',
201
                'payone_clearing_bankcity',
202
                ['type' => 'varchar', 'length' => 64, 'default' => '']
203
            );
204
        }
205
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankname')) {
206
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
207
            $salesInstaller->addAttribute(
208
                'order',
209
                'payone_clearing_bankname',
210
                ['type' => 'varchar', 'length' => 64, 'default' => '']
211
            );
212
        }
213
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_refund_iban')) {
214
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
215
            $salesInstaller->addAttribute(
216
                'order',
217
                'payone_refund_iban',
218
                ['type' => 'varchar', 'length' => 64, 'default' => '']
219
            );
220
        }
221
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_refund_bic')) {
222
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
223
            $salesInstaller->addAttribute(
224
                'order',
225
                'payone_refund_bic',
226
                ['type' => 'varchar', 'length' => 64, 'default' => '']
227
            );
228
        }
229
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_cancel_substitute_increment_id')) {
230
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
231
            $salesInstaller->addAttribute(
232
                'order',
233
                'payone_cancel_substitute_increment_id',
234
                ['type' => 'varchar', 'length' => 64, 'default' => '']
235
            );
236
        }
237
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_duedate')) {
238
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
239
            $salesInstaller->addAttribute(
240
                'order',
241
                'payone_clearing_duedate',
242
                ['type' => 'varchar', 'length' => 32, 'default' => '']
243
            );
244
        }
245
246
        $serializedRows = $this->getSerializedConfigRows($setup);
247
        if (!empty($serializedRows) && version_compare($this->shopHelper->getMagentoVersion(), '2.2.0', '>=')) {
248
            $this->convertSerializedDataToJson($setup, $serializedRows);
249
        }
250
251
        if (version_compare($context->getVersion(), '2.2.0', '<=')) {// pre update version is less than or equal to 2.2.1
252
            $this->convertPersonstatusMappingConfig($setup);
253
        }
254
255
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_express_type')) {
256
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
257
            $salesInstaller->addAttribute(
258
                'order',
259
                'payone_express_type',
260
                ['type' => 'varchar', 'length' => 64, 'default' => '']
261
            );
262
        }
263
264
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_ratepay_shop_id')) {
265
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
266
            $salesInstaller->addAttribute(
267
                'order',
268
                'payone_ratepay_shop_id',
269
                ['type' => 'varchar', 'length' => 32, 'default' => '']
270
            );
271
        }
272
273
        $this->deactivateNewPaymentMethods($setup);
274
275
        if (version_compare($context->getVersion(), '2.8.0', '<=')) { // pre update version is less than or equal to 2.8.0
276
            $this->convertCreditcardTypesConfig($setup);
277
        }
278
279
        $setup->endSetup();
280
    }
281
282
    /**
283
     * Fetch all serialized config rows from the payone module from the DB
284
     *
285
     * @param  ModuleDataSetupInterface $setup
286
     * @return array
287
     */
288
    protected function getSerializedConfigRows(ModuleDataSetupInterface $setup)
289
    {
290
        $select = $setup->getConnection()
291
            ->select()
292
            ->from($setup->getTable('core_config_data'), ['config_id', 'value'])
293
            ->where('path LIKE "%payone%"')
294
            ->where('value LIKE "a:%"');
295
296
        return $setup->getConnection()->fetchAssoc($select);
297
    }
298
299
    /**
300
     * Convert serialized data to json-encoded data in the core_config_data table
301
     *
302
     * @param ModuleDataSetupInterface $setup
303
     * @param array                    $serializedRows
304
     * @return void
305
     */
306
    protected function convertSerializedDataToJson(ModuleDataSetupInterface $setup, $serializedRows)
307
    {
308
        foreach ($serializedRows as $id => $serializedRow) {
309
            $aValue = unserialize($serializedRow['value']);
310
            $sNewValue = json_encode($aValue);
311
312
            $data = ['value' => $sNewValue];
313
            $where = ['config_id = ?' => $id];
314
            $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
315
        }
316
    }
317
318
    /**
319
     * Change config path of personstatus mapping configuration
320
     *
321
     * @param  ModuleDataSetupInterface $setup
322
     * @return void
323
     */
324
    protected function convertPersonstatusMappingConfig(ModuleDataSetupInterface $setup)
325
    {
326
        $data = ['path' => 'payone_protect/personstatus/mapping'];
327
        $where = ['path = ?' => 'payone_protect/address_check/mapping_personstatus'];
328
        $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
329
    }
330
331
    /**
332
     * Updates configured creditcard types from old data handling to new data handling
333
     *
334
     * @param  ModuleDataSetupInterface $setup
335
     * @return void
336
     */
337
    protected function convertCreditcardTypesConfig(ModuleDataSetupInterface $setup)
338
    {
339
        $select = $setup->getConnection()
340
            ->select()
341
            ->from($setup->getTable('core_config_data'), ['config_id', 'value'])
342
            ->where('path = "payone_payment/payone_creditcard/types"');
343
        $result = $setup->getConnection()->fetchAssoc($select);
344
345
        $cardTypes = CreditcardTypes::getCreditcardTypes();
346
        foreach ($result as $row) {
347
            $newCardTypes = [];
348
            $activatedCardtypes = explode(',', $row['value'] ?? '');
349
            foreach ($activatedCardtypes as $activeCardType) {
350
                if ($activeCardType == "C") {
351
                    $newCardTypes[] = "discover";
352
                } elseif ($activeCardType == "D") {
353
                    $newCardTypes[] = "dinersclub";
354
                } else {
355
                    foreach ($cardTypes as $cardTypeId => $cardType) {
356
                        if ($cardType['cardtype'] == $activeCardType) {
357
                            $newCardTypes[] = $cardTypeId;
358
                        }
359
                    }
360
                }
361
            }
362
363
            $data = ['value' => implode(",", $newCardTypes)];
364
            $where = ['config_id = ?' => $row['config_id']];
365
            $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
366
        }
367
    }
368
369
    /**
370
     * Adds a config entry to the database to set the payment method to inactive
371
     *
372
     * @param  string $methodCode
373
     * @return void
374
     */
375
    protected function addPaymentInactiveConfig($methodCode)
376
    {
377
        if ($methodCode == PayoneConfig::METHOD_KLARNA_BASE) { // Klarna base has to stay active
378
            return;
379
        }
380
        $this->configWriter->save('payment/'.$methodCode.'/active', 0);
381
    }
382
383
    /**
384
     * Checks if there is a active config entry for the given payment method
385
     *
386
     * @param  ModuleDataSetupInterface $setup
387
     * @param  string                   $methodCode
388
     * @return bool
389
     */
390
    protected function isPaymentConfigExisting(ModuleDataSetupInterface $setup, $methodCode)
391
    {
392
        $select = $setup->getConnection()
393
            ->select()
394
            ->from($setup->getTable('core_config_data'), ['config_id', 'value'])
395
            ->where('path LIKE "%'.$methodCode.'/active"');
396
397
        $result = $setup->getConnection()->fetchAssoc($select);
398
        if (!empty($result)) {
399
            return true;
400
        }
401
        return false;
402
    }
403
404
    /**
405
     * Deactivates new payment methods, since they have to be marked as active in the config.xml files to be shown in payment method select backend elements
406
     *
407
     * @param  ModuleDataSetupInterface $setup
408
     * @return void
409
     */
410
    protected function deactivateNewPaymentMethods(ModuleDataSetupInterface $setup)
411
    {
412
        foreach ($this->paymentHelper->getAvailablePaymentTypes() as $methodCode) {
413
            if ($this->isPaymentConfigExisting($setup, $methodCode) === false) {
414
                $this->addPaymentInactiveConfig($methodCode);
415
            }
416
        }
417
    }
418
}
419