Guest User

Untitled

a guest
Oct 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. <?php
  2.  
  3. use Foo\Cart;
  4. use Foo\CartItem;
  5. use Foo\FreshMilk;
  6. use Foo\ThisConditionReturnItemInterface;
  7.  
  8. class FreeFreshMilkWhenByAmericanoEspresso implements ThisConditionReturnItemInterface
  9. {
  10. $needToBuyBothToGetFreeFreshMilk = false;
  11.  
  12. public function setNeedToBuyBothToGetFreeFreshMilk(bool $flag)
  13. {
  14. $this->needToBuyBothToGetFreeFreshMilk = $flag;
  15. }
  16.  
  17. public function getItem(Cart $cart): CartItem
  18. {
  19. $amoutOfAmericano = $cart->items->find('Americano')->count();
  20. $amoutOfEspresso = $cart->items->find('Espresso')->count();
  21.  
  22. $amoutOfFreshMilkFromAmericano = $amoutOfAmericano / 2;
  23. $amoutOfFreshMilkFromEspresso = $amoutOfEspresso / 2;
  24.  
  25. if ($this->needToBuyBothToGetFreeFreshMilk) {
  26. $amoutOfFreshMilk = min($amoutOfFreshMilkFromAmericano, $amoutOfFreshMilkFromEspresso);
  27. } else {
  28. $amoutOfFreshMilk = max($amoutOfFreshMilkFromAmericano, $amoutOfFreshMilkFromEspresso);
  29. }
  30.  
  31. return new CartItem(new FreshMilk, ['quantity' => $amoutOfFreshMilk, 'price' => 0]);
  32. }
  33. }
Add Comment
Please, Sign In to add comment