Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Calisthenics version of a client, a barman, an order and some beer!
<?php
class drink
{
private $alcool = '';
private $pourcent = 0;
public function __construct($alcool, $pourcent)
{
$this->fill($alcool, $pourcent);
}
public function __toString()
{
return $this->alcool . ': ' . $this->pourcent . '%';
}
public function fill($alcool, $pourcent)
{
$this->alcool = $alcool;
$this->pourcent = $pourcent;
return $this;
}
}
class drinks
{
private $drinks = array();
public function add(drink $drink)
{
$this->drinks[] = $drink;
return $this;
}
public function each(callable $callable)
{
foreach ($this->drinks as $drink)
{
$callable($drink);
}
return $this;
}
}
class order
{
private $numberOfDrinks = 0;
private $alcool = '';
public function __construct($numberOfDrinks, $alcool)
{
$this->numberOfDrinks = $numberOfDrinks;
$this->alcool = $alcool;
}
public function preparedBy(barman $barman)
{
for ($numberOfDrinks = $this->numberOfDrinks; $numberOfDrinks > 0; $numberOfDrinks--)
{
$barman->createDrink($this->alcool);
}
return $this;
}
public function computePrice(tpe $tpe)
{
for ($numberOfDrinks = $this->numberOfDrinks; $numberOfDrinks > 0; $numberOfDrinks--)
{
$tpe->add(6);
}
return $this;
}
}
class client
{
private $drinks = null;
private $creditCardNumber = '';
public function __construct()
{
$this->drinks = new drinks;
$this->creditCardNumber = uniqid();
}
public function order($numberOfDrinks, $alcool, $barman)
{
$barman->takeOrder(new order($numberOfDrinks, $alcool));
return $this;
}
public function receiveDrink(drink $drink)
{
$this->drinks->add($drink);
return $this;
}
public function showDrinks()
{
$this->drinks->each(array($this, 'showDrink'));
return $this;
}
public function showDrink($drink)
{
echo $drink . PHP_EOL;
}
public function validateBankTransaction(tpe $tpe)
{
$tpe->execute($this->creditCardNumber);
return $this;
}
}
class tpe
{
private $price = 0;
public function add($drinkPrice)
{
$this->price += $drinkPrice;
}
public function validate(client $client)
{
$client->validateTpe($this);
return $this;
}
public function execute($creditCardNumber)
{
echo 'Just pay ' . $this->price . ' with card ' . $creditCardNumber . '!' . PHP_EOL;
return $this;
}
}
class barman
{
private $order = null;
private $drinks = null;
public function takeOrder(order $order)
{
$this->order = $order;
if ($this->drinks !== null)
{
$this->prepareOrder();
}
return $this;
}
public function prepareOrder()
{
$this->drinks = new drinks();
$this->order->preparedBy($this);
return $this;
}
public function createDrink($alcool, $pourcent = 75)
{
$this->drinks->add(new drink($alcool, $pourcent));
return $this;
}
public function giveDrinks(client $client)
{
$this->drinks->each(array($client, 'receiveDrink'));
$this->drinks = null;
return $this;
}
public function billDrinks(client $client)
{
$tpe = new tpe();
$this->order->computePrice($tpe);
$client->validateBankTransaction($tpe);
$this->order = null;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment