Skip to content

Commit

Permalink
Handle custom message names better
Browse files Browse the repository at this point in the history
resolves: #109
  • Loading branch information
prolic committed Nov 18, 2016
1 parent deb8f53 commit c211d30
Show file tree
Hide file tree
Showing 12 changed files with 10 additions and 166 deletions.
3 changes: 1 addition & 2 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ $commandBus->utilize($router);
An invoke strategy knows how a message handler can be invoked. You can register many invoke strategies at once depending on
how many different handler types you are using. The best way is to choose a convention and go with it. PSB ships with the invoke strategies
listed below. If your favorite convention is not there you can easily write your own invoke strategy
by extending `Prooph\ServiceBus\Plugin\InvokeStrategy\AbstractInvokeStrategy` and implementing the
`canInvoke` and `invoke` methods.
by extending `Prooph\ServiceBus\Plugin\InvokeStrategy\AbstractInvokeStrategy` and implementing the `invoke` method.

### Available Strategies

Expand Down
16 changes: 2 additions & 14 deletions src/Plugin/InvokeStrategy/AbstractInvokeStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ abstract class AbstractInvokeStrategy implements ActionEventListenerAggregate
/**
* @param mixed $handler
* @param mixed $message
*
* @return bool
*/
abstract protected function canInvoke($handler, $message): bool;

/**
* @param mixed $handler
* @param mixed $message
*
* @return void
*/
abstract protected function invoke($handler, $message): void;

Expand All @@ -53,9 +43,7 @@ public function __invoke(ActionEvent $e): void
$message = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE);
$handler = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER);

if ($this->canInvoke($handler, $message)) {
$this->invoke($handler, $message);
$e->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, true);
}
$this->invoke($handler, $message);
$e->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, true);
}
}
23 changes: 2 additions & 21 deletions src/Plugin/InvokeStrategy/FinderInvokeStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\Common\Event\ActionEventListenerAggregate;
use Prooph\Common\Event\DetachAggregateHandlers;
use Prooph\Common\Messaging\HasMessageName;
use Prooph\ServiceBus\MessageBus;
use Prooph\ServiceBus\QueryBus;

Expand All @@ -38,26 +37,8 @@ public function __invoke(ActionEvent $actionEvent): void
$deferred = $actionEvent->getParam(QueryBus::EVENT_PARAM_DEFERRED);

if (is_object($finder)) {
$queryName = $this->determineQueryName($query);

if (method_exists($finder, $queryName)) {
$finder->{$queryName}($query, $deferred);
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, true);
}
$finder->find($query, $deferred);
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, true);
}
}

/**
* @param mixed $query
*
* @return string
*/
private function determineQueryName($query): string
{
$queryName = ($query instanceof HasMessageName)
? $query->messageName()
: (is_object($query) ? get_class($query) : gettype($query));

return implode('', array_slice(explode('\\', $queryName), -1));
}
}
37 changes: 1 addition & 36 deletions src/Plugin/InvokeStrategy/HandleCommandStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,8 @@

namespace Prooph\ServiceBus\Plugin\InvokeStrategy;

use Prooph\Common\Messaging\HasMessageName;

class HandleCommandStrategy extends AbstractInvokeStrategy
{
/**
* @param mixed $handler
* @param mixed $message
*
* @return bool
*/
public function canInvoke($handler, $message): bool
{
$handleMethod = 'handle' . $this->determineCommandName($message);

return method_exists($handler, $handleMethod) || method_exists($handler, 'handle');
}

/**
* @param mixed $handler
* @param mixed $message
Expand All @@ -37,26 +22,6 @@ public function canInvoke($handler, $message): bool
*/
public function invoke($handler, $message): void
{
$handleMethod = 'handle' . $this->determineCommandName($message);

if (method_exists($handler, $handleMethod)) {
$handler->{$handleMethod}($message);
} else {
$handler->handle($message);
}
}

/**
* @param mixed $message
*
* @return string
*/
protected function determineCommandName($message): string
{
$eventName = ($message instanceof HasMessageName)
? $message->messageName()
: (is_object($message)? get_class($message): gettype($message));

return implode('', array_slice(explode('\\', $eventName), -1));
$handler->handle($message);
}
}
29 changes: 1 addition & 28 deletions src/Plugin/InvokeStrategy/OnEventStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,14 @@

namespace Prooph\ServiceBus\Plugin\InvokeStrategy;

use Prooph\Common\Messaging\HasMessageName;

class OnEventStrategy extends AbstractInvokeStrategy
{
/**
* @param mixed $handler
* @param mixed $message
*/
public function canInvoke($handler, $message): bool
{
$handleMethod = 'on' . $this->determineEventName($message);

return method_exists($handler, $handleMethod);
}

/**
* @param mixed $handler
* @param mixed $message
*/
public function invoke($handler, $message): void
{
$handleMethod = 'on' . $this->determineEventName($message);

$handler->{$handleMethod}($message);
}

/**
* @param mixed $event
*/
protected function determineEventName($event): string
{
$eventName = ($event instanceof HasMessageName)
? $event->messageName()
: (is_object($event)? get_class($event): gettype($event));

return implode('', array_slice(explode('\\', $eventName), -1));
$handler->onEvent($message);
}
}
2 changes: 1 addition & 1 deletion tests/Mock/CustomMessageCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CustomMessageCommandHandler
{
private $lastMessage;

public function handleCustomMessage($message): void
public function handle($message): void
{
$this->lastMessage = $message;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Mock/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Finder

private $deferred;

public function customMessage($message, $deferred)
public function find($message, $deferred)
{
$this->message = $message;
$this->deferred = $deferred;
Expand Down
2 changes: 1 addition & 1 deletion tests/Mock/MessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function handle($message): void
$this->invokeCounter++;
}

public function onCustomMessage($message): void
public function onEvent($message): void
{
$this->lastMessage = $message;
$this->invokeCounter++;
Expand Down
5 changes: 0 additions & 5 deletions tests/Plugin/InvokeStrategy/AbstractInvokeStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ public function it_fetches_message_and_handler_and_invokes_them_if_possible(): v


$strategy = $this->getMockForAbstractClass(AbstractInvokeStrategy::class);
$strategy
->expects($this->once())
->method('canInvoke')
->with('handler', 'message')
->will($this->returnValue(true));

$strategy
->expects($this->once())
Expand Down
17 changes: 0 additions & 17 deletions tests/Plugin/InvokeStrategy/FinderInvokeStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Prooph\ServiceBus\Plugin\InvokeStrategy\FinderInvokeStrategy;
use Prooph\ServiceBus\QueryBus;
use ProophTest\ServiceBus\Mock\CustomMessage;
use ProophTest\ServiceBus\Mock\CustomMessageWithName;
use ProophTest\ServiceBus\Mock\Finder;
use React\Promise\Deferred;

Expand Down Expand Up @@ -60,20 +59,4 @@ public function it_invokes_a_finder_which_has_method_named_like_the_query(): voi
$this->assertSame($this->actionEvent->getParam(QueryBus::EVENT_PARAM_DEFERRED), $finder->getLastDeferred());
$this->assertTrue($this->actionEvent->getParam(QueryBus::EVENT_PARAM_MESSAGE_HANDLED));
}

/**
* @test
*/
public function it_determines_the_query_name_from_message_name_call_if_event_has_one(): void
{
$finderInvokeStrategy = new FinderInvokeStrategy();
$customQuery = new CustomMessageWithName("I am an event with a messageName() method");

$closure = function ($query) {
return $this->determineQueryName($query);
};
$determineQueryName = $closure->bindTo($finderInvokeStrategy, $finderInvokeStrategy);

$this->assertSame('CustomMessageWithSomeOtherName', $determineQueryName($customQuery));
}
}
21 changes: 0 additions & 21 deletions tests/Plugin/InvokeStrategy/HandleCommandStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Prooph\ServiceBus\Plugin\InvokeStrategy\HandleCommandStrategy;
use ProophTest\ServiceBus\Mock\CustomMessage;
use ProophTest\ServiceBus\Mock\CustomMessageCommandHandler;
use ProophTest\ServiceBus\Mock\CustomMessageWithName;
use ProophTest\ServiceBus\Mock\MessageHandler;
use ProophTest\ServiceBus\TestCase;

Expand All @@ -32,8 +31,6 @@ public function it_invokes_the_handle_command_method_of_the_handler(): void

$handleCommandHandler = new MessageHandler();

$this->assertTrue($handleCommandStrategy->canInvoke($handleCommandHandler, $doSomething));

$handleCommandStrategy->invoke($handleCommandHandler, $doSomething);

$this->assertSame($doSomething, $handleCommandHandler->getLastMessage());
Expand All @@ -50,26 +47,8 @@ public function it_invokes_the_handle_command_method_of_the_handler_without_comm

$handleCommandHandler = new CustomMessageCommandHandler();

$this->assertTrue($handleCommandStrategy->canInvoke($handleCommandHandler, $doSomething));

$handleCommandStrategy->invoke($handleCommandHandler, $doSomething);

$this->assertSame($doSomething, $handleCommandHandler->getLastMessage());
}

/**
* @test
*/
public function it_determines_the_command_name_from_message_name_call_if_event_has_one(): void
{
$handleCommandStrategy = new HandleCommandStrategy();
$customCommand = new CustomMessageWithName("I am an event with a messageName() method");

$closure = function ($command) {
return $this->determineCommandName($command);
};
$determineCommandName = $closure->bindTo($handleCommandStrategy, $handleCommandStrategy);

$this->assertSame('CustomMessageWithSomeOtherName', $determineCommandName($customCommand));
}
}
19 changes: 0 additions & 19 deletions tests/Plugin/InvokeStrategy/OnEventStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use Prooph\ServiceBus\Plugin\InvokeStrategy\OnEventStrategy;
use ProophTest\ServiceBus\Mock\CustomMessage;
use ProophTest\ServiceBus\Mock\CustomMessageWithName;
use ProophTest\ServiceBus\Mock\MessageHandler;
use ProophTest\ServiceBus\TestCase;

Expand All @@ -31,26 +30,8 @@ public function it_invokes_the_on_event_method_of_the_handler(): void

$onEventHandler = new MessageHandler();

$this->assertTrue($onEventStrategy->canInvoke($onEventHandler, $customEvent));

$onEventStrategy->invoke($onEventHandler, $customEvent);

$this->assertSame($customEvent, $onEventHandler->getLastMessage());
}

/**
* @test
*/
public function it_determines_the_event_name_from_message_name_call_if_event_has_one(): void
{
$onEventStrategy = new OnEventStrategy();
$customEvent = new CustomMessageWithName("I am an event with a messageName() method");

$closure = function ($event) {
return $this->determineEventName($event);
};
$determineEventName = $closure->bindTo($onEventStrategy, $onEventStrategy);

$this->assertSame('CustomMessageWithSomeOtherName', $determineEventName($customEvent));
}
}

0 comments on commit c211d30

Please sign in to comment.