Created
October 17, 2017 04:53
-
-
Save sirawitpra/a7fbce6a35f987ce87f68b508a3a2ace to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
use Foo\Cart; | |
use Foo\CartItem; | |
use Foo\FreshMilk; | |
use Foo\ThisConditionReturnItemInterface; | |
class FreeFreshMilkWhenByAmericanoEspresso implements ThisConditionReturnItemInterface | |
{ | |
$needToBuyBothToGetFreeFreshMilk = false; | |
public function setNeedToBuyBothToGetFreeFreshMilk(bool $flag) | |
{ | |
$this->needToBuyBothToGetFreeFreshMilk = $flag; | |
} | |
public function getItem(Cart $cart): CartItem | |
{ | |
$amoutOfAmericano = $cart->items->find('Americano')->count(); | |
$amoutOfEspresso = $cart->items->find('Espresso')->count(); | |
$amoutOfFreshMilkFromAmericano = $amoutOfAmericano / 2; | |
$amoutOfFreshMilkFromEspresso = $amoutOfEspresso / 2; | |
if ($this->needToBuyBothToGetFreeFreshMilk) { | |
$amoutOfFreshMilk = min($amoutOfFreshMilkFromAmericano, $amoutOfFreshMilkFromEspresso); | |
} else { | |
$amoutOfFreshMilk = max($amoutOfFreshMilkFromAmericano, $amoutOfFreshMilkFromEspresso); | |
} | |
return new CartItem(new FreshMilk, ['quantity' => $amoutOfFreshMilk, 'price' => 0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment