| Total Complexity | 48 |
| Total Lines | 304 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Debit 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 Debit, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Debit extends Base |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var \Payone\Core\Model\Api\Invoice $invoiceGenerator |
||
| 42 | */ |
||
| 43 | protected $invoiceGenerator; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * PAYONE database helper |
||
| 47 | * |
||
| 48 | * @var \Payone\Core\Helper\Database |
||
| 49 | */ |
||
| 50 | protected $databaseHelper; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * PAYONE toolkit helper |
||
| 54 | * |
||
| 55 | * @var \Payone\Core\Helper\Toolkit |
||
| 56 | */ |
||
| 57 | protected $toolkitHelper; |
||
| 58 | |||
| 59 | |||
| 60 | const AREA_CODE = \Magento\Framework\App\Area::AREA_ADMINHTML; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * State resolving |
||
| 64 | * |
||
| 65 | * @var \Magento\Framework\App\State |
||
| 66 | */ |
||
| 67 | protected $state; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Constructor |
||
| 71 | * |
||
| 72 | * @param \Payone\Core\Helper\Shop $shopHelper |
||
| 73 | * @param \Payone\Core\Helper\Environment $environmentHelper |
||
| 74 | * @param \Payone\Core\Helper\Api $apiHelper |
||
| 75 | * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog |
||
| 76 | * @param \Payone\Core\Model\Api\Invoice $invoiceGenerator |
||
| 77 | * @param \Payone\Core\Helper\Database $databaseHelper |
||
| 78 | * @param \Payone\Core\Helper\Toolkit $toolkitHelper |
||
| 79 | * @param \Magento\Framework\App\State $state |
||
| 80 | */ |
||
| 81 | public function __construct( |
||
| 82 | \Payone\Core\Helper\Shop $shopHelper, |
||
| 83 | \Payone\Core\Helper\Environment $environmentHelper, |
||
| 84 | \Payone\Core\Helper\Api $apiHelper, |
||
| 85 | \Payone\Core\Model\ResourceModel\ApiLog $apiLog, |
||
| 86 | \Payone\Core\Model\Api\Invoice $invoiceGenerator, |
||
| 87 | \Payone\Core\Helper\Database $databaseHelper, |
||
| 88 | \Payone\Core\Helper\Toolkit $toolkitHelper, |
||
| 89 | \Magento\Framework\App\State $state |
||
| 90 | ) { |
||
| 91 | parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog); |
||
| 92 | $this->invoiceGenerator = $invoiceGenerator; |
||
| 93 | $this->databaseHelper = $databaseHelper; |
||
| 94 | $this->toolkitHelper = $toolkitHelper; |
||
| 95 | $this->state = $state; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get creditmemo array from request parameters |
||
| 100 | * |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | protected function getCreditmemoRequestParams() |
||
| 104 | { |
||
| 105 | return $this->shopHelper->getRequestParameter('creditmemo'); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Generate position list for invoice data transmission |
||
| 110 | * |
||
| 111 | * @param Order $oOrder |
||
| 112 | * @param Creditmemo $oCreditmemo |
||
| 113 | * @return array|false |
||
| 114 | */ |
||
| 115 | protected function getInvoiceList(Order $oOrder, Creditmemo $oCreditmemo) |
||
| 116 | { |
||
| 117 | $aCreditmemo = $this->getCreditmemoRequestParams(); |
||
| 118 | |||
| 119 | if (empty($aCreditmemo) && $this->state->getAreaCode() != self::AREA_CODE) { |
||
| 120 | $aCreditmemo = $this->getCreditMemoData($oCreditmemo); |
||
| 121 | } |
||
| 122 | |||
| 123 | $aPositions = []; |
||
| 124 | $blFull = true; |
||
| 125 | if ($aCreditmemo && array_key_exists('items', $aCreditmemo) !== false) { |
||
| 126 | foreach ($oOrder->getAllItems() as $oItem) { |
||
| 127 | if (isset($aCreditmemo['items'][$oItem->getItemId()]) && $aCreditmemo['items'][$oItem->getItemId()]['qty'] > 0) { |
||
| 128 | $aPositions[$oItem->getProductId().$oItem->getSku()] = $aCreditmemo['items'][$oItem->getItemId()]['qty']; |
||
| 129 | if ($aCreditmemo['items'][$oItem->getItemId()]['qty'] != $oItem->getQtyOrdered()) { |
||
| 130 | $blFull = false; |
||
| 131 | } |
||
| 132 | } elseif (!$oItem->getParentItemId()) { // dont set invoice list to not full on variant dummy items |
||
| 133 | $blFull = false; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | if (isset($aCreditmemo['shipping_amount']) && $aCreditmemo['shipping_amount'] != 0) { |
||
| 138 | $aPositions['delcost'] = $oCreditmemo->getBaseShippingInclTax(); |
||
| 139 | if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $this->storeCode) == 'display') { |
||
| 140 | $aPositions['delcost'] = $oCreditmemo->getShippingInclTax(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | if ($blFull !== true && $oCreditmemo->getBaseDiscountAmount() != 0) { |
||
| 144 | $aPositions['discount'] = $oCreditmemo->getBaseDiscountAmount(); |
||
| 145 | if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $this->storeCode) == 'display') { |
||
| 146 | $aPositions['discount'] = $oCreditmemo->getDiscountAmount(); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | if ($blFull === true && (!isset($aCreditmemo['shipping_amount']) || $aCreditmemo['shipping_amount'] == $oOrder->getBaseShippingInclTax())) { |
||
| 150 | $aPositions = false; // false = full debit |
||
| 151 | } |
||
| 152 | return $aPositions; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Send request "debit" to PAYONE server API |
||
| 157 | * |
||
| 158 | * @param PayoneMethod $oPayment |
||
| 159 | * @param InfoInterface $oPaymentInfo |
||
| 160 | * @param float $dAmount |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public function sendRequest(PayoneMethod $oPayment, InfoInterface $oPaymentInfo, $dAmount) |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get substituted refund appendix text |
||
| 224 | * |
||
| 225 | * @param Order $oOrder |
||
| 226 | * @param PayoneMethod $oPayment |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | protected function getRefundAppendix(Order $oOrder, PayoneMethod $oPayment) |
||
| 230 | { |
||
| 231 | $sText = $this->shopHelper->getConfigParam('invoice_appendix_refund', 'invoicing', 'payone_general', $this->storeCode); |
||
| 232 | $sCreditMemoIncrId = ''; |
||
| 233 | $sInvoiceIncrementId = ''; |
||
| 234 | $sInvoiceId = ''; |
||
| 235 | |||
| 236 | $oCreditmemo = $oPayment->getCreditmemo(); |
||
| 237 | if ($oCreditmemo) { |
||
| 238 | $sCreditMemoIncrId = $oCreditmemo->getIncrementId(); |
||
| 239 | $oInvoice = $oCreditmemo->getInvoice(); |
||
| 240 | if ($oInvoice) { |
||
| 241 | $sInvoiceIncrementId = $oInvoice->getIncrementId(); |
||
| 242 | $sInvoiceId = $oInvoice->getId(); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | $aSubstitutionArray = [ |
||
| 247 | '{{order_increment_id}}' => $oOrder->getIncrementId(), |
||
| 248 | '{{order_id}}' => $oOrder->getId(), |
||
| 249 | '{{customer_id}}' => $oOrder->getCustomerId(), |
||
| 250 | '{{creditmemo_increment_id}}' => $sCreditMemoIncrId, |
||
| 251 | '{{invoice_increment_id}}' => $sInvoiceIncrementId, |
||
| 252 | '{{invoice_id}}' => $sInvoiceId, |
||
| 253 | ]; |
||
| 254 | $sRefundAppendix = $this->toolkitHelper->handleSubstituteReplacement($sText, $aSubstitutionArray, 255); |
||
| 255 | return $sRefundAppendix; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Validate IBAN |
||
| 260 | * |
||
| 261 | * @param string $sIban |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | protected function isIbanValid($sIban) |
||
| 265 | { |
||
| 266 | $sRegex = '/^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}$/'; |
||
| 267 | return $this->checkRegex($sRegex, $sIban); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Check if the regex validates correctly |
||
| 272 | * |
||
| 273 | * @param string $sRegex |
||
| 274 | * @param string $sValue |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | protected function checkRegex($sRegex, $sValue) |
||
| 278 | { |
||
| 279 | preg_match($sRegex, str_replace(' ', '', $sValue), $aMatches); |
||
| 280 | if (empty($aMatches)) { |
||
| 281 | return false; |
||
| 282 | } |
||
| 283 | return true; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Validate IBAN |
||
| 288 | * |
||
| 289 | * @param string $sBic |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | protected function isBicValid($sBic) |
||
| 293 | { |
||
| 294 | $sRegex = '/^([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?)$/'; |
||
| 295 | return $this->checkRegex($sRegex, $sBic); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Check IBAN and BIC fields |
||
| 300 | * |
||
| 301 | * @param string $sIban |
||
| 302 | * @param string $sBic |
||
| 303 | * @return bool |
||
| 304 | * @throws LocalizedException |
||
| 305 | */ |
||
| 306 | public function isSepaDataValid($sIban, $sBic) |
||
| 307 | { |
||
| 308 | if (!$this->isIbanValid($sIban)) { |
||
| 309 | throw new LocalizedException(__('The given IBAN is invalid!')); |
||
| 310 | } |
||
| 311 | if (!$this->isBicValid($sBic)) { |
||
| 312 | throw new LocalizedException(__('The given BIC is invalid!')); |
||
| 313 | } |
||
| 314 | return true; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Emulate the getCreditmemoRequestParams method for creditmemos that are not created via adminhtml |
||
| 319 | * |
||
| 320 | * @param $oCreditmemo |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | protected function getCreditMemoData($oCreditmemo) |
||
| 342 | } |
||
| 343 | } |
||
| 344 |
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