Skip to content

Commit

Permalink
Add EventFactoryLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
codeliner committed Jul 5, 2014
1 parent c46aabb commit 82ce198
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 23 deletions.
58 changes: 58 additions & 0 deletions src/Prooph/ServiceBus/Event/AbstractEventFactoryFactory.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:00
*/

namespace Prooph\ServiceBus\Event;

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

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

/**
* 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 EventFactoryLoader;
}

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

return $this->eventFactory;
}
}

4 changes: 1 addition & 3 deletions src/Prooph/ServiceBus/Event/DefaultEventReceiverFactory.php
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::EVENT_FACTORY)) {
$eventReceiver->setEventFactory($mainServiceLocator->get(Definition::EVENT_FACTORY));
}
$eventReceiver->setEventFactoryLoader($mainServiceLocator->get(Definition::EVENT_FACTORY_LOADER));

return $eventReceiver;
}
Expand Down
23 changes: 10 additions & 13 deletions src/Prooph/ServiceBus/Event/EventReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Prooph\ServiceBus\Exception\RuntimeException;
use Prooph\ServiceBus\Message\MessageInterface;
use Prooph\ServiceBus\Service\Definition;
use Prooph\ServiceBus\Service\EventFactoryLoader;
use Prooph\ServiceBus\Service\InvokeStrategyLoader;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
Expand All @@ -33,9 +34,9 @@ class EventReceiver implements EventReceiverInterface
protected $eventMap = array();

/**
* @var EventFactoryInterface
* @var EventFactoryLoader
*/
protected $eventFactory;
protected $eventFactoryLoader;

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

$event = $this->getEventFactory()->fromMessage($aMessage);
$event = $this->getEventFactoryLoader()->get($aMessage->name())->fromMessage($aMessage);

$eventHandlerAliases = (is_string($this->eventMap[$aMessage->name()]))?
array($this->eventMap[$aMessage->name()])
Expand Down Expand Up @@ -131,23 +132,19 @@ public function handle(MessageInterface $aMessage)
}

/**
* @param EventFactoryInterface $anEventFactory
* @param EventFactoryLoader $anEventFactory
*/
public function setEventFactory(EventFactoryInterface $anEventFactory)
public function setEventFactoryLoader(EventFactoryLoader $anEventFactory)
{
$this->eventFactory = $anEventFactory;
$this->eventFactoryLoader = $anEventFactory;
}

/**
* @return EventFactoryInterface
* @return EventFactoryLoader
*/
public function getEventFactory()
public function getEventFactoryLoader()
{
if (is_null($this->eventFactory)) {
$this->eventFactory = new EventFactory();
}

return $this->eventFactory;
return $this->eventFactoryLoader;
}

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

const COMMAND_HANDLER_INVOKE_STRATEGIES = 'command_handler_invoke_strategies';

const COMMAND_FACTORY = "command_factory";

const COMMAND_FACTORY_LOADER = "command_factory_loader";

const EVENT_BUS = "event_bus";
Expand Down
53 changes: 53 additions & 0 deletions src/Prooph/ServiceBus/Service/EventFactoryLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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:58
*/

namespace Prooph\ServiceBus\Service;

use Prooph\ServiceBus\Event\AbstractEventFactoryFactory;
use Prooph\ServiceBus\Event\EventFactoryInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\Exception;

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ 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'
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ public function it_creates_a_fully_configured_event_receiver()
$eventReceiver = $defaultEventReceiverFactory
->createServiceWithName($this->eventReceiverLoader, 'testcasebus', 'test-case-bus');

$this->assertSame(
$this->serviceBusManager->get(Definition::EVENT_FACTORY),
$eventReceiver->getEventFactory()
);

$this->assertSame(
$this->serviceBusManager->get(Definition::INVOKE_STRATEGY_LOADER),
$eventReceiver->getInvokeStrategyLoader()
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 @@ -17,6 +17,7 @@
use Prooph\ServiceBus\Message\MessageFactory;
use Prooph\ServiceBus\Message\Queue;
use Prooph\ServiceBus\Service\EventBusLoader;
use Prooph\ServiceBus\Service\EventFactoryLoader;
use Prooph\ServiceBus\Service\EventReceiverLoader;
use Prooph\ServiceBus\Service\ServiceBusManager;
use Prooph\ServiceBusTest\Mock\OnEventHandler;
Expand Down Expand Up @@ -63,6 +64,8 @@ protected function setUp()
$serviceBusManager
);

$eventReceiver->setEventFactoryLoader(new EventFactoryLoader());

$eventReceiverLoader = new EventReceiverLoader();

$eventReceiverLoader->setService('test-case-bus', $eventReceiver);
Expand Down
3 changes: 3 additions & 0 deletions tests/Prooph/ServiceBusTest/Event/EventReceiverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Prooph\ServiceBus\Event\EventReceiver;
use Prooph\ServiceBus\Message\MessageHeader;
use Prooph\ServiceBus\Message\StandardMessage;
use Prooph\ServiceBus\Service\EventFactoryLoader;
use Prooph\ServiceBus\Service\ServiceBusManager;
use Prooph\ServiceBusTest\Mock\SomethingDone;
use Prooph\ServiceBusTest\TestCase;
Expand Down Expand Up @@ -72,6 +73,8 @@ protected function setUp()
),
$eventHandlerLocator
);

$this->eventReceiver->setEventFactoryLoader(new EventFactoryLoader());
}

/**
Expand Down

0 comments on commit 82ce198

Please sign in to comment.