| Total Complexity | 43 |
| Total Lines | 411 |
| 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 |
||
| 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( |
||
| 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_installment_duration')) { |
||
| 248 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 249 | $salesInstaller->addAttribute( |
||
| 250 | 'order', |
||
| 251 | 'payone_installment_duration', |
||
| 252 | ['type' => 'integer', 'length' => null] |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | if (!$setup->getConnection()->tableColumnExists($setup->getTable('sales_order'), 'payone_express_type')) { |
||
| 257 | $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); |
||
| 258 | $salesInstaller->addAttribute( |
||
| 259 | 'order', |
||
| 260 | 'payone_express_type', |
||
| 261 | ['type' => 'varchar', 'length' => 64, 'default' => ''] |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | |||
| 265 | if ($setup->getConnection()->tableColumnExists($setup->getTable('customer_entity'), 'payone_paydirekt_registered')) { |
||
| 266 | $customerInstaller = $this->customerSetupFactory->create(['setup' => $setup]); |
||
| 267 | $customerInstaller->removeAttribute('customer', 'payone_paydirekt_registered'); |
||
| 268 | if (!$customerInstaller->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'payone_paydirekt_registered', 'attribute_id')) { |
||
| 269 | $customerInstaller->addAttribute( |
||
| 270 | 'customer', |
||
| 271 | 'payone_paydirekt_registered', |
||
| 272 | [ |
||
| 273 | 'type' => 'int', |
||
| 274 | 'label' => 'Payone paydirekt OneClick is registered', |
||
| 275 | 'input' => 'text', |
||
| 276 | 'required' => false, |
||
| 277 | 'visible' => false, |
||
| 278 | 'user_defined' => false, |
||
| 279 | 'sort_order' => 999, |
||
| 280 | 'position' => 999, |
||
| 281 | 'system' => 0, |
||
| 282 | ] |
||
| 283 | ); |
||
| 284 | |||
| 285 | $this->copyPaydirektRegisteredData($setup, $customerInstaller->getAttributeId('customer', 'payone_paydirekt_registered')); |
||
| 286 | $setup->getConnection()->dropColumn($setup->getTable('customer_entity'), 'payone_paydirekt_registered'); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | $this->deactivateNewPaymentMethods($setup); |
||
| 291 | |||
| 292 | if (version_compare($context->getVersion(), '2.8.0', '<=')) { // pre update version is less than or equal to 2.8.0 |
||
| 293 | $this->convertCreditcardTypesConfig($setup); |
||
| 294 | } |
||
| 295 | |||
| 296 | $setup->endSetup(); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Fetch all serialized config rows from the payone module from the DB |
||
| 301 | * |
||
| 302 | * @param ModuleDataSetupInterface $setup |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | protected function getSerializedConfigRows(ModuleDataSetupInterface $setup) |
||
| 306 | { |
||
| 307 | $select = $setup->getConnection() |
||
| 308 | ->select() |
||
| 309 | ->from($setup->getTable('core_config_data'), ['config_id', 'value']) |
||
| 310 | ->where('path LIKE "%payone%"') |
||
| 311 | ->where('value LIKE "a:%"'); |
||
| 312 | |||
| 313 | return $setup->getConnection()->fetchAssoc($select); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Convert serialized data to json-encoded data in the core_config_data table |
||
| 318 | * |
||
| 319 | * @param ModuleDataSetupInterface $setup |
||
| 320 | * @param array $serializedRows |
||
| 321 | * @return void |
||
| 322 | */ |
||
| 323 | protected function convertSerializedDataToJson(ModuleDataSetupInterface $setup, $serializedRows) |
||
| 324 | { |
||
| 325 | foreach ($serializedRows as $id => $serializedRow) { |
||
| 326 | $aValue = unserialize($serializedRow['value']); |
||
| 327 | $sNewValue = json_encode($aValue); |
||
| 328 | |||
| 329 | $data = ['value' => $sNewValue]; |
||
| 330 | $where = ['config_id = ?' => $id]; |
||
| 331 | $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Change config path of personstatus mapping configuration |
||
| 337 | * |
||
| 338 | * @param ModuleDataSetupInterface $setup |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | protected function convertPersonstatusMappingConfig(ModuleDataSetupInterface $setup) |
||
| 342 | { |
||
| 343 | $data = ['path' => 'payone_protect/personstatus/mapping']; |
||
| 344 | $where = ['path = ?' => 'payone_protect/address_check/mapping_personstatus']; |
||
| 345 | $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Updates configured creditcard types from old data handling to new data handling |
||
| 350 | * |
||
| 351 | * @param ModuleDataSetupInterface $setup |
||
| 352 | * @return void |
||
| 353 | */ |
||
| 354 | protected function convertCreditcardTypesConfig(ModuleDataSetupInterface $setup) |
||
| 355 | { |
||
| 356 | $select = $setup->getConnection() |
||
| 357 | ->select() |
||
| 358 | ->from($setup->getTable('core_config_data'), ['config_id', 'value']) |
||
| 359 | ->where('path = "payone_payment/payone_creditcard/types"'); |
||
| 360 | $result = $setup->getConnection()->fetchAssoc($select); |
||
| 361 | |||
| 362 | $cardTypes = CreditcardTypes::getCreditcardTypes(); |
||
| 363 | foreach ($result as $row) { |
||
| 364 | $newCardTypes = []; |
||
| 365 | $activatedCardtypes = explode(',', $row['value']); |
||
| 366 | foreach ($activatedCardtypes as $activeCardType) { |
||
| 367 | if ($activeCardType == "C") { |
||
| 368 | $newCardTypes[] = "discover"; |
||
| 369 | } elseif ($activeCardType == "D") { |
||
| 370 | $newCardTypes[] = "dinersclub"; |
||
| 371 | } else { |
||
| 372 | foreach ($cardTypes as $cardTypeId => $cardType) { |
||
| 373 | if ($cardType['cardtype'] == $activeCardType) { |
||
| 374 | $newCardTypes[] = $cardTypeId; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | $data = ['value' => implode(",", $newCardTypes)]; |
||
| 381 | $where = ['config_id = ?' => $row['config_id']]; |
||
| 382 | $setup->getConnection()->update($setup->getTable('core_config_data'), $data, $where); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Adds a config entry to the database to set the payment method to inactive |
||
| 388 | * |
||
| 389 | * @param string $methodCode |
||
| 390 | * @return void |
||
| 391 | */ |
||
| 392 | protected function addPaymentInactiveConfig($methodCode) |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Checks if there is a active config entry for the given payment method |
||
| 399 | * |
||
| 400 | * @param ModuleDataSetupInterface $setup |
||
| 401 | * @param string $methodCode |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | protected function isPaymentConfigExisting(ModuleDataSetupInterface $setup, $methodCode) |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Copied payone_paydirekt_registered data from the customer_entity table to the eav_entities |
||
| 420 | * |
||
| 421 | * @param ModuleDataSetupInterface $setup |
||
| 422 | * @param int $attributeId |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | protected function copyPaydirektRegisteredData(ModuleDataSetupInterface $setup, $attributeId) |
||
| 426 | { |
||
| 427 | $select = $setup->getConnection() |
||
| 428 | ->select() |
||
| 429 | ->from($setup->getTable('customer_entity'), ['entity_id']) |
||
| 430 | ->where('payone_paydirekt_registered = 1'); |
||
| 431 | $result = $setup->getConnection()->fetchAssoc($select); |
||
| 432 | |||
| 433 | foreach ($result as $row) { |
||
| 434 | $setup->getConnection()->insert( |
||
| 435 | $setup->getTable('customer_entity_int'), |
||
| 436 | [ |
||
| 437 | 'attribute_id' => $attributeId, |
||
| 438 | 'entity_id' => $row['entity_id'], |
||
| 439 | 'value' => 1, |
||
| 440 | ] |
||
| 441 | ); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * 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 |
||
| 447 | * |
||
| 448 | * @param ModuleDataSetupInterface $setup |
||
| 449 | * @return void |
||
| 450 | */ |
||
| 451 | protected function deactivateNewPaymentMethods(ModuleDataSetupInterface $setup) |
||
| 456 | } |
||
| 457 | } |
||
| 460 |
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