| Conditions | 51 |
| Paths | 1770 |
| Total Lines | 179 |
| Code Lines | 107 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 413 | protected function LiveCalculatedTotal() |
||
| 414 | { |
||
| 415 | //________________ start caching mechanism |
||
| 416 | if (self::$calculations_done) { |
||
| 417 | return self::$_actual_charges; |
||
| 418 | } |
||
| 419 | self::$calculations_done = true; |
||
| 420 | //________________ end caching mechanism |
||
| 421 | |||
| 422 | self::$_actual_charges = 0; |
||
| 423 | //do we have enough information |
||
| 424 | $obj = $this->liveOptionObject(); |
||
| 425 | $items = $this->Order()->Items(); |
||
| 426 | if (is_object($obj) && $obj->exists() && $items && $items->count()) { |
||
| 427 | |||
| 428 | |||
| 429 | //are ALL products excluded? |
||
| 430 | if ($obj->ExcludedProducts() && $obj->ExcludedProducts()->count()) { |
||
| 431 | $hasIncludedProduct = false; |
||
| 432 | $excludedProductIDArray = $obj->ExcludedProducts()->column('ID'); |
||
| 433 | //are all the products excluded? |
||
| 434 | foreach ($items as $orderItem) { |
||
| 435 | $product = $orderItem->Product(); |
||
| 436 | if ($product) { |
||
| 437 | if (in_array($product->ID, $excludedProductIDArray)) { |
||
| 438 | //do nothing |
||
| 439 | } else { |
||
| 440 | $hasIncludedProduct = true; |
||
| 441 | break; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | } |
||
| 445 | if ($hasIncludedProduct === false) { |
||
| 446 | $this->debugMessage .= "<hr />all products are excluded from delivery charges"; |
||
| 447 | return self::$_actual_charges; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | $this->debugMessage .= "<hr />option selected: ".$obj->Title.", and items present"; |
||
| 452 | //lets check sub-total |
||
| 453 | $subTotalAmount = $this->LiveSubTotalAmount(); |
||
| 454 | $this->debugMessage .= "<hr />sub total amount is: \$". $subTotalAmount; |
||
| 455 | // no need to charge, order is big enough |
||
| 456 | $minForZeroRate = floatval($obj->MinimumOrderAmountForZeroRate); |
||
| 457 | $maxForZeroRate = floatval($obj->FreeShippingUpToThisOrderAmount); |
||
| 458 | |||
| 459 | $weigth = $weight = $this->LiveTotalWeight(); |
||
| 460 | $weightBrackets = $obj->WeightBrackets(); |
||
| 461 | $subTotalBrackets = $obj->SubTotalBrackets(); |
||
| 462 | |||
| 463 | // zero becauase over minForZeroRate |
||
| 464 | if ($minForZeroRate > 0 && $minForZeroRate < $subTotalAmount) { |
||
| 465 | self::$_actual_charges = 0; |
||
| 466 | $this->debugMessage .= "<hr />Minimum Order Amount For Zero Rate: ".$obj->MinimumOrderAmountForZeroRate." is lower than amount ordered: ".self::$_actual_charges; |
||
| 467 | } |
||
| 468 | |||
| 469 | //zero because below maxForZeroRate |
||
| 470 | elseif ($maxForZeroRate > 0 && $maxForZeroRate > $subTotalAmount) { |
||
| 471 | self::$_actual_charges = 0; |
||
| 472 | $this->debugMessage .= "<hr />Maximum Order Amount For Zero Rate: ".$obj->FreeShippingUpToThisOrderAmount." is higher than amount ordered: ".self::$_actual_charges; |
||
| 473 | } else { |
||
| 474 | //examine weight brackets |
||
| 475 | if ($weight && $weightBrackets->count()) { |
||
| 476 | $this->debugMessage .= "<hr />there is weight: {$weight}gr."; |
||
| 477 | //weight brackets |
||
| 478 | $foundWeightBracket = null; |
||
| 479 | $weightBracketQuantity = 1; |
||
| 480 | $additionalWeightBracket = null; |
||
| 481 | $minimumMinimum = null; |
||
| 482 | $maximumMaximum = null; |
||
| 483 | foreach ($weightBrackets as $weightBracket) { |
||
| 484 | if ((! $foundWeightBracket) && ($weightBracket->MinimumWeight <= $weight) && ($weight <= $weightBracket->MaximumWeight)) { |
||
| 485 | $foundWeightBracket = $weightBracket; |
||
| 486 | } |
||
| 487 | //look for absolute min and max |
||
| 488 | if ($minimumMinimum === null || ($weightBracket->MinimumWeight > $minimumMinimum->MinimumWeight)) { |
||
| 489 | $minimumMinimum = $weightBracket; |
||
| 490 | } |
||
| 491 | if ($maximumMaximum === null || ($weightBracket->MaximumWeight > $maximumMaximum->MaximumWeight)) { |
||
| 492 | $maximumMaximum = $weightBracket; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | if (! $foundWeightBracket) { |
||
| 496 | if ($weight < $minimumMinimum->MinimumWeight) { |
||
| 497 | $foundWeightBracket = $minimumMinimum; |
||
| 498 | } elseif ($weight > $maximumMaximum->MaximumWeight) { |
||
| 499 | $foundWeightBracket = $maximumMaximum; |
||
| 500 | $weightBracketQuantity = floor($weight / $maximumMaximum->MaximumWeight); |
||
| 501 | $restWeight = $weight - ($maximumMaximum->MaximumWeight * $weightBracketQuantity); |
||
| 502 | $additionalWeightBracket = null; |
||
| 503 | foreach ($weightBrackets as $weightBracket) { |
||
| 504 | if (($weightBracket->MinimumWeight <= $restWeight) && ($restWeight <= $weightBracket->MaximumWeight)) { |
||
| 505 | $additionalWeightBracket = $weightBracket; |
||
| 506 | break; |
||
| 507 | } |
||
| 508 | } |
||
| 509 | } |
||
| 510 | } |
||
| 511 | //we found some applicable weight brackets |
||
| 512 | if ($foundWeightBracket) { |
||
| 513 | self::$_actual_charges += $foundWeightBracket->FixedCost * $weightBracketQuantity; |
||
| 514 | $this->debugMessage .= "<hr />found Weight Bracket (from {$foundWeightBracket->MinimumWeight}gr. to {$foundWeightBracket->MaximumWeight}gr.): \${$foundWeightBracket->FixedCost} ({$foundWeightBracket->Name}) from times $weightBracketQuantity"; |
||
| 515 | if ($additionalWeightBracket) { |
||
| 516 | self::$_actual_charges += $additionalWeightBracket->FixedCost; |
||
| 517 | $this->debugMessage .= "<hr />+ additional Weight Bracket (from {$additionalWeightBracket->MinimumWeight}gr. to {$additionalWeightBracket->MaximumWeight}gr.): \${$additionalWeightBracket->FixedCost} ({$foundWeightBracket->Name})"; |
||
| 518 | } |
||
| 519 | } |
||
| 520 | } |
||
| 521 | |||
| 522 | |||
| 523 | |||
| 524 | // weight based on multiplier ... |
||
| 525 | elseif ($weight && $obj->WeightMultiplier) { |
||
| 526 | // add weight based shipping |
||
| 527 | if (!$obj->WeightUnit) { |
||
| 528 | $obj->WeightUnit = 1; |
||
| 529 | } |
||
| 530 | $this->debugMessage .= "<hr />actual weight:".$weight." multiplier = ".$obj->WeightMultiplier." weight unit = ".$obj->WeightUnit." "; |
||
| 531 | //legacy fix |
||
| 532 | $units = ceil($weight / $obj->WeightUnit); |
||
| 533 | $weightCharge = $units * $obj->WeightMultiplier; |
||
| 534 | self::$_actual_charges += $weightCharge; |
||
| 535 | $this->debugMessage .= "<hr />weight charge: ".$weightCharge; |
||
| 536 | } |
||
| 537 | |||
| 538 | |||
| 539 | //examine price brackets |
||
| 540 | elseif ($subTotalAmount && $subTotalBrackets->count()) { |
||
| 541 | $this->debugMessage .= "<hr />there is subTotal: {$subTotalAmount} and subtotal brackets."; |
||
| 542 | //subTotal brackets |
||
| 543 | $foundSubTotalBracket = null; |
||
| 544 | foreach ($subTotalBrackets as $subTotalBracket) { |
||
| 545 | if ((! $foundSubTotalBracket) && ($subTotalBracket->MinimumSubTotal <= $subTotalAmount) && ($subTotalAmount <= $subTotalBracket->MaximumSubTotal)) { |
||
| 546 | $foundSubTotalBracket = $subTotalBracket; |
||
| 547 | break; |
||
| 548 | } |
||
| 549 | } |
||
| 550 | //we found some applicable subTotal brackets |
||
| 551 | if ($foundSubTotalBracket) { |
||
| 552 | self::$_actual_charges += $foundSubTotalBracket->FixedCost; |
||
| 553 | $this->debugMessage .= "<hr />found SubTotal Bracket (between {$foundSubTotalBracket->MinimumSubTotal} and {$foundSubTotalBracket->MaximumSubTotal}): \${$foundSubTotalBracket->FixedCost} ({$foundSubTotalBracket->Name}) "; |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | // add percentage |
||
| 558 | if ($obj->Percentage) { |
||
| 559 | $percentageCharge = $subTotalAmount * $obj->Percentage; |
||
| 560 | self::$_actual_charges += $percentageCharge; |
||
| 561 | $this->debugMessage .= "<hr />percentage charge: \$".$percentageCharge; |
||
| 562 | } |
||
| 563 | |||
| 564 | // add fixed price |
||
| 565 | if ($obj->FixedCost <> 0) { |
||
| 566 | self::$_actual_charges += $obj->FixedCost; |
||
| 567 | $this->debugMessage .= "<hr />fixed charge: \$". $obj->FixedCost; |
||
| 568 | } |
||
| 569 | } |
||
| 570 | //is it enough? |
||
| 571 | if (self::$_actual_charges < $obj->MinimumDeliveryCharge && $obj->MinimumDeliveryCharge > 0) { |
||
| 572 | $oldActualCharge = self::$_actual_charges; |
||
| 573 | self::$_actual_charges = $obj->MinimumDeliveryCharge; |
||
| 574 | $this->debugMessage .= "<hr />too little: actual charge: ".$oldActualCharge.", minimum delivery charge: ".$obj->MinimumDeliveryCharge; |
||
| 575 | } |
||
| 576 | // is it too much |
||
| 577 | if (self::$_actual_charges > $obj->MaximumDeliveryCharge && $obj->MaximumDeliveryCharge > 0) { |
||
| 578 | self::$_actual_charges = $obj->MaximumDeliveryCharge; |
||
| 579 | $this->debugMessage .= "<hr />too much: ".self::$_actual_charges.", maximum delivery charge is ".$obj->MaximumDeliveryCharge; |
||
| 580 | } |
||
| 581 | } else { |
||
| 582 | if (!$items) { |
||
| 583 | $this->debugMessage .= "<hr />no items present"; |
||
| 584 | } else { |
||
| 585 | $this->debugMessage .= "<hr />no delivery option available"; |
||
| 586 | } |
||
| 587 | } |
||
| 588 | $this->debugMessage .= "<hr />final score: \$".self::$_actual_charges; |
||
| 589 | //special case, we are using weight and there is no weight! |
||
| 590 | return self::$_actual_charges; |
||
| 591 | } |
||
| 592 | |||
| 684 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.