Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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]);
- }
- }
Add Comment
Please, Sign In to add comment