Last active
August 29, 2015 14:08
-
-
Save rochamarcelo/4129e4d1dc49a981b13e to your computer and use it in GitHub Desktop.
Bonus finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Bonus; | |
class FindBonusCollection implements Iterator | |
{ | |
private $_finders = array(); | |
public function add(IFindBonus $finder) | |
{ | |
$this->_finders[] = $finder; | |
} | |
//Iterator methods | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Bonus; | |
interface IFindBonus | |
{ | |
public function find(IProduct $product); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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