Skip to content

Instantly share code, notes, and snippets.

@alle
Forked from oqq/ProcessManagerPlugin.php
Created March 17, 2018 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alle/8740cba354ca68f28bf3ddc5f6b55b07 to your computer and use it in GitHub Desktop.
Save alle/8740cba354ca68f28bf3ddc5f6b55b07 to your computer and use it in GitHub Desktop.
prooph event bus process manager plugin
<?php
declare(strict_types=1);
namespace Prooph\ServiceBus\Plugin;
use Prooph\Common\Event\ActionEvent;
use Prooph\ServiceBus\CommandBus;
use Prooph\ServiceBus\EventBus;
use Prooph\ServiceBus\MessageBus;
final class ProcessManagerPlugin extends AbstractPlugin
{
private $commandBus;
public function __construct(CommandBus $commandBus)
{
$this->commandBus = $commandBus;
}
public function attachToMessageBus(MessageBus $messageBus): void
{
$this->listenerHandlers[] = $messageBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent): void {
if ($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, false)) {
return;
}
$message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE);
$handlers = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, []);
foreach ($handlers as $handler) {
$commands = $handler->handle($message);
foreach ($commands as $command) {
$this->commandBus->dispatch($command);
}
}
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, true);
},
MessageBus::PRIORITY_INVOKE_HANDLER
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment