Skip to content

Instantly share code, notes, and snippets.

@sirawitpra
Created October 17, 2017 04:53
Show Gist options
  • Save sirawitpra/a7fbce6a35f987ce87f68b508a3a2ace to your computer and use it in GitHub Desktop.
Save sirawitpra/a7fbce6a35f987ce87f68b508a3a2ace to your computer and use it in GitHub Desktop.
<?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