Skip to content

Commit

Permalink
Merge pull request #261 from sagikazarmark/class_router
Browse files Browse the repository at this point in the history
Add classname router
  • Loading branch information
henrikbjorn authored Dec 15, 2016
2 parents 2513feb + 0e304ed commit 8941ead
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"pda/pheanstalk": "~3.0",
"league/container": "~1.0",
"php-amqplib/php-amqplib": "~2.5",
"phpspec/phpspec": "~2.0",
"phpspec/phpspec": "^2.4",
"phpunit/phpunit": "^4.8"
},
"suggest": {
Expand Down
40 changes: 40 additions & 0 deletions spec/Bernard/Router/ClassNameRouterSpec.php
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);
}
}
40 changes: 40 additions & 0 deletions src/Router/ClassNameRouter.php
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;
}
}

0 comments on commit 8941ead

Please sign in to comment.