Created
April 17, 2017 17:35
-
-
Save carlosbelisario/59da255945bae03da22dbb277769895a to your computer and use it in GitHub Desktop.
refactor del la clase Order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Se aplico la técnica de extract method al metodo calculate dejandolo con | |
* la responsabilidad de solo hacer el calculo y dejandolo legible y eliminandole complejidad ciclica | |
* | |
* Class Order | |
*/ | |
class Order | |
{ | |
/** | |
* @return float | |
*/ | |
public function calculate() | |
{ | |
$details = $this->getDetail(); | |
$total = 0; | |
foreach ($details as $detail) { | |
$vat = $this->getVat($detail); | |
$price = $this->getPrice($detail); | |
$total += $price + ($price/100 * $vat->getValue()); | |
} | |
$discountPercent = $this->getDiscountPercent($total); | |
return $total - $discountPercent; | |
} | |
/** | |
* @param $detail | |
* @return mixed | |
*/ | |
public function getVat($detail) | |
{ | |
return !$detail->hasVat() ? $this->getCustomer()->getVat() : $detail->getVat(); | |
} | |
public function getPrice($detail) | |
{ | |
return $detail->getAmount() * $detail->getPrice(); | |
} | |
/** | |
* @param $customer | |
* @param $total | |
* @return float | |
*/ | |
public function getDiscountPercentForCustomer($customer, $total) | |
{ | |
if ($this->hasDiscount()) { | |
return ($total/100) * $this->getDiscount(); | |
} elseif ($this->hasPercentDiscountForMaxAmount($customer, $total)) { | |
return ($total/100) * $this->getDiscount($customer); | |
} | |
} | |
/** | |
* @param $customer | |
* @param $total | |
* @return bool | |
*/ | |
public function hasPercentDiscountForCustomerMaxAmount($customer, $total) | |
{ | |
return $customer->hasDiscountForMaxAmount() && $total >= $customer->getDiscountForMaxAmount(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment