Skip to content

Instantly share code, notes, and snippets.

@rochamarcelo
Last active August 29, 2015 14:08
Show Gist options
  • Save rochamarcelo/4129e4d1dc49a981b13e to your computer and use it in GitHub Desktop.
Save rochamarcelo/4129e4d1dc49a981b13e to your computer and use it in GitHub Desktop.
Bonus finder
<?php
$FindBonusCollection = new App\Bonus\FindBonusCollection;
$FindBonusCollection->add(new App\Bonus\FindBonusGamerXbox);
$FindBonusCollection->add(new App\Bonus\FindBonusGamerAll);
$FindBonusCollection->add(new App\Bonus\FindBonusPrime);
$FindProductBonus = new App\Bonus\FindProductBonus($FindBonusCollection);
$Product = new Product(25);
$ProductBonus = $FindProductBonus->find($Product);
$Product2 = new Product(135);
$ProductBonus2 = $FindProductBonus->find($Product2);
$FindBonusCollection = new App\Bonus\FindBonusCollection;
$FindBonusCollection->add(new App\Bonus\FindBonusGamer(new App\Bonus\FindBonusGamerXbox, App\Bonus\FindBonusGamerAll));
$FindBonusCollection->add(new App\Bonus\FindBonusPrime);
$Product3 = new Product(65);
$ProductBonus3 = $FindProductBonus->find($Product3);
<?php
namespace App\Bonus;
class FindBonusCollection implements Iterator
{
private $_finders = array();
public function add(IFindBonus $finder)
{
$this->_finders[] = $finder;
}
//Iterator methods
}
<?php
namespace App\Bonus;
class FindBonusGamer implements IFindBonus
{
private $_xbox;
private $_all;
public function __construct(FindBonusGamerXbox $xbox, FindBonusGamerAll $all)
{
$this->_xbox = $xbox;
$this->_all = $all;
}
public function find(IProduct $product)
{
try {
return $this->_xbox->find($product);
} catch (DomainException $e ) {
return $this->_all->find($product);
}
}
}
<?php
namespace App\Bonus;
class FindBonusGamerAll implements IFindBonus
{
public function find(IProduct $product)
{
//Find a bonus for gamers
return new Bonus(
'Halloween 2014',//Campaign
30.00,//bonus money
3000,//bonus points
);
}
}
<?php
namespace App\Bonus;
class FindBonusGamerXbox implements IFindBonus
{
public function find(IProduct $product)
{
//Find a bonus for gamers
return new Bonus(
'New games',//Campaign
50.00,//bonus money
2500,//bonus points
);
}
}
<?php
namespace App\Bonus;
class FindBonusPrime implements IFindBonus
{
public function find(IProduct $product)
{
//Find a prime bonus active
return new Bonus(
'New Year\'s Eve',//Campaign
1000.00,//bonus
15000, //Bonus points
);
}
}
<?php
namespace App\Bonus;
class FindProductBonus implements IFindProductBonus
{
private $_collection;
public function __construct(FindBonusCollection $collection)
{
$this->_collection = $collection;
}
public function find($Product)
{
$ProductBonus = new ProductBonus($Product);
foreach ( $this->_collection as $finder ) {
try {
$ProductBonus->add($finder->find($Product));
} catch (DomainException $e) {
}
}
return $ProductBonus;
}
}
<?php
namespace App\Bonus;
interface IFindBonus
{
public function find(IProduct $product);
}
<?php
namespace App\Bonus;
class ProductBonus
{
private $_bonus = array();
private $_product;
public function __construct(Product $product)
{
$this->_product = $product;
}
public function add(Bonus $bonus)
{
$this->_bonus[] = $bonus;
}
public function getBonus()
{
return $this->_bonus;
}
public function getMoney()
{
$money = 0;
foreach ( $this->_bonus as $bonus ) {
$money = $bonus->getMoney();
}
return $money;
}
public function getPoints()
{
$points = 0;
foreach ( $this->_bonus as $bonus ) {
$points = $bonus->getPoints();
}
return $points;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment