Passed
Pull Request — master (#568)
by
unknown
02:57
created

UpgradeData::addPaymentInactiveConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
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
238
        $serializedRows = $this->getSerializedConfigRows($setup);
239
        if (!empty($serializedRows) && version_compare($this->shopHelper->getMagentoVersion(), '2.2.0', '>=')) {
240
            $this->convertSerializedDataToJson($setup, $serializedRows);
241
        }
242
243
        if (version_compare($context->getVersion(), '2.2.0', '<=')) {// pre update version is less than or equal to 2.2.1
244
            $this->convertPersonstatusMappingConfig($setup);
245
        }
246
247
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_express_type')) {
248
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
249
            $salesInstaller->addAttribute(
250
                'order',
251
                'payone_express_type',
252
                ['type' => 'varchar', 'length' => 64, 'default' => '']
253
            );
254
        }
255
256
        $customerInstaller = $this->customerSetupFactory->create(['setup' => $setup]);
257
        if (!$customerInstaller->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'payone_paydirekt_registered', 'attribute_id')) {
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Model\Customer 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...
258
            $customerInstaller->addAttribute(
259
                'customer',
260
                'payone_paydirekt_registered',
261
                [
262
                    'type'         => 'int',
263
                    'label'        => 'Payone paydirekt OneClick is registered',
264
                    'input'        => 'text',
265
                    'required'     => false,
266
                    'visible'      => false,
267
                    'user_defined' => true,
268
                    'sort_order'   => 999,
269
                    'position'     => 999,
270
                    'system'       => 0,
271
                ]
272
            );
273
        }
274
275
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_ratepay_shop_id')) {
276
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
277
            $salesInstaller->addAttribute(
278
                'order',
279
                'payone_ratepay_shop_id',
280
                ['type' => 'varchar', 'length' => 32, 'default' => '']
281
            );
282
        }
283
284
        if (version_compare($context->getVersion(), '2.8.0', '<=')) { // pre update version is less than or equal to 2.8.0
285
            $this->convertCreditcardTypesConfig($setup);
286
        }
287
288
        $setup->endSetup();
289
    }
290
291
    /**
292
     * Fetch all serialized config rows from the payone module from the DB
293
     *
294
     * @param  ModuleDataSetupInterface $setup
295
     * @return array
296
     */
297
    protected function getSerializedConfigRows(ModuleDataSetupInterface $setup)
298
    {
299
        $select = $setup->getConnection()
300
            ->select()
301
            ->from($setup->getTable('core_config_data'), ['config_id', 'value'])
302
            ->where('path LIKE "%payone%"')
303
            ->where('value LIKE "a:%"');
304
305
        return $setup->getConnection()->fetchAssoc($select);
306
    }
307
308
    /**
309
     * Convert serialized data to json-encoded data in the core_config_data table
310
     *
311
     * @param ModuleDataSetupInterface $setup
312
     * @param array                    $serializedRows
313
     * @return void
314
     */
315
    protected function convertSerializedDataToJson(ModuleDataSetupInterface $setup, $serializedRows)
316
    {
317
        foreach ($serializedRows as $id => $serializedRow) {
318
            $aValue = unserialize($serializedRow['value']);
319
            $sNewValue = json_encode($aValue);
320
321
            $data = ['value' => $sNewValue];
322
            $where = ['config_id = ?' => $id];
323
            $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
324
        }
325
    }
326
327
    /**
328
     * Change config path of personstatus mapping configuration
329
     *
330
     * @param  ModuleDataSetupInterface $setup
331
     * @return void
332
     */
333
    protected function convertPersonstatusMappingConfig(ModuleDataSetupInterface $setup)
334
    {
335
        $data = ['path' => 'payone_protect/personstatus/mapping'];
336
        $where = ['path = ?' => 'payone_protect/address_check/mapping_personstatus'];
337
        $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
338
    }
339
340
    /**
341
     * Updates configured creditcard types from old data handling to new data handling
342
     *
343
     * @param  ModuleDataSetupInterface $setup
344
     * @return void
345
     */
346
    protected function convertCreditcardTypesConfig(ModuleDataSetupInterface $setup)
347
    {
348
        $select = $setup->getConnection()
349
            ->select()
350
            ->from($setup->getTable('core_config_data'), ['config_id', 'value'])
351
            ->where('path = "payone_payment/payone_creditcard/types"');
352
        $result = $setup->getConnection()->fetchAssoc($select);
353
354
        $cardTypes = CreditcardTypes::getCreditcardTypes();
355
        foreach ($result as $row) {
356
            $newCardTypes = [];
357
            $activatedCardtypes = explode(',', $row['value'] ?? '');
358
            foreach ($activatedCardtypes as $activeCardType) {
359
                if ($activeCardType == "C") {
360
                    $newCardTypes[] = "discover";
361
                } elseif ($activeCardType == "D") {
362
                    $newCardTypes[] = "dinersclub";
363
                } else {
364
                    foreach ($cardTypes as $cardTypeId => $cardType) {
365
                        if ($cardType['cardtype'] == $activeCardType) {
366
                            $newCardTypes[] = $cardTypeId;
367
                        }
368
                    }
369
                }
370
            }
371
372
            $data = ['value' => implode(",", $newCardTypes)];
373
            $where = ['config_id = ?' => $row['config_id']];
374
            $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
375
        }
376
    }
377
}
378