diff --git a/composer.json b/composer.json index 11b6c90a..b7753c14 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/spec/Bernard/Router/ClassNameRouterSpec.php b/spec/Bernard/Router/ClassNameRouterSpec.php new file mode 100644 index 00000000..298700ee --- /dev/null +++ b/spec/Bernard/Router/ClassNameRouterSpec.php @@ -0,0 +1,40 @@ +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); + } +} diff --git a/src/Router/ClassNameRouter.php b/src/Router/ClassNameRouter.php new file mode 100644 index 00000000..c2480fa7 --- /dev/null +++ b/src/Router/ClassNameRouter.php @@ -0,0 +1,40 @@ +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; + } +}