Passed
Push — master ( fc4cd0...470652 )
by Hannes
03:43 queued 11s
created

UpgradeData::addPaymentInactiveConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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;
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 Payone\Core\Helper\Payment;
37
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...
38
39
/**
40
 * Class UpgradeData
41
 */
42
class UpgradeData implements UpgradeDataInterface
43
{
44
    /**
45
     * Sales setup factory
46
     *
47
     * @var SalesSetupFactory
48
     */
49
    protected $salesSetupFactory;
50
51
    /**
52
     * Config writer resource
53
     *
54
     * @var WriterInterface
55
     */
56
    protected $configWriter;
57
58
    /**
59
     * Store manager object
60
     *
61
     * @var StoreManagerInterface
62
     */
63
    protected $storeManager;
64
65
    /**
66
     * PAYONE shop helper object
67
     *
68
     * @var Shop
69
     */
70
    protected $shopHelper;
71
72
    /**
73
     * PAYONE payment helper object
74
     *
75
     * @var Payment
76
     */
77
    protected $paymentHelper;
78
79
    /**
80
     * Constructor
81
     *
82
     * @param SalesSetupFactory     $salesSetupFactory
83
     * @param Shop                  $shopHelper
84
     * @param Payment               $paymentHelper
85
     * @param WriterInterface       $configWriter
86
     * @param StoreManagerInterface $storeManager
87
     */
88
    public function __construct(
89
        SalesSetupFactory $salesSetupFactory,
90
        Shop $shopHelper,
91
        Payment $paymentHelper,
92
        WriterInterface $configWriter,
93
        StoreManagerInterface $storeManager
94
    ) {
95
        $this->salesSetupFactory = $salesSetupFactory;
96
        $this->shopHelper = $shopHelper;
97
        $this->paymentHelper = $paymentHelper;
98
        $this->configWriter = $configWriter;
99
        $this->storeManager = $storeManager;
100
    }
101
102
    /**
103
     * Upgrade method
104
     *
105
     * @param  ModuleDataSetupInterface $setup
106
     * @param  ModuleContextInterface   $context
107
     * @return void
108
     */
109
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
110
    {
111
        $setup->startSetup();
112
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_reference')) {
113
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
114
            $salesInstaller->addAttribute(
115
                'order',
116
                'payone_clearing_reference',
117
                ['type' => 'varchar', 'length' => 64, 'default' => '']
118
            );
119
        }
120
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_workorder_id')) {
121
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
122
            $salesInstaller->addAttribute(
123
                'order',
124
                'payone_workorder_id',
125
                ['type' => 'varchar', 'length' => 64, 'default' => '']
126
            );
127
        }
128
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_installment_duration')) {
129
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
130
            $salesInstaller->addAttribute(
131
                'order',
132
                'payone_installment_duration',
133
                ['type' => 'integer', 'length' => null]
134
            );
135
        }
136
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccountholder')) {
137
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
138
            $salesInstaller->addAttribute(
139
                'order',
140
                'payone_clearing_bankaccountholder',
141
                ['type' => 'varchar', 'length' => 64, 'default' => '']
142
            );
143
        }
144
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcountry')) {
145
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
146
            $salesInstaller->addAttribute(
147
                'order',
148
                'payone_clearing_bankcountry',
149
                ['type' => 'varchar', 'length' => 2, 'default' => '']
150
            );
151
        }
152
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccount')) {
153
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
154
            $salesInstaller->addAttribute(
155
                'order',
156
                'payone_clearing_bankaccount',
157
                ['type' => 'varchar', 'length' => 32, 'default' => '']
158
            );
159
        }
160
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcode')) {
161
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
162
            $salesInstaller->addAttribute(
163
                'order',
164
                'payone_clearing_bankcode',
165
                ['type' => 'varchar', 'length' => 32, 'default' => '']
166
            );
167
        }
168
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankiban')) {
169
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
170
            $salesInstaller->addAttribute(
171
                'order',
172
                'payone_clearing_bankiban',
173
                ['type' => 'varchar', 'length' => 64, 'default' => '']
174
            );
175
        }
176
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankbic')) {
177
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
178
            $salesInstaller->addAttribute(
179
                'order',
180
                'payone_clearing_bankbic',
181
                ['type' => 'varchar', 'length' => 32, 'default' => '']
182
            );
183
        }
184
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcity')) {
185
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
186
            $salesInstaller->addAttribute(
187
                'order',
188
                'payone_clearing_bankcity',
189
                ['type' => 'varchar', 'length' => 64, 'default' => '']
190
            );
191
        }
192
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankname')) {
193
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
194
            $salesInstaller->addAttribute(
195
                'order',
196
                'payone_clearing_bankname',
197
                ['type' => 'varchar', 'length' => 64, 'default' => '']
198
            );
199
        }
200
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_refund_iban')) {
201
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
202
            $salesInstaller->addAttribute(
203
                'order',
204
                'payone_refund_iban',
205
                ['type' => 'varchar', 'length' => 64, 'default' => '']
206
            );
207
        }
208
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_refund_bic')) {
209
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
210
            $salesInstaller->addAttribute(
211
                'order',
212
                'payone_refund_bic',
213
                ['type' => 'varchar', 'length' => 64, 'default' => '']
214
            );
215
        }
216
        if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_cancel_substitute_increment_id')) {
217
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
218
            $salesInstaller->addAttribute(
219
                'order',
220
                'payone_cancel_substitute_increment_id',
221
                ['type' => 'varchar', 'length' => 64, 'default' => '']
222
            );
223
        }
224
225
        $serializedRows = $this->getSerializedConfigRows($setup);
226
        if (!empty($serializedRows) && version_compare($this->shopHelper->getMagentoVersion(), '2.2.0', '>=')) {
227
            $this->convertSerializedDataToJson($setup, $serializedRows);
228
        }
229
230
        if (version_compare($context->getVersion(), '2.2.0', '<=')) {// pre update version is less than or equal to 2.2.1
231
            $this->convertPersonstatusMappingConfig($setup);
232
        }
233
234
        $this->deactivateNewPaymentMethods($setup);
235
236
        $setup->endSetup();
237
    }
238
239
    /**
240
     * Fetch all serialized config rows from the payone module from the DB
241
     *
242
     * @param  ModuleDataSetupInterface $setup
243
     * @return array
244
     */
245
    protected function getSerializedConfigRows(ModuleDataSetupInterface $setup)
246
    {
247
        $select = $setup->getConnection()
248
            ->select()
249
            ->from($setup->getTable('core_config_data'), ['config_id', 'value'])
250
            ->where('path LIKE "%payone%"')
251
            ->where('value LIKE "a:%"');
252
253
        return $setup->getConnection()->fetchAssoc($select);
254
    }
255
256
    /**
257
     * Convert serialized data to json-encoded data in the core_config_data table
258
     *
259
     * @param ModuleDataSetupInterface $setup
260
     * @param array                    $serializedRows
261
     * @return void
262
     */
263
    protected function convertSerializedDataToJson(ModuleDataSetupInterface $setup, $serializedRows)
264
    {
265
        foreach ($serializedRows as $id => $serializedRow) {
266
            $aValue = unserialize($serializedRow['value']);
267
            $sNewValue = json_encode($aValue);
268
269
            $data = ['value' => $sNewValue];
270
            $where = ['config_id = ?' => $id];
271
            $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
272
        }
273
    }
274
275
    /**
276
     * Change config path of personstatus mapping configuration
277
     *
278
     * @param  ModuleDataSetupInterface $setup
279
     * @return void
280
     */
281
    protected function convertPersonstatusMappingConfig(ModuleDataSetupInterface $setup)
282
    {
283
        $data = ['path' => 'payone_protect/personstatus/mapping'];
284
        $where = ['path = ?' => 'payone_protect/address_check/mapping_personstatus'];
285
        $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where);
286
    }
287
288
    /**
289
     * Adds a config entry to the database to set the payment method to inactive
290
     *
291
     * @param  string $methodCode
292
     * @return void
293
     */
294
    protected function addPaymentInactiveConfig($methodCode)
295
    {
296
        $this->configWriter->save('payment/'.$methodCode.'/active', 0);
297
    }
298
299
    /**
300
     * Checks if there is a active config entry for the given payment method
301
     *
302
     * @param  ModuleDataSetupInterface $setup
303
     * @param  string                   $methodCode
304
     * @return bool
305
     */
306
    protected function isPaymentConfigExisting(ModuleDataSetupInterface $setup, $methodCode)
307
    {
308
        $select = $setup->getConnection()
309
            ->select()
310
            ->from($setup->getTable('core_config_data'), ['config_id', 'value'])
311
            ->where('path LIKE "%'.$methodCode.'/active"');
312
313
        $result = $setup->getConnection()->fetchAssoc($select);
314
        if (!empty($result)) {
315
            return true;
316
        }
317
        return false;
318
    }
319
320
    /**
321
     * 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
322
     *
323
     * @param  ModuleDataSetupInterface $setup
324
     * @return void
325
     */
326
    protected function deactivateNewPaymentMethods(ModuleDataSetupInterface $setup)
327
    {
328
        foreach ($this->paymentHelper->getAvailablePaymentTypes() as $methodCode) {
329
            if ($this->isPaymentConfigExisting($setup, $methodCode) === false) {
330
                $this->addPaymentInactiveConfig($methodCode);
331
            }
332
        }
333
    }
334
}
335