Skip to content

Commit

Permalink
Close #20: Add MessageFactoryLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
codeliner committed Jul 5, 2014
1 parent 82ce198 commit 948dffa
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 35 deletions.
23 changes: 10 additions & 13 deletions src/Prooph/ServiceBus/Command/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Prooph\ServiceBus\Message\QueueInterface;
use Prooph\ServiceBus\Message\StandardMessage;
use Prooph\ServiceBus\Service\Definition;
use Prooph\ServiceBus\Service\MessageFactoryLoader;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;

Expand Down Expand Up @@ -45,9 +46,9 @@ class CommandBus implements CommandBusInterface
protected $name;

/**
* @var MessageFactoryInterface
* @var MessageFactoryLoader
*/
protected $messageFactory;
protected $messageFactoryLoader;

/**
* @var EventManagerInterface
Expand Down Expand Up @@ -81,31 +82,27 @@ public function send($aCommand)
return;
}

$message = $this->getMessageFactory()->fromCommand($aCommand, $this->name);
$message = $this->getMessageFactoryLoader()->get(get_class($aCommand))->fromCommand($aCommand, $this->name);

$this->messageDispatcher->dispatch($this->queue, $message);

$this->events()->trigger(__FUNCTION__ . '.post', $this, array('command' => $aCommand, 'message' => $message));
}

/**
* @param MessageFactoryInterface $aMessageFactory
* @param MessageFactoryLoader $aMessageFactoryLoader
*/
public function setMessageFactory(MessageFactoryInterface $aMessageFactory)
public function setMessageFactoryLoader(MessageFactoryLoader $aMessageFactoryLoader)
{
$this->messageFactory = $aMessageFactory;
$this->messageFactoryLoader = $aMessageFactoryLoader;
}

/**
* @return MessageFactoryInterface
* @return MessageFactoryLoader
*/
public function getMessageFactory()
public function getMessageFactoryLoader()
{
if (is_null($this->messageFactory)) {
$this->messageFactory = new MessageFactory();
}

return $this->messageFactory;
return $this->messageFactoryLoader;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Prooph/ServiceBus/Command/DefaultCommandBusFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $

$commandBus = new CommandBus($requestedName, $messageDispatcher, $queue);

if ($mainServiceLocator->has(Definition::MESSAGE_FACTORY)) {
$commandBus->setMessageFactory($mainServiceLocator->get(Definition::MESSAGE_FACTORY));
}

$commandBus->setMessageFactoryLoader($mainServiceLocator->get(Definition::MESSAGE_FACTORY_LOADER));


return $commandBus;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Prooph/ServiceBus/Event/DefaultEventBusFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $

$eventBus = new EventBus($requestedName, $messageDispatcher, $queues);

if ($mainServiceLocator->has(Definition::MESSAGE_FACTORY)) {
$eventBus->setMessageFactory($mainServiceLocator->get(Definition::MESSAGE_FACTORY));
}
$eventBus->setMessageFactoryLoader($mainServiceLocator->get(Definition::MESSAGE_FACTORY_LOADER));


return $eventBus;
}
Expand Down
23 changes: 10 additions & 13 deletions src/Prooph/ServiceBus/Event/EventBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Prooph\ServiceBus\Message\MessageFactoryInterface;
use Prooph\ServiceBus\Message\QueueInterface;
use Prooph\ServiceBus\Service\Definition;
use Prooph\ServiceBus\Service\MessageFactoryLoader;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;

Expand Down Expand Up @@ -43,9 +44,9 @@ class EventBus implements EventBusInterface
protected $name;

/**
* @var MessageFactoryInterface
* @var MessageFactoryLoader
*/
protected $messageFactory;
protected $messageFactoryLoader;

/**
* @var EventManagerInterface
Expand Down Expand Up @@ -80,7 +81,7 @@ public function publish($anEvent)
return;
}

$message = $this->getMessageFactory()->fromEvent($anEvent, $this->name);
$message = $this->getMessageFactoryLoader()->get(get_class($anEvent))->fromEvent($anEvent, $this->name);

foreach ($this->queueCollection as $queue) {

Expand All @@ -101,23 +102,19 @@ public function publish($anEvent)
}

/**
* @param MessageFactoryInterface $aMessageFactory
* @param MessageFactoryLoader $aMessageFactory
*/
public function setMessageFactory(MessageFactoryInterface $aMessageFactory)
public function setMessageFactoryLoader(MessageFactoryLoader $aMessageFactory)
{
$this->messageFactory = $aMessageFactory;
$this->messageFactoryLoader = $aMessageFactory;
}

/**
* @return MessageFactoryInterface
* @return MessageFactoryLoader
*/
public function getMessageFactory()
public function getMessageFactoryLoader()
{
if (is_null($this->messageFactory)) {
$this->messageFactory = new MessageFactory();
}

return $this->messageFactory;
return $this->messageFactoryLoader;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions src/Prooph/ServiceBus/Message/AbstractMessageFactoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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 - 23:11
*/

namespace Prooph\ServiceBus\Message;

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

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

/**
* 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)
{
return $serviceLocator instanceof MessageFactoryLoader;
}

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

return $this->messageFactory;
}
}

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

const IN_MEMORY_MESSAGE_DISPATCHER = "in_memory_message_dispatcher";

const MESSAGE_FACTORY = "message_factory";

const MESSAGE_FACTORY_LOADER = "message_factory_loader";

const COMMAND_BUS_LOADER = "command_bus_loader";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ protected function getServicesMap()
Definition::MESSAGE_DISPATCHER_LOADER => 'Prooph\ServiceBus\Service\MessageDispatcherLoader',
Definition::QUEUE_LOADER => 'Prooph\ServiceBus\Service\QueueLoader',
Definition::COMMAND_FACTORY_LOADER => 'Prooph\ServiceBus\Service\CommandFactoryLoader',
Definition::EVENT_FACTORY_LOADER => 'Prooph\ServiceBus\Service\EventFactoryLoader'
Definition::EVENT_FACTORY_LOADER => 'Prooph\ServiceBus\Service\EventFactoryLoader',
Definition::MESSAGE_FACTORY_LOADER => 'Prooph\ServiceBus\Service\MessageFactoryLoader',
);
}

Expand Down
60 changes: 60 additions & 0 deletions src/Prooph/ServiceBus/Service/MessageFactoryLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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 - 23:10
*/

namespace Prooph\ServiceBus\Service;

use Prooph\ServiceBus\Message\AbstractMessageFactoryFactory;
use Prooph\ServiceBus\Message\MessageFactoryInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\Exception;

/**
* Class MessageFactoryLoader
*
* @package Prooph\ServiceBus\Service
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class MessageFactoryLoader extends AbstractPluginManager
{

/**
* @param ConfigInterface $aConfig
*/
public function __construct(ConfigInterface $aConfig = null)
{
parent::__construct($aConfig);

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

/**
* 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 MessageFactoryInterface) {
throw new Exception\RuntimeException(sprintf(
'MessageFactory must be instance of Prooph\ServiceBus\Message\MessageFactoryInterface,'
. 'instance of type %s given',
((is_object($plugin)? get_class($plugin) : gettype($plugin)))
));
}
}
}

3 changes: 3 additions & 0 deletions tests/Prooph/ServiceBusTest/Command/CommandBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Prooph\ServiceBus\Message\Queue;
use Prooph\ServiceBus\Service\CommandFactoryLoader;
use Prooph\ServiceBus\Service\CommandReceiverLoader;
use Prooph\ServiceBus\Service\MessageFactoryLoader;
use Prooph\ServiceBus\Service\ServiceBusManager;
use Prooph\ServiceBusTest\Mock\DoSomething;
use Prooph\ServiceBusTest\Mock\HandleCommandHandler;
Expand Down Expand Up @@ -71,6 +72,8 @@ protected function setUp()
$messageDispatcher->registerCommandReceiverLoaderForQueue($queue, $commandReceiverLoader);

$this->commandBus = new CommandBus('test-case-bus', $messageDispatcher, $queue);

$this->commandBus->setMessageFactoryLoader(new MessageFactoryLoader());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Prooph\ServiceBus\Message\Queue;
use Prooph\ServiceBus\Service\Definition;
use Prooph\ServiceBus\Service\EventBusLoader;
use Prooph\ServiceBus\Service\MessageFactoryLoader;
use Prooph\ServiceBus\Service\ServiceBusManager;
use Prooph\ServiceBusTest\Mock\OnEventHandler;
use Prooph\ServiceBusTest\Mock\SomethingDone;
Expand Down Expand Up @@ -94,6 +95,8 @@ public function it_creates_a_fully_configured_event_bus()

$eventBus = $factory->createServiceWithName($eventBusLoader, 'testcasebus', 'test-case-bus');

$eventBus->setMessageFactoryLoader(new MessageFactoryLoader());

$somethingDone = SomethingDone::fromData('test payload');

$eventBus->publish($somethingDone);
Expand Down
3 changes: 3 additions & 0 deletions tests/Prooph/ServiceBusTest/Event/EventBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Prooph\ServiceBus\Service\EventBusLoader;
use Prooph\ServiceBus\Service\EventFactoryLoader;
use Prooph\ServiceBus\Service\EventReceiverLoader;
use Prooph\ServiceBus\Service\MessageFactoryLoader;
use Prooph\ServiceBus\Service\ServiceBusManager;
use Prooph\ServiceBusTest\Mock\OnEventHandler;
use Prooph\ServiceBusTest\Mock\SomethingDone;
Expand Down Expand Up @@ -74,6 +75,8 @@ protected function setUp()
$messageDispatcher->registerEventReceiverLoaderForQueue($queue2, $eventReceiverLoader);

$this->eventBus = new EventBus('test-case-bus', $messageDispatcher, array($queue, $queue2));

$this->eventBus->setMessageFactoryLoader(new MessageFactoryLoader());
}

/**
Expand Down

0 comments on commit 948dffa

Please sign in to comment.