| Total Complexity | 66 |
| Total Lines | 334 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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) |
||
| 118 | { |
||
| 119 | $this->oRequest->addParameter('id['.$this->iIndex.']', $sId); // add invoice item id |
||
| 120 | $this->oRequest->addParameter('pr['.$this->iIndex.']', $this->toolkitHelper->formatNumber($dPrice) * 100); // expected in smallest unit of currency |
||
| 121 | $this->oRequest->addParameter('it['.$this->iIndex.']', $sItemType); // add invoice item type |
||
| 122 | $this->oRequest->addParameter('no['.$this->iIndex.']', $iAmount); // add invoice item amount |
||
| 123 | $this->oRequest->addParameter('de['.$this->iIndex.']', $sDesc); // add invoice item description |
||
| 124 | $this->oRequest->addParameter('va['.$this->iIndex.']', $this->toolkitHelper->formatNumber($dVat * 100, 0)); // expected * 100 to also handle vats with decimals |
||
| 125 | $this->dAmount += $dPrice * $iAmount; // needed for return of the main method |
||
| 126 | $this->iIndex++; // increase index for next item |
||
| 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) |
||
| 140 | { |
||
| 141 | $this->oRequest = $oRequest; // write request to property for manipulation of the object |
||
| 142 | $this->setStoreCode($oOrder->getStore()->getCode()); |
||
| 143 | if ($oOrder instanceof Order) { |
||
| 144 | $sInvoiceAppendix = $this->toolkitHelper->getInvoiceAppendix($oOrder); // get invoice appendix |
||
| 145 | if (!empty($sInvoiceAppendix)) { // invoice appendix existing? |
||
| 146 | $this->oRequest->addParameter('invoiceappendix', $sInvoiceAppendix); // add appendix to request |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | $iQtyInvoiced = 0; |
||
| 151 | foreach ($oOrder->getAllItems() as $oItem) { // add invoice items for all order items |
||
| 152 | if (($oOrder instanceof Order && $oItem->isDummy() === false) || ($oOrder instanceof Quote && $oItem->getParentItemId() === null)) { // prevent variant-products of adding 2 items |
||
| 153 | $this->addProductItem($oItem, $aPositions); // add product invoice params to request |
||
| 154 | } |
||
| 155 | $iQtyInvoiced += $oItem->getOrigData('qty_invoiced'); // get data pre-capture |
||
| 156 | } |
||
| 157 | |||
| 158 | $blFirstCapture = true; // Is first capture? |
||
| 159 | if ($iQtyInvoiced > 0) { |
||
| 160 | $blFirstCapture = false; |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($aPositions === false || $blFirstCapture === true || $blDebit === true) { |
||
| 164 | $this->addShippingItem($oOrder, $aPositions, $blDebit, $dShippingCosts); // add shipping invoice params to request |
||
| 165 | $this->addDiscountItem($oOrder, $aPositions, $blDebit); // add discount invoice params to request |
||
| 166 | $this->addGiftCardItem($oOrder); // add gift card invoice params to request |
||
| 167 | $this->addAmastyGiftcards($oOrder, $aPositions, $blDebit); // add amasty giftcard invoice params to request |
||
| 168 | } |
||
| 169 | return $this->dAmount; |
||
| 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) |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Add invoicing item for shipping |
||
| 217 | * |
||
| 218 | * @param Order $oOrder |
||
| 219 | * @param array $aPositions |
||
| 220 | * @param bool $blDebit |
||
| 221 | * @param double $dShippingCosts |
||
| 222 | * @return void |
||
| 223 | */ |
||
| 224 | protected function addShippingItem($oOrder, $aPositions, $blDebit, $dShippingCosts = false) |
||
| 225 | { |
||
| 226 | $dPrice = $dShippingCosts; |
||
| 227 | if ($dPrice === false) { |
||
| 228 | $dPrice = $oOrder->getBaseShippingInclTax(); |
||
| 229 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 230 | $dPrice = $oOrder->getShippingInclTax(); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | // shipping costs existing or given for partial captures/debits? |
||
| 235 | if ($dPrice != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('delcost', $aPositions) !== false))) { |
||
| 236 | if ($aPositions !== false && array_key_exists('delcost', $aPositions) !== false) { // product existing in single-invoice? |
||
| 237 | $dPrice = $aPositions['delcost']; |
||
| 238 | } |
||
| 239 | $sDelDesc = __('Surcharge').' '.__('Shipping Costs'); // default description |
||
| 240 | if ($dPrice < 0) { // negative shipping cost |
||
| 241 | $sDelDesc = __('Deduction').' '.__('Shipping Costs'); // change item description to deduction |
||
| 242 | } |
||
| 243 | $sShippingSku = $this->toolkitHelper->getConfigParam('sku', 'costs', 'payone_misc', $this->getStoreCode()); // get configured shipping SKU |
||
| 244 | $this->addInvoicePosition($sShippingSku, $dPrice, 'shipment', 1, $sDelDesc, $this->dTax); // add invoice params to request |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Add invoicing item for discounts |
||
| 250 | * |
||
| 251 | * @param Order $oOrder |
||
| 252 | * @param array $aPositions |
||
| 253 | * @param bool $blDebit |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | protected function addDiscountItem($oOrder, $aPositions, $blDebit) |
||
| 257 | { |
||
| 258 | // discount costs existing or given for partial captures/debit? |
||
| 259 | $dTransmitDiscount = $oOrder->getBaseDiscountAmount(); |
||
| 260 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 261 | $dTransmitDiscount = $oOrder->getDiscountAmount(); |
||
| 262 | } |
||
| 263 | |||
| 264 | if ($oOrder instanceof Quote) { |
||
| 265 | $dTransmitDiscount = $oOrder->getBaseSubtotal() - $oOrder->getBaseSubtotalWithDiscount(); |
||
| 266 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 267 | $dTransmitDiscount = $oOrder->getSubtotal() - $oOrder->getSubtotalWithDiscount(); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | if ($dTransmitDiscount != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('discount', $aPositions) !== false))) { |
||
| 272 | if ($aPositions !== false && array_key_exists('discount', $aPositions) !== false) { |
||
| 273 | $dTransmitDiscount = $aPositions['discount']; |
||
| 274 | } |
||
| 275 | $dDiscount = $this->toolkitHelper->formatNumber($dTransmitDiscount); // format discount |
||
| 276 | if ($aPositions === false && $this->amastyHelper->hasAmastyGiftcards($oOrder->getQuoteId()) === false) { |
||
| 277 | // The calculations broken down to single items of Magento2 are unprecise and the Payone API will send an error if |
||
| 278 | // the calculated positions don't match, so we compensate for rounding-problems here |
||
| 279 | $dTotal = $oOrder->getBaseGrandTotal(); |
||
| 280 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 281 | $dTotal = $oOrder->getGrandTotal(); |
||
| 282 | } |
||
| 283 | $dDiff = ($this->dAmount + $dTransmitDiscount - $dTotal); // calc rounding discrepancy |
||
| 284 | $dDiscount -= $dDiff; // subtract difference from discount |
||
| 285 | } |
||
| 286 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'discount', 'payone_misc', $this->getStoreCode()); // get configured discount SKU |
||
| 287 | $sDesc = (string)__('Discount'); // default description |
||
| 288 | if ($oOrder->getCouponCode()) {// was a coupon code used? |
||
| 289 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc', $this->getStoreCode()); // get configured voucher SKU |
||
| 290 | $sDesc = (string)__('Coupon').' - '.$oOrder->getCouponCode(); // add counpon code to description |
||
| 291 | } |
||
| 292 | $this->addInvoicePosition($sDiscountSku, $dDiscount, 'voucher', 1, $sDesc, $this->dTax); // add invoice params to request |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Adding amasty giftcards to request |
||
| 298 | * |
||
| 299 | * @param Order $oOrder |
||
| 300 | * @param array $aPositions |
||
| 301 | * @param bool $blDebit |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | protected function addAmastyGiftcards($oOrder, $aPositions, $blDebit) |
||
| 305 | { |
||
| 306 | $aGiftCards = $this->amastyHelper->getAmastyGiftCards($oOrder->getQuoteId()); |
||
| 307 | for ($i = 0; $i < count($aGiftCards); $i++) { |
||
| 308 | $aGiftCard = $aGiftCards[$i]; |
||
| 309 | $blIsLastGiftcard = false; |
||
| 310 | if ($i + 1 == count($aGiftCards)) { |
||
| 311 | $blIsLastGiftcard = true; |
||
| 312 | } |
||
| 313 | |||
| 314 | $dTransmitDiscount = $aGiftCard['base_gift_amount']; |
||
| 315 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 316 | $dTransmitDiscount = $aGiftCard['gift_amount']; |
||
| 317 | } |
||
| 318 | if ($dTransmitDiscount != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('discount', $aPositions) !== false))) { |
||
| 319 | $dTransmitDiscount = $dTransmitDiscount * -1; |
||
| 320 | $dDiscount = $this->toolkitHelper->formatNumber($dTransmitDiscount); // format discount |
||
| 321 | if ($aPositions === false && $blIsLastGiftcard === true) { |
||
| 322 | // The calculations broken down to single items of Magento2 are unprecise and the Payone API will send an error if |
||
| 323 | // the calculated positions don't match, so we compensate for rounding-problems here |
||
| 324 | $dTotal = $oOrder->getBaseGrandTotal(); |
||
| 325 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
| 326 | $dTotal = $oOrder->getGrandTotal(); |
||
| 327 | } |
||
| 328 | $dDiff = ($this->dAmount + $dTransmitDiscount - $dTotal); // calc rounding discrepancy |
||
| 329 | $dDiscount -= $dDiff; // subtract difference from discount |
||
| 330 | } |
||
| 331 | |||
| 332 | if ($dDiscount != 0) { |
||
| 333 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc', $this->getStoreCode()); // get configured voucher SKU |
||
| 334 | $sDesc = (string)__('Amasty Coupon').' - '.$aGiftCard['code']; // add counpon code to description |
||
| 335 | $this->addInvoicePosition($sDiscountSku, $dDiscount, 'voucher', 1, $sDesc, $this->dTax); // add invoice params to request |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Check if item amount has decimal places |
||
| 343 | * Throw exception if given amount is no integer |
||
| 344 | * |
||
| 345 | * @param double $dItemAmount |
||
| 346 | * @throws \InvalidArgumentException |
||
| 347 | * @return int |
||
| 348 | */ |
||
| 349 | protected function convertItemAmount($dItemAmount) |
||
| 350 | { |
||
| 351 | if (fmod(floatval($dItemAmount), 1.0) > 0) { // input does not represent an integer |
||
| 352 | $sErrorMessage = "Unable to use floating point values for item amounts! Parameter was: "; |
||
| 353 | throw new \InvalidArgumentException($sErrorMessage . strval($dItemAmount), 1); |
||
| 354 | } else { // return the integer value |
||
| 355 | return intval($dItemAmount); |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set store code |
||
| 361 | * |
||
| 362 | * @param $sStoreCode |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | protected function setStoreCode($sStoreCode) |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns store code |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | protected function getStoreCode() |
||
| 378 | } |
||
| 379 | } |
||
| 380 |
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