-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So as you may have noticed I've been avoiding persistence throughout this. It's a small, simple, contrived example which I didn't want to muddy by adding any means of persistence. I realise this perhaps removes validity to the example, but I don't really care. This is more about how to model a domain with a modelling by example approach, as opposed to the intricacies of database persistence with DDD. However, I did want a tidier way of dealing with the state of the House. Building the house in the constructor every time wasn't ideal. So I came up with this idea of a factory. Because we all know houses come from factories right? In essence it's more of a singleton, which works for me here. I'm stil building the house on every request, but at least this does it in a clear and re-usable manner. It also doesn't get me bogged down in persistence when it's not my primary aim here. The final thing to note is that I added some basic handlers to handle the commands I created earlier. These are likely to change but are fine for the time being.
- Loading branch information
Showing
6 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace spec\Jenko\House\Factory; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class HomeAloneHouseFactorySpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Jenko\House\Factory\HomeAloneHouseFactory'); | ||
$this->shouldHaveType('Jenko\House\Factory\HouseFactory'); | ||
} | ||
|
||
function it_should_return_a_house() | ||
{ | ||
self::getHouse()->shouldHaveType('Jenko\House\House'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Factory; | ||
|
||
use Jenko\House\Garden; | ||
use Jenko\House\House; | ||
use Jenko\House\Room; | ||
|
||
/** | ||
* Note: Usually you would have some kind of persistence mechanism. However, in our case a static factory (essentially | ||
* a singleton) is fine, as the house itself won't change. | ||
*/ | ||
final class HomeAloneHouseFactory implements HouseFactory | ||
{ | ||
/** | ||
* @var House $house | ||
*/ | ||
private static $house; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getHouse($currentLocation = null) | ||
{ | ||
if (isset(static::$house)) { | ||
return static::$house; | ||
} | ||
|
||
$locations = [ | ||
Garden::named('front garden'), | ||
Room::named('hallway'), | ||
Room::named('living room'), | ||
Room::named('kitchen'), | ||
]; | ||
|
||
static::$house = House::build($locations); | ||
|
||
if (null !== $currentLocation) { | ||
static::$house->enterRoom($currentLocation); | ||
} | ||
|
||
return static::$house; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Factory; | ||
|
||
use Jenko\House\House; | ||
use Jenko\House\Location; | ||
|
||
interface HouseFactory | ||
{ | ||
/** | ||
* @param Location|string|null $currentLocation | ||
* @return House | ||
*/ | ||
public static function getHouse($currentLocation = null); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Handler; | ||
|
||
use Jenko\House\Factory\HomeAloneHouseFactory; | ||
|
||
final class EnterRoomHandler | ||
{ | ||
private $house; | ||
|
||
public function __construct() | ||
{ | ||
$this->house = HomeAloneHouseFactory::getHouse(); | ||
} | ||
|
||
/** | ||
* @param $command | ||
* @return mixed|void | ||
*/ | ||
public function handle($command) | ||
{ | ||
$this->house->enterRoom($command->room); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Handler; | ||
|
||
use Jenko\House\Factory\HomeAloneHouseFactory; | ||
|
||
final class ExitRoomHandler | ||
{ | ||
private $house; | ||
|
||
public function __construct() | ||
{ | ||
$this->house = HomeAloneHouseFactory::getHouse(); | ||
} | ||
|
||
/** | ||
* @param $command | ||
* @return mixed|void | ||
*/ | ||
public function handle($command) | ||
{ | ||
$this->house->exitToRoom($command->room); | ||
} | ||
} |
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