Skip to content

Instantly share code, notes, and snippets.

@Biont
Created October 28, 2018 19:42
Show Gist options
  • Save Biont/f302c76af8747065816912f2346952e4 to your computer and use it in GitHub Desktop.
Save Biont/f302c76af8747065816912f2346952e4 to your computer and use it in GitHub Desktop.
PHP Hush Little Baby
<?php
class Lullaby
{
/**
* @var HumanoidInterface
*/
private $littleBaby;
/**
* @var HumanoidInterface
*/
private $papa;
/**
* @var ContainerInterface
*/
private $rewards;
/**
* @var HumanoidCollectionInterface
*/
private $town;
private const EVENTS = [
'mockingbird.wont_sing' => 'diamond_ring',
'diamond_ring.turns_brass' => 'looking_glass',
'looking_glass.gets_broke' => 'billy_goat',
'billy_goat.wont_pull' => 'cart_and_bull',
'cart_and_bull.turn_over' => 'dog_named_rover',
'dog_named_rover.wont_bark' => 'horse_and_cart',
'horse_and_cart.fall_down' => null,
];
/**
* @var EventBroadCasterInterface
*/
private $eventBroadcaster;
public function __construct(
HumanoidInterface $littleBaby,
ContainerInterface $rewards,
HumanoidCollectionInterface $town,
EventBroadCasterInterface $eventBroadcaster
)
{
$this->littleBaby = $littleBaby;
$this->papa = $littleBaby->getParent();
$this->rewards = $rewards;
$this->town = $town;
$this->eventBroadcaster = $eventBroadcaster;
}
function hush()
{
ob_start(); // Don't say a word
$this->papa->buy($this->rewards->get('mockingbird'));
array_walk($events, function ($value, $eventName) {
$this->eventBroadcaster->on($eventName, function () use ($eventName, $value) {
if (!array_key_exists($eventName, self::EVENTS)) {
return;
}
$reward = self::EVENTS[$eventName];
if (null !== $reward) {
$this->papa->buy($this->rewards->get($reward));
} else {
$this->town->setSweetestBaby($this->littleBaby);
}
});
});
ob_end_clean();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment