Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update handle command strategy #71

Merged
merged 3 commits into from
Sep 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Plugin/InvokeStrategy/HandleCommandStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Prooph\ServiceBus\Plugin\InvokeStrategy;

use Prooph\Common\Messaging\HasMessageName;

/**
* Class HandleCommandStrategy
*
Expand All @@ -27,7 +29,9 @@ class HandleCommandStrategy extends AbstractInvokeStrategy
*/
public function canInvoke($handler, $message)
{
return method_exists($handler, 'handle');
$handleMethod = 'handle' . $this->determineCommandName($message);

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

/**
Expand All @@ -36,6 +40,22 @@ public function canInvoke($handler, $message)
*/
public function invoke($handler, $message)
{
$handler->handle($message);
$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)
{
$eventName = ($message instanceof HasMessageName)? $message->messageName() : is_object($message)? get_class($message) : gettype($message);
return implode('', array_slice(explode('\\', $eventName), -1));
}
}
31 changes: 31 additions & 0 deletions tests/Mock/CustomMessageCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 08/02/15 - 8:38 PM
*/

namespace Prooph\ServiceBusTest\Mock;

/**
* Class CustomMessageCommandHandler
* @package Prooph\ServiceBusTest\Mock
*/
final class CustomMessageCommandHandler
{
private $lastMessage;

public function handleCustomMessage($message)
{
$this->lastMessage = $message;
}

public function getLastMessage()
{
return $this->lastMessage;
}
}
19 changes: 19 additions & 0 deletions tests/Plugin/InvokeStrategy/HandleCommandStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Prooph\ServiceBus\Plugin\InvokeStrategy\HandleCommandStrategy;
use Prooph\ServiceBusTest\Mock\CustomMessage;
use Prooph\ServiceBusTest\Mock\CustomMessageCommandHandler;
use Prooph\ServiceBusTest\Mock\MessageHandler;
use Prooph\ServiceBusTest\TestCase;

Expand Down Expand Up @@ -41,4 +42,22 @@ public function it_invokes_the_handle_command_method_of_the_handler()

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

/**
* @test
*/
public function it_invokes_the_handle_command_method_of_the_handler_without_command_name()
{
$handleCommandStrategy = new HandleCommandStrategy();

$doSomething = new CustomMessage("I am a command");

$handleCommandHandler = new CustomMessageCommandHandler();

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

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

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