Skip to content

Instantly share code, notes, and snippets.

@chukShirley
Created April 21, 2014 14:21
Show Gist options
  • Save chukShirley/11144149 to your computer and use it in GitHub Desktop.
Save chukShirley/11144149 to your computer and use it in GitHub Desktop.
Calculation best practice
class Product
{
protected $unitPrice;
protected $discount;
protected $finalPrice;
public function __construct($unitPrice, $discount)
{
$this->setUnitPrice($unitPrice);
$this->setDiscount($discount);
}
//...getters and setters
}
class ProductController
{
public function storeAction()
{
$product = new Product(12,4);
$this->getProductFacade()->calculateFinalPrice($product);
// ...store $product in db
}
public function getProductFacade()
{
return new ProductFacade();
}
}
class ProductFacade
{
public function calculateFinalPrice(Product $product)
{
$finalPrice = $product->getUnitPrice() - $product->getDiscount();
$product->setFinalPrice($finalPrice);
return $product->getFinalPrice();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment