-
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.
The main thing I did in this commit was to add an event dispatcher. I throw a domain event in my model when entering a room and needed the dispatcher in place to support that. As I am using Symfony it made sense to me to use the Symfony event dispatcher. However, it is a bit of a pain because it expects to have a name and to be `Symfony\Component\EventDispatcher\Event`, as I was trying to follow the clean architecture and have no outer dependencies I didn't want to have my event extend `Symfony\Component\EventDispatcher\Event`. Therefore, I needed to create an adapter. Going back to the Raspberry Pi analogy made in a previous version. I needed a serial to usb adapter, or something like that. I also wanted to use my own Dispatcher interface, because what if I wanted to stop using the Symfony dispatcher and replace it with something else? I want to be able to continue using the same interface but just add a new adapter and change my DI. You can see how I achieved this from the code within this commit. Finally, I added a `HandlerInterface` so that I can swap out handlers using DI should I need to.
- Loading branch information
Showing
16 changed files
with
235 additions
and
17 deletions.
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
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,34 @@ | ||
<?php | ||
|
||
namespace spec\Jenko\House\Handler; | ||
|
||
use Jenko\House\Command\EnterRoomCommand; | ||
use Jenko\House\Event\EventDispatcherInterface; | ||
use Jenko\House\Factory\HomeAloneHouseFactory; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class EnterRoomHandlerSpec extends ObjectBehavior | ||
{ | ||
function let(EventDispatcherInterface $dispatcher) | ||
{ | ||
$this->beConstructedWith($dispatcher); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Jenko\House\Handler\EnterRoomHandler'); | ||
} | ||
|
||
function it_dispatches_events(EventDispatcherInterface $dispatcher) | ||
{ | ||
$house = HomeAloneHouseFactory::getHouse(); | ||
$house->enterRoom('hallway'); | ||
|
||
$command = new EnterRoomCommand(); | ||
$command->room = 'kitchen'; | ||
|
||
$dispatcher->dispatch(Argument::any())->shouldBeCalled(); | ||
$this->handle($command); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace spec\Jenko\House\Handler; | ||
|
||
use Jenko\House\Command\ExitRoomCommand; | ||
use Jenko\House\Event\EventDispatcherInterface; | ||
use Jenko\House\Factory\HomeAloneHouseFactory; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class ExitRoomHandlerSpec extends ObjectBehavior | ||
{ | ||
function let(EventDispatcherInterface $dispatcher) | ||
{ | ||
$this->beConstructedWith($dispatcher); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Jenko\House\Handler\ExitRoomHandler'); | ||
} | ||
|
||
function it_dispatches_events(EventDispatcherInterface $dispatcher) | ||
{ | ||
$house = HomeAloneHouseFactory::getHouse(); | ||
$house->enterRoom('hallway'); | ||
|
||
$command = new ExitRoomCommand(); | ||
$command->room = 'kitchen'; | ||
|
||
$dispatcher->dispatch(Argument::any())->shouldBeCalled(); | ||
$this->handle($command); | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Adapter; | ||
|
||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class SymfonyEventAdapter extends Event | ||
{ | ||
|
||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Adapter; | ||
|
||
use Jenko\House\Event\EventDispatcherInterface as LocalEventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
class SymfonyEventDispatcherAdapter implements LocalEventDispatcherInterface | ||
{ | ||
/** | ||
* @var EventDispatcherInterface | ||
*/ | ||
protected $dispatcher; | ||
/** | ||
* @param EventDispatcherInterface $dispatcher | ||
*/ | ||
public function __construct(EventDispatcherInterface $dispatcher) | ||
{ | ||
$this->dispatcher = $dispatcher; | ||
} | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function dispatch(array $events) | ||
{ | ||
foreach ($events as $event) { | ||
$this->dispatcher->dispatch($event::NAME, $event); | ||
} | ||
} | ||
} |
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
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,12 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Event; | ||
|
||
interface EventDispatcherInterface | ||
{ | ||
/** | ||
* @param array $events | ||
* @return mixed | ||
*/ | ||
public function dispatch(array $events); | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Event; | ||
|
||
trait EventGenerator | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $pendingEvents = []; | ||
|
||
/** | ||
* Raise a new event | ||
* | ||
* @param $event | ||
*/ | ||
public function raiseEvent($event) | ||
{ | ||
$this->pendingEvents[] = $event; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function releaseEvents() | ||
{ | ||
$events = $this->pendingEvents; | ||
$this->pendingEvents = []; | ||
|
||
return $events; | ||
} | ||
} |
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
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
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
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
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,12 @@ | ||
<?php | ||
|
||
namespace Jenko\House\Handler; | ||
|
||
interface HandlerInterface | ||
{ | ||
/** | ||
* @param $command | ||
* @return mixed | ||
*/ | ||
public function handle($command); | ||
} |
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
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
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