Skip to content

Commit

Permalink
Version19
Browse files Browse the repository at this point in the history
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
jenkoian committed Jan 20, 2015
1 parent e4ace65 commit 0952bb4
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 1 deletion.
20 changes: 20 additions & 0 deletions spec/Jenko/House/Factory/HomeAloneHouseFactorySpec.php
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');
}
}
44 changes: 44 additions & 0 deletions src/Jenko/House/Factory/HomeAloneHouseFactory.php
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;
}
}
15 changes: 15 additions & 0 deletions src/Jenko/House/Factory/HouseFactory.php
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);
}
24 changes: 24 additions & 0 deletions src/Jenko/House/Handler/EnterRoomHandler.php
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);
}
}
24 changes: 24 additions & 0 deletions src/Jenko/House/Handler/ExitRoomHandler.php
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);
}
}
2 changes: 1 addition & 1 deletion src/Jenko/House/House.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function build(array $locations)
}

/**
* @return array
* @return Location[]|array
*/
public function getLocations()
{
Expand Down

0 comments on commit 0952bb4

Please sign in to comment.