Created
March 21, 2014 02:41
-
-
Save clarkf/9678425 to your computer and use it in GitHub Desktop.
A really bad Dependency Injection example
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 Acme\CookieRobot\Controllers; | |
use Acme\FoodInventory; | |
class EggProcurementController extends BaseController | |
{ | |
/** | |
* @type Acme\FoodInventory | |
*/ | |
protected $inventory; | |
/** | |
* Construct a new EggProcurementController | |
* | |
* @param Acme\FoodInventory $inventory | |
*/ | |
public function __construct(FoodInventory $inventory) | |
{ | |
$this->inventory = $inventory; | |
} | |
/** | |
* Procure some amount of eggs | |
* | |
* @param int $quantity The number of eggs to procure | |
* | |
* @return Acme\Egg[] An array of eggs | |
*/ | |
public function procure($quantity = 2) | |
{ | |
// Silly pseudo-code follows... | |
if (!$this->inventory->has(FoodInventory::EGG, $quantity)) { | |
throw new InvalidArgumentException( | |
"Unable to locate $quantity eggs!" | |
); | |
} | |
$this->moveTo($this->inventory->locationOf(FoodInventory::EGG)); | |
$eggs = $this->acquire(FoodInventory::EGG, $quantity); | |
$this->moveBack(); | |
return $eggs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment