-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from sagikazarmark/class_router
Add classname router
- Loading branch information
Showing
3 changed files
with
81 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
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,40 @@ | ||
<?php | ||
|
||
namespace spec\Bernard\Router; | ||
|
||
use Bernard\Envelope; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class ClassNameRouterSpec extends ObjectBehavior | ||
{ | ||
function let() | ||
{ | ||
$this->beConstructedWith([ | ||
'Bernard\\Message' => function() {}, | ||
]); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Bernard\\Router\\ClassNameRouter'); | ||
} | ||
|
||
function it_is_a_router() | ||
{ | ||
$this->shouldImplement('Bernard\\Router'); | ||
} | ||
|
||
function it_maps_an_envelope(Envelope $envelope) | ||
{ | ||
$envelope->getClass()->willReturn('Bernard\\Message\\DefaultMessage'); | ||
|
||
$this->map($envelope)->shouldBeCallable(); | ||
} | ||
|
||
function it_throws_an_exception_when_envelope_cannot_be_mapped(Envelope $envelope) | ||
{ | ||
$envelope->getClass()->willReturn('Bernard\\Producer'); | ||
|
||
$this->shouldThrow('Bernard\\Exception\\ReceiverNotFoundException')->duringMap($envelope); | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace Bernard\Router; | ||
|
||
use Bernard\Envelope; | ||
use Bernard\Exception\ReceiverNotFoundException; | ||
|
||
/** | ||
* @package Bernard | ||
*/ | ||
class ClassNameRouter extends SimpleRouter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function map(Envelope $envelope) | ||
{ | ||
$receiver = $this->get($envelope->getClass()); | ||
|
||
if (!is_callable($receiver)) { | ||
throw new ReceiverNotFoundException(sprintf('No receiver found for class "%s".', $envelope->getClass())); | ||
} | ||
|
||
return $receiver; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function get($name) | ||
{ | ||
foreach ($this->receivers as $key => $receiver) { | ||
if (is_a($name, $key, true)) { | ||
return $receiver; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |