Skip to content

Commit

Permalink
Add CommandFactoryLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
codeliner committed Jul 5, 2014
1 parent 11ca278 commit c46aabb
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 35 deletions.
59 changes: 59 additions & 0 deletions src/Prooph/ServiceBus/Command/AbstractCommandFactoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) Alexander Miertsch <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 05.07.14 - 22:51
*/

namespace Prooph\ServiceBus\Command;

use Prooph\ServiceBus\Service\CommandFactoryLoader;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Class AbstractCommandFactoryFactory
*
* @package Prooph\ServiceBus\Command
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class AbstractCommandFactoryFactory implements AbstractFactoryInterface
{
protected $commandFactory;

/**
* Determine if we can create a service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if ($serviceLocator instanceof CommandFactoryLoader) {
return true;
}
}

/**
* Create service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if (is_null($this->commandFactory)) {
$this->commandFactory = new CommandFactory();
}
return $this->commandFactory;
}
}

23 changes: 10 additions & 13 deletions src/Prooph/ServiceBus/Command/CommandReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Prooph\ServiceBus\Exception\RuntimeException;
use Prooph\ServiceBus\Message\MessageInterface;
use Prooph\ServiceBus\Service\CommandFactoryLoader;
use Prooph\ServiceBus\Service\Definition;
use Prooph\ServiceBus\Service\InvokeStrategyLoader;
use Zend\EventManager\EventManager;
Expand All @@ -33,9 +34,9 @@ class CommandReceiver implements CommandReceiverInterface
protected $commandMap = array();

/**
* @var CommandFactoryInterface
* @var CommandFactoryLoader
*/
protected $commandFactory;
protected $commandFactoryLoader;

/**
* @var ServiceLocatorInterface
Expand Down Expand Up @@ -85,7 +86,7 @@ public function handle(MessageInterface $aMessage)
return;
}

$command = $this->getCommandFactory()->fromMessage($aMessage);
$command = $this->getCommandFactoryLoader()->get($aMessage->name())->fromMessage($aMessage);

$handler = $this->commandHandlerLocator->get($this->commandMap[$aMessage->name()]);

Expand Down Expand Up @@ -128,23 +129,19 @@ public function handle(MessageInterface $aMessage)
}

/**
* @param CommandFactoryInterface $aCommandFactory
* @param CommandFactoryLoader $aCommandFactoryLoader
*/
public function setCommandFactory(CommandFactoryInterface $aCommandFactory)
public function setCommandFactoryLoader(CommandFactoryLoader $aCommandFactoryLoader)
{
$this->commandFactory = $aCommandFactory;
$this->commandFactoryLoader = $aCommandFactoryLoader;
}

/**
* @return CommandFactoryInterface
* @return CommandFactoryLoader
*/
public function getCommandFactory()
public function getCommandFactoryLoader()
{
if (is_null($this->commandFactory)) {
$this->commandFactory = new CommandFactory();
}

return $this->commandFactory;
return $this->commandFactoryLoader;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
);
}

if ($mainServiceLocator->has(Definition::COMMAND_FACTORY)) {
$commandReceiver->setCommandFactory($mainServiceLocator->get(Definition::COMMAND_FACTORY));
}
$commandReceiver->setCommandFactoryLoader($mainServiceLocator->get(Definition::COMMAND_FACTORY_LOADER));

return $commandReceiver;
}
Expand Down
59 changes: 59 additions & 0 deletions src/Prooph/ServiceBus/Service/CommandFactoryLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) Alexander Miertsch <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 05.07.14 - 22:30
*/

namespace Prooph\ServiceBus\Service;

use Prooph\ServiceBus\Command\AbstractCommandFactoryFactory;
use Prooph\ServiceBus\Command\CommandFactoryInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\Exception;

/**
* Class CommandFactoryLoader
*
* @package Prooph\ServiceBus\Service
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class CommandFactoryLoader extends AbstractPluginManager
{
/**
* @param ConfigInterface $aConfig
*/
public function __construct(ConfigInterface $aConfig = null)
{
parent::__construct($aConfig);

$this->abstractFactories[] = new AbstractCommandFactoryFactory();
}

/**
* Validate the plugin
*
* Checks that the filter loaded is either a valid callback or an instance
* of FilterInterface.
*
* @param mixed $plugin
* @throws Exception\RuntimeException
* @return void
*/
public function validatePlugin($plugin)
{
if (! $plugin instanceof CommandFactoryInterface) {
throw new Exception\RuntimeException(sprintf(
'CommandFactory must be instance of Prooph\ServiceBus\Command\CommandFactoryInterface,'
. 'instance of type %s given',
((is_object($plugin)? get_class($plugin) : gettype($plugin)))
));
}
}
}

6 changes: 6 additions & 0 deletions src/Prooph/ServiceBus/Service/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Definition

const COMMAND_FACTORY = "command_factory";

const COMMAND_FACTORY_LOADER = "command_factory_loader";

const EVENT_BUS = "event_bus";

const DEFAULT_EVENT_BUS = "default_event_bus";
Expand All @@ -43,6 +45,8 @@ class Definition

const EVENT_FACTORY = "event_factory";

const EVENT_FACTORY_LOADER = "event_factory_loader";

const QUEUE = "queue";

const MESSAGE_DISPATCHER = "message_dispatcher";
Expand All @@ -51,6 +55,8 @@ class Definition

const MESSAGE_FACTORY = "message_factory";

const MESSAGE_FACTORY_LOADER = "message_factory_loader";

const COMMAND_BUS_LOADER = "command_bus_loader";

const COMMAND_RECEIVER_LOADER = "command_receiver_loader";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ protected function getServicesMap()
Definition::EVENT_RECEIVER_LOADER => 'Prooph\ServiceBus\Service\EventReceiverLoader',
Definition::INVOKE_STRATEGY_LOADER => 'Prooph\ServiceBus\Service\InvokeStrategyLoader',
Definition::MESSAGE_DISPATCHER_LOADER => 'Prooph\ServiceBus\Service\MessageDispatcherLoader',
Definition::QUEUE_LOADER => 'Prooph\ServiceBus\Service\QueueLoader'
Definition::QUEUE_LOADER => 'Prooph\ServiceBus\Service\QueueLoader',
Definition::COMMAND_FACTORY_LOADER => 'Prooph\ServiceBus\Service\CommandFactoryLoader',
);
}

Expand Down
10 changes: 0 additions & 10 deletions src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class ServiceBusConfiguration implements ConfigInterface
*/
protected $eventHandlers = array();

/**
* @var CommandFactoryInterface
*/
protected $commandFactory;


/**
* @param null|array $aConfiguration
*/
Expand Down Expand Up @@ -78,10 +72,6 @@ public function configureServiceManager(ServiceManager $serviceManager)

$serviceBusManagerServicesConfig->configureServiceManager($serviceManager);

if (!is_null($this->commandFactory)) {
$serviceManager->setService(Definition::COMMAND_FACTORY, $this->commandFactory);
}

if (isset($this->configuration[Definition::CONFIG_ROOT][Definition::DEFAULT_COMMAND_BUS])) {
$serviceManager->setDefaultCommandBus(
$this->configuration[Definition::CONFIG_ROOT][Definition::DEFAULT_COMMAND_BUS]
Expand Down
3 changes: 3 additions & 0 deletions tests/Prooph/ServiceBusTest/Command/CommandBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Prooph\ServiceBus\Message\InMemoryMessageDispatcher;
use Prooph\ServiceBus\Message\MessageFactory;
use Prooph\ServiceBus\Message\Queue;
use Prooph\ServiceBus\Service\CommandFactoryLoader;
use Prooph\ServiceBus\Service\CommandReceiverLoader;
use Prooph\ServiceBus\Service\ServiceBusManager;
use Prooph\ServiceBusTest\Mock\DoSomething;
Expand Down Expand Up @@ -61,6 +62,8 @@ protected function setUp()
$serviceBusManager
);

$commandReceiver->setCommandFactoryLoader(new CommandFactoryLoader());

$commandReceiverLoader = new CommandReceiverLoader();

$commandReceiverLoader->setService('test-case-bus', $commandReceiver);
Expand Down
3 changes: 3 additions & 0 deletions tests/Prooph/ServiceBusTest/Command/CommandReceiverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Prooph\ServiceBus\Command\CommandReceiver;
use Prooph\ServiceBus\Message\MessageHeader;
use Prooph\ServiceBus\Message\StandardMessage;
use Prooph\ServiceBus\Service\CommandFactoryLoader;
use Prooph\ServiceBus\Service\ServiceBusManager;
use Prooph\ServiceBusTest\Mock\DoSomething;
use Prooph\ServiceBusTest\TestCase;
Expand Down Expand Up @@ -67,6 +68,8 @@ protected function setUp()
),
$commandHandlerLocator
);

$this->commandReceiver->setCommandFactoryLoader(new CommandFactoryLoader());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ protected function setUp()
//Register InvokeStrategyLoader as Service
$this->serviceBusManager->setService(Definition::INVOKE_STRATEGY_LOADER, $invokeStrategyLoader);

//Register CommandFactory as Service, this is not necessary but we do it for testing purposes
$this->serviceBusManager->setService(Definition::COMMAND_FACTORY, new CommandFactory());

$this->commandReceiverLoader = new CommandReceiverLoader();

//Set MainServiceManager as ServiceLocator for the CommandReceiverLoader
Expand Down Expand Up @@ -120,11 +117,6 @@ public function it_creates_a_fully_configured_command_receiver()
$commandReceiver = $defaultCommandReceiverFactory
->createServiceWithName($this->commandReceiverLoader, 'testcasebus', 'test-case-bus');

$this->assertSame(
$this->serviceBusManager->get(Definition::COMMAND_FACTORY),
$commandReceiver->getCommandFactory()
);

$this->assertSame(
$this->serviceBusManager->get(Definition::INVOKE_STRATEGY_LOADER),
$commandReceiver->getInvokeStrategyLoader()
Expand Down

0 comments on commit c46aabb

Please sign in to comment.