| Total Complexity | 70 |
| Total Lines | 367 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Invoice 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 Invoice, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Invoice |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * Index of added invoice items |
||
| 48 | * |
||
| 49 | * @var integer |
||
| 50 | */ |
||
| 51 | protected $iIndex = 1; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Invoice amount |
||
| 55 | * |
||
| 56 | * @var integer |
||
| 57 | */ |
||
| 58 | protected $dAmount = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Vat rate for following entities which may not have the vat attached to it |
||
| 62 | * |
||
| 63 | * @var double |
||
| 64 | */ |
||
| 65 | protected $dTax = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * PAYONE toolkit helper |
||
| 69 | * |
||
| 70 | * @var \Payone\Core\Helper\Toolkit |
||
| 71 | */ |
||
| 72 | protected $toolkitHelper; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * PAYONE amasty helper |
||
| 76 | * |
||
| 77 | * @var \Payone\Core\Helper\AmastyGiftcard |
||
| 78 | */ |
||
| 79 | protected $amastyHelper; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Request object |
||
| 83 | * |
||
| 84 | * @var Base |
||
| 85 | */ |
||
| 86 | protected $oRequest; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Current store code |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $sStoreCode; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Constructor |
||
| 97 | * |
||
| 98 | * @param \Payone\Core\Helper\Toolkit $toolkitHelper Toolkit helper |
||
| 99 | */ |
||
| 100 | public function __construct(\Payone\Core\Helper\Toolkit $toolkitHelper, \Payone\Core\Helper\AmastyGiftcard $amastyHelper) |
||
| 101 | { |
||
| 102 | $this->toolkitHelper = $toolkitHelper; |
||
| 103 | $this->amastyHelper = $amastyHelper; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Add parameters for a invoice position |
||
| 108 | * |
||
| 109 | * @param string $sId item identification |
||
| 110 | * @param double $dPrice item price |
||
| 111 | * @param string $sItemType item type |
||
| 112 | * @param int $iAmount item amount |
||
| 113 | * @param string $sDesc item description |
||
| 114 | * @param double $dVat item tax rate |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | protected function addInvoicePosition($sId, $dPrice, $sItemType, $iAmount, $sDesc, $dVat) |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Add invoicing data to the request and return the summed invoicing amount |
||
| 131 | * |
||
| 132 | * @param Base $oRequest Request object |
||
| 133 | * @param object $oOrder Order object |
||
| 134 | * @param array $aPositions Is given with non-complete captures or debits |
||
| 135 | * @param bool $blDebit Is the call coming from a debit request |
||
| 136 | * @param double $dShippingCosts Shipping costs - needed for Klarna start_session |
||
| 137 | * @return integer |
||
| 138 | */ |
||
| 139 | public function addProductInfo(Base $oRequest, $oOrder, $aPositions = false, $blDebit = false, $dShippingCosts = false) |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Add invoicing item for a product |
||
| 174 | * |
||
| 175 | * @param \Magento\Sales\Model\Order\Item $oItem |
||
| 176 | * @param array $aPositions |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | protected function addProductItem($oItem, $aPositions) |
||
| 180 | { |
||
| 181 | $sPositionKey = $oItem->getProductId().$oItem->getSku(); |
||
| 182 | if ($aPositions === false || array_key_exists($sPositionKey, $aPositions) !== false) { // full or single-invoice? |
||
| 183 | $dItemAmount = $oItem->getQtyOrdered(); // get ordered item amount |
||
| 184 | if ($oItem instanceof QuoteItem) { |
||
| 185 | $dItemAmount = $oItem->getQty(); |
||
| 186 | } |
||
| 187 | if ($aPositions !== false && array_key_exists($sPositionKey, $aPositions) !== false) { // product existing in single-invoice? |
||
| 188 | $dItemAmount = $aPositions[$sPositionKey]; // use amount from single-invoice |
||
| 189 | } |
||
| 190 | $iAmount = $this->convertItemAmount($dItemAmount); |
||
| 191 | $dPrice = $oItem->getBasePriceInclTax(); |
||
| 192 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 193 | $dPrice = $oItem->getPriceInclTax(); |
||
| 194 | } |
||
| 195 | $this->addInvoicePosition($oItem->getSku(), $dPrice, 'goods', $iAmount, $oItem->getName(), $oItem->getTaxPercent()); // add invoice params to request |
||
| 196 | if ($this->dTax === false) { // is dTax not set yet? |
||
| 197 | $this->dTax = $oItem->getTaxPercent(); // set the tax for following entities which dont have the vat attached to it |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | protected function addGiftCardItem($oOrder) |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * return giftcard-amount based on magento version |
||
| 218 | * |
||
| 219 | * @param $aGiftCard |
||
| 220 | * @return |
||
| 221 | */ |
||
| 222 | private function getGiftCardAmount($aGiftCard) |
||
| 223 | { |
||
| 224 | // up to Magento 2.3.4 giftcard-amount is saved in 'authorized', again in 2.4 |
||
| 225 | if (array_key_exists('authorized', $aGiftCard)) { |
||
| 226 | return -$aGiftCard['authorized']; |
||
| 227 | } |
||
| 228 | // in Magento 2.3.5 the array has slightly changed, giftcard-amount is only saved in 'ba' |
||
| 229 | if (array_key_exists('ba', $aGiftCard)) { |
||
| 230 | return -$aGiftCard['ba']; |
||
| 231 | } |
||
| 232 | return 0; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add invoicing item for shipping |
||
| 237 | * |
||
| 238 | * @param Order $oOrder |
||
| 239 | * @param array $aPositions |
||
| 240 | * @param bool $blDebit |
||
| 241 | * @param double $dShippingCosts |
||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | protected function addShippingItem($oOrder, $aPositions, $blDebit, $dShippingCosts = false) |
||
| 245 | { |
||
| 246 | $dPrice = $dShippingCosts; |
||
| 247 | if ($dPrice === false) { |
||
| 248 | $dPrice = $oOrder->getBaseShippingInclTax(); |
||
| 249 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 250 | $dPrice = $oOrder->getShippingInclTax(); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | // shipping costs existing or given for partial captures/debits? |
||
| 255 | if ($dPrice != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('delcost', $aPositions) !== false))) { |
||
| 256 | if ($aPositions !== false && array_key_exists('delcost', $aPositions) !== false) { // product existing in single-invoice? |
||
| 257 | $dPrice = $aPositions['delcost']; |
||
| 258 | } |
||
| 259 | $sDelDesc = __('Surcharge').' '.__('Shipping Costs'); // default description |
||
| 260 | if ($dPrice < 0) { // negative shipping cost |
||
| 261 | $sDelDesc = __('Deduction').' '.__('Shipping Costs'); // change item description to deduction |
||
| 262 | } |
||
| 263 | $sShippingSku = $this->toolkitHelper->getConfigParam('sku', 'costs', 'payone_misc', $this->getStoreCode()); // get configured shipping SKU |
||
| 264 | $this->addInvoicePosition($sShippingSku, $dPrice, 'shipment', 1, $sDelDesc, $this->dTax); // add invoice params to request |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Add invoicing item for discounts |
||
| 270 | * |
||
| 271 | * @param Order $oOrder |
||
| 272 | * @param array $aPositions |
||
| 273 | * @param bool $blDebit |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | protected function addDiscountItem($oOrder, $aPositions, $blDebit) |
||
| 277 | { |
||
| 278 | // discount costs existing or given for partial captures/debit? |
||
| 279 | $dTransmitDiscount = $oOrder->getBaseDiscountAmount(); |
||
| 280 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 281 | $dTransmitDiscount = $oOrder->getDiscountAmount(); |
||
| 282 | } |
||
| 283 | |||
| 284 | if ($oOrder instanceof Quote) { |
||
| 285 | $dTransmitDiscount = $oOrder->getBaseSubtotal() - $oOrder->getBaseSubtotalWithDiscount(); |
||
| 286 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 287 | $dTransmitDiscount = $oOrder->getSubtotal() - $oOrder->getSubtotalWithDiscount(); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | if ($dTransmitDiscount != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('discount', $aPositions) !== false))) { |
||
| 292 | if ($aPositions !== false && array_key_exists('discount', $aPositions) !== false) { |
||
| 293 | $dTransmitDiscount = $aPositions['discount']; |
||
| 294 | } |
||
| 295 | $dDiscount = $this->toolkitHelper->formatNumber($dTransmitDiscount); // format discount |
||
| 296 | if ($aPositions === false && $this->amastyHelper->hasAmastyGiftcards($oOrder->getQuoteId()) === false) { |
||
| 297 | // The calculations broken down to single items of Magento2 are unprecise and the Payone API will send an error if |
||
| 298 | // the calculated positions don't match, so we compensate for rounding-problems here |
||
| 299 | $dTotal = $oOrder->getBaseGrandTotal(); |
||
| 300 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 301 | $dTotal = $oOrder->getGrandTotal(); |
||
| 302 | } |
||
| 303 | $dDiff = ($this->dAmount + $dTransmitDiscount - $dTotal); // calc rounding discrepancy |
||
| 304 | $dDiscount -= $dDiff; // subtract difference from discount |
||
| 305 | } |
||
| 306 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'discount', 'payone_misc', $this->getStoreCode()); // get configured discount SKU |
||
| 307 | $sDesc = (string)__('Discount'); // default description |
||
| 308 | if ($oOrder->getCouponCode()) {// was a coupon code used? |
||
| 309 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc', $this->getStoreCode()); // get configured voucher SKU |
||
| 310 | $sDesc = (string)__('Coupon').' - '.$oOrder->getCouponCode(); // add counpon code to description |
||
| 311 | } |
||
| 312 | $this->addInvoicePosition($sDiscountSku, $dDiscount, 'voucher', 1, $sDesc, $this->dTax); // add invoice params to request |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Adding amasty giftcards to request |
||
| 318 | * |
||
| 319 | * @param Order $oOrder |
||
| 320 | * @param array $aPositions |
||
| 321 | * @param bool $blDebit |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | protected function addAmastyGiftcards($oOrder, $aPositions, $blDebit) |
||
| 325 | { |
||
| 326 | $aGiftCards = $this->amastyHelper->getAmastyGiftCards($oOrder->getQuoteId()); |
||
| 327 | for ($i = 0; $i < count($aGiftCards); $i++) { |
||
| 328 | $aGiftCard = $aGiftCards[$i]; |
||
| 329 | $blIsLastGiftcard = false; |
||
| 330 | if ($i + 1 == count($aGiftCards)) { |
||
| 331 | $blIsLastGiftcard = true; |
||
| 332 | } |
||
| 333 | |||
| 334 | $dTransmitDiscount = $aGiftCard['base_gift_amount']; |
||
| 335 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 336 | $dTransmitDiscount = $aGiftCard['gift_amount']; |
||
| 337 | } |
||
| 338 | if ($dTransmitDiscount != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('discount', $aPositions) !== false))) { |
||
| 339 | $dTransmitDiscount = $dTransmitDiscount * -1; |
||
| 340 | $dDiscount = $this->toolkitHelper->formatNumber($dTransmitDiscount); // format discount |
||
| 341 | if ($aPositions === false && $blIsLastGiftcard === true) { |
||
| 342 | // The calculations broken down to single items of Magento2 are unprecise and the Payone API will send an error if |
||
| 343 | // the calculated positions don't match, so we compensate for rounding-problems here |
||
| 344 | $dTotal = $oOrder->getBaseGrandTotal(); |
||
| 345 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 346 | $dTotal = $oOrder->getGrandTotal(); |
||
| 347 | } |
||
| 348 | $dDiff = ($this->dAmount + $dTransmitDiscount - $dTotal); // calc rounding discrepancy |
||
| 349 | $dDiscount -= $dDiff; // subtract difference from discount |
||
| 350 | } |
||
| 351 | |||
| 352 | if ($dDiscount != 0) { |
||
| 353 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc', $this->getStoreCode()); // get configured voucher SKU |
||
| 354 | $sDesc = (string)__('Amasty Coupon').' - '.$aGiftCard['code']; // add counpon code to description |
||
| 355 | $this->addInvoicePosition($sDiscountSku, $dDiscount, 'voucher', 1, $sDesc, $this->dTax); // add invoice params to request |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Check if item amount has decimal places |
||
| 363 | * Throw exception if given amount is no integer |
||
| 364 | * |
||
| 365 | * @param double $dItemAmount |
||
| 366 | * @throws \InvalidArgumentException |
||
| 367 | * @return int |
||
| 368 | */ |
||
| 369 | protected function convertItemAmount($dItemAmount) |
||
| 370 | { |
||
| 371 | if (fmod(floatval($dItemAmount), 1.0) > 0) { // input does not represent an integer |
||
| 372 | $sErrorMessage = "Unable to use floating point values for item amounts! Parameter was: "; |
||
| 373 | throw new \InvalidArgumentException($sErrorMessage . strval($dItemAmount), 1); |
||
| 374 | } else { // return the integer value |
||
| 375 | return intval($dItemAmount); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Set store code |
||
| 381 | * |
||
| 382 | * @param $sStoreCode |
||
| 383 | * @return void |
||
| 384 | */ |
||
| 385 | protected function setStoreCode($sStoreCode) |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Returns store code |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | protected function getStoreCode() |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns formatted sku |
||
| 402 | * |
||
| 403 | * @param string $sSku |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | protected function formatSku($sSku) |
||
| 413 |
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