| Total Complexity | 45 |
| Total Lines | 423 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UpgradeData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UpgradeData, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class UpgradeData implements UpgradeDataInterface |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * Sales setup factory |
||
| 50 | * |
||
| 51 | * @var SalesSetupFactory |
||
| 52 | */ |
||
| 53 | protected $salesSetupFactory; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Config writer resource |
||
| 57 | * |
||
| 58 | * @var WriterInterface |
||
| 59 | */ |
||
| 60 | protected $configWriter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Store manager object |
||
| 64 | * |
||
| 65 | * @var StoreManagerInterface |
||
| 66 | */ |
||
| 67 | protected $storeManager; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * PAYONE shop helper object |
||
| 71 | * |
||
| 72 | * @var Shop |
||
| 73 | */ |
||
| 74 | protected $shopHelper; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Eav setup factory |
||
| 78 | * |
||
| 79 | * @var CustomerSetupFactory |
||
| 80 | */ |
||
| 81 | protected $customerSetupFactory; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * PAYONE payment helper object |
||
| 85 | * |
||
| 86 | * @var Payment |
||
| 87 | */ |
||
| 88 | protected $paymentHelper; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Constructor |
||
| 92 | * |
||
| 93 | * @param SalesSetupFactory $salesSetupFactory |
||
| 94 | * @param Shop $shopHelper |
||
| 95 | * @param Payment $paymentHelper |
||
| 96 | * @param WriterInterface $configWriter |
||
| 97 | * @param StoreManagerInterface $storeManager |
||
| 98 | * @param CustomerSetupFactory $customerSetupFactory |
||
| 99 | */ |
||
| 100 | public function __construct( |
||
| 101 | SalesSetupFactory $salesSetupFactory, |
||
| 102 | Shop $shopHelper, |
||
| 103 | Payment $paymentHelper, |
||
| 104 | WriterInterface $configWriter, |
||
| 105 | StoreManagerInterface $storeManager, |
||
| 106 | CustomerSetupFactory $customerSetupFactory |
||
| 107 | ) { |
||
| 108 | $this->salesSetupFactory = $salesSetupFactory; |
||
| 109 | $this->shopHelper = $shopHelper; |
||
| 110 | $this->paymentHelper = $paymentHelper; |
||
| 111 | $this->configWriter = $configWriter; |
||
| 112 | $this->storeManager = $storeManager; |
||
| 113 | $this->customerSetupFactory = $customerSetupFactory; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Upgrade method |
||
| 118 | * |
||
| 119 | * @param ModuleDataSetupInterface $setup |
||
| 120 | * @param ModuleContextInterface $context |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) |
||
| 124 | { |
||
| 125 | $setup->startSetup(); |
||
| 126 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_reference')) { |
||
| 127 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 128 | $salesInstaller->addAttribute( |
||
| 129 | 'order', |
||
| 130 | 'payone_clearing_reference', |
||
| 131 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_workorder_id')) { |
||
| 135 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 136 | $salesInstaller->addAttribute( |
||
| 137 | 'order', |
||
| 138 | 'payone_workorder_id', |
||
| 139 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_installment_duration')) { |
||
| 143 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 144 | $salesInstaller->addAttribute( |
||
| 145 | 'order', |
||
| 146 | 'payone_installment_duration', |
||
| 147 | ['type' => 'integer', 'length' => null] |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccountholder')) { |
||
| 151 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 152 | $salesInstaller->addAttribute( |
||
| 153 | 'order', |
||
| 154 | 'payone_clearing_bankaccountholder', |
||
| 155 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcountry')) { |
||
| 159 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 160 | $salesInstaller->addAttribute( |
||
| 161 | 'order', |
||
| 162 | 'payone_clearing_bankcountry', |
||
| 163 | ['type' => 'varchar', 'length' => 2, 'default' => ''] |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankaccount')) { |
||
| 167 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 168 | $salesInstaller->addAttribute( |
||
| 169 | 'order', |
||
| 170 | 'payone_clearing_bankaccount', |
||
| 171 | ['type' => 'varchar', 'length' => 32, 'default' => ''] |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcode')) { |
||
| 175 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 176 | $salesInstaller->addAttribute( |
||
| 177 | 'order', |
||
| 178 | 'payone_clearing_bankcode', |
||
| 179 | ['type' => 'varchar', 'length' => 32, 'default' => ''] |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankiban')) { |
||
| 183 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 184 | $salesInstaller->addAttribute( |
||
| 185 | 'order', |
||
| 186 | 'payone_clearing_bankiban', |
||
| 187 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankbic')) { |
||
| 191 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 192 | $salesInstaller->addAttribute( |
||
| 193 | 'order', |
||
| 194 | 'payone_clearing_bankbic', |
||
| 195 | ['type' => 'varchar', 'length' => 32, 'default' => ''] |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankcity')) { |
||
| 199 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 200 | $salesInstaller->addAttribute( |
||
| 201 | 'order', |
||
| 202 | 'payone_clearing_bankcity', |
||
| 203 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_clearing_bankname')) { |
||
| 207 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 208 | $salesInstaller->addAttribute( |
||
| 209 | 'order', |
||
| 210 | 'payone_clearing_bankname', |
||
| 211 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_refund_iban')) { |
||
| 215 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 216 | $salesInstaller->addAttribute( |
||
| 217 | 'order', |
||
| 218 | 'payone_refund_iban', |
||
| 219 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_refund_bic')) { |
||
| 223 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 224 | $salesInstaller->addAttribute( |
||
| 225 | 'order', |
||
| 226 | 'payone_refund_bic', |
||
| 227 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_cancel_substitute_increment_id')) { |
||
| 231 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 232 | $salesInstaller->addAttribute( |
||
| 233 | 'order', |
||
| 234 | 'payone_cancel_substitute_increment_id', |
||
| 235 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 236 | ); |
||
| 237 | } |
||
| 238 | |||
| 239 | $serializedRows = $this->getSerializedConfigRows($setup); |
||
| 240 | if (!empty($serializedRows) && version_compare($this->shopHelper->getMagentoVersion(), '2.2.0', '>=')) { |
||
| 241 | $this->convertSerializedDataToJson($setup, $serializedRows); |
||
| 242 | } |
||
| 243 | |||
| 244 | if (version_compare($context->getVersion(), '2.2.0', '<=')) {// pre update version is less than or equal to 2.2.1 |
||
| 245 | $this->convertPersonstatusMappingConfig($setup); |
||
| 246 | } |
||
| 247 | |||
| 248 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_installment_duration')) { |
||
| 249 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 250 | $salesInstaller->addAttribute( |
||
| 251 | 'order', |
||
| 252 | 'payone_installment_duration', |
||
| 253 | ['type' => 'integer', 'length' => null] |
||
| 254 | ); |
||
| 255 | } |
||
| 256 | |||
| 257 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_express_type')) { |
||
| 258 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 259 | $salesInstaller->addAttribute( |
||
| 260 | 'order', |
||
| 261 | 'payone_express_type', |
||
| 262 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | $customerInstaller = $this->customerSetupFactory->create(['setup' => $setup]); |
||
| 267 | if ($setup->getConnection()->tableColumnExists($setup->getTable('customer_entity'), 'payone_paydirekt_registered')) { |
||
| 268 | $customerInstaller->removeAttribute('customer', 'payone_paydirekt_registered'); |
||
| 269 | if (!$customerInstaller->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'payone_paydirekt_registered', 'attribute_id')) { |
||
| 270 | $this->addPaydirektRegisteredAttribute($customerInstaller); |
||
| 271 | $this->copyPaydirektRegisteredData($setup, $customerInstaller->getAttributeId('customer', 'payone_paydirekt_registered')); |
||
| 272 | $setup->getConnection()->dropColumn($setup->getTable('customer_entity'), 'payone_paydirekt_registered'); |
||
| 273 | } |
||
| 274 | } elseif (!$customerInstaller->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'payone_paydirekt_registered', 'attribute_id')) { |
||
| 275 | $this->addPaydirektRegisteredAttribute($customerInstaller); |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->deactivateNewPaymentMethods($setup); |
||
| 279 | |||
| 280 | if (version_compare($context->getVersion(), '2.8.0', '<=')) { // pre update version is less than or equal to 2.8.0 |
||
| 281 | $this->convertCreditcardTypesConfig($setup); |
||
| 282 | } |
||
| 283 | |||
| 284 | $setup->endSetup(); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Adds payone_paydirekt_registered attribute |
||
| 289 | * |
||
| 290 | * @param CustomerSetup $customerInstaller |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | protected function addPaydirektRegisteredAttribute(CustomerSetup $customerInstaller) |
||
| 294 | { |
||
| 295 | $customerInstaller->addAttribute( |
||
| 296 | 'customer', |
||
| 297 | 'payone_paydirekt_registered', |
||
| 298 | [ |
||
| 299 | 'type' => 'int', |
||
| 300 | 'label' => 'Payone paydirekt OneClick is registered', |
||
| 301 | 'input' => 'text', |
||
| 302 | 'required' => false, |
||
| 303 | 'visible' => false, |
||
| 304 | 'user_defined' => false, |
||
| 305 | 'sort_order' => 999, |
||
| 306 | 'position' => 999, |
||
| 307 | 'system' => 0, |
||
| 308 | ] |
||
| 309 | ); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Fetch all serialized config rows from the payone module from the DB |
||
| 314 | * |
||
| 315 | * @param ModuleDataSetupInterface $setup |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | protected function getSerializedConfigRows(ModuleDataSetupInterface $setup) |
||
| 319 | { |
||
| 320 | $select = $setup->getConnection() |
||
| 321 | ->select() |
||
| 322 | ->from($setup->getTable('core_config_data'), ['config_id', 'value']) |
||
| 323 | ->where('path LIKE "%payone%"') |
||
| 324 | ->where('value LIKE "a:%"'); |
||
| 325 | |||
| 326 | return $setup->getConnection()->fetchAssoc($select); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Convert serialized data to json-encoded data in the core_config_data table |
||
| 331 | * |
||
| 332 | * @param ModuleDataSetupInterface $setup |
||
| 333 | * @param array $serializedRows |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | protected function convertSerializedDataToJson(ModuleDataSetupInterface $setup, $serializedRows) |
||
| 337 | { |
||
| 338 | foreach ($serializedRows as $id => $serializedRow) { |
||
| 339 | $aValue = unserialize($serializedRow['value']); |
||
| 340 | $sNewValue = json_encode($aValue); |
||
| 341 | |||
| 342 | $data = ['value' => $sNewValue]; |
||
| 343 | $where = ['config_id = ?' => $id]; |
||
| 344 | $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Change config path of personstatus mapping configuration |
||
| 350 | * |
||
| 351 | * @param ModuleDataSetupInterface $setup |
||
| 352 | * @return void |
||
| 353 | */ |
||
| 354 | protected function convertPersonstatusMappingConfig(ModuleDataSetupInterface $setup) |
||
| 355 | { |
||
| 356 | $data = ['path' => 'payone_protect/personstatus/mapping']; |
||
| 357 | $where = ['path = ?' => 'payone_protect/address_check/mapping_personstatus']; |
||
| 358 | $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Updates configured creditcard types from old data handling to new data handling |
||
| 363 | * |
||
| 364 | * @param ModuleDataSetupInterface $setup |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | protected function convertCreditcardTypesConfig(ModuleDataSetupInterface $setup) |
||
| 368 | { |
||
| 369 | $select = $setup->getConnection() |
||
| 370 | ->select() |
||
| 371 | ->from($setup->getTable('core_config_data'), ['config_id', 'value']) |
||
| 372 | ->where('path = "payone_payment/payone_creditcard/types"'); |
||
| 373 | $result = $setup->getConnection()->fetchAssoc($select); |
||
| 374 | |||
| 375 | $cardTypes = CreditcardTypes::getCreditcardTypes(); |
||
| 376 | foreach ($result as $row) { |
||
| 377 | $newCardTypes = []; |
||
| 378 | $activatedCardtypes = explode(',', $row['value']); |
||
| 379 | foreach ($activatedCardtypes as $activeCardType) { |
||
| 380 | if ($activeCardType == "C") { |
||
| 381 | $newCardTypes[] = "discover"; |
||
| 382 | } elseif ($activeCardType == "D") { |
||
| 383 | $newCardTypes[] = "dinersclub"; |
||
| 384 | } else { |
||
| 385 | foreach ($cardTypes as $cardTypeId => $cardType) { |
||
| 386 | if ($cardType['cardtype'] == $activeCardType) { |
||
| 387 | $newCardTypes[] = $cardTypeId; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | $data = ['value' => implode(",", $newCardTypes)]; |
||
| 394 | $where = ['config_id = ?' => $row['config_id']]; |
||
| 395 | $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Adds a config entry to the database to set the payment method to inactive |
||
| 401 | * |
||
| 402 | * @param string $methodCode |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | protected function addPaymentInactiveConfig($methodCode) |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Checks if there is a active config entry for the given payment method |
||
| 412 | * |
||
| 413 | * @param ModuleDataSetupInterface $setup |
||
| 414 | * @param string $methodCode |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | protected function isPaymentConfigExisting(ModuleDataSetupInterface $setup, $methodCode) |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Copied payone_paydirekt_registered data from the customer_entity table to the eav_entities |
||
| 433 | * |
||
| 434 | * @param ModuleDataSetupInterface $setup |
||
| 435 | * @param int $attributeId |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | protected function copyPaydirektRegisteredData(ModuleDataSetupInterface $setup, $attributeId) |
||
| 439 | { |
||
| 440 | $select = $setup->getConnection() |
||
| 441 | ->select() |
||
| 442 | ->from($setup->getTable('customer_entity'), ['entity_id']) |
||
| 443 | ->where('payone_paydirekt_registered = 1'); |
||
| 444 | $result = $setup->getConnection()->fetchAssoc($select); |
||
| 445 | |||
| 446 | foreach ($result as $row) { |
||
| 447 | $setup->getConnection()->insert( |
||
| 448 | $setup->getTable('customer_entity_int'), |
||
| 449 | [ |
||
| 450 | 'attribute_id' => $attributeId, |
||
| 451 | 'entity_id' => $row['entity_id'], |
||
| 452 | 'value' => 1, |
||
| 453 | ] |
||
| 454 | ); |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * 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 |
||
| 460 | * |
||
| 461 | * @param ModuleDataSetupInterface $setup |
||
| 462 | * @return void |
||
| 463 | */ |
||
| 464 | protected function deactivateNewPaymentMethods(ModuleDataSetupInterface $setup) |
||
| 469 | } |
||
| 470 | } |
||
| 473 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths