| Total Complexity | 41 |
| Total Lines | 370 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 | 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) |
||
| 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) |
||
| 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) |
||
| 419 |
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