Skip to content

Instantly share code, notes, and snippets.

@rommsen
Created May 25, 2016 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rommsen/3142028d65ab87985cabd3355f265cdb to your computer and use it in GitHub Desktop.
Save rommsen/3142028d65ab87985cabd3355f265cdb to your computer and use it in GitHub Desktop.
<?php
namespace Ipark\ApplicationBundle\Messaging\ServiceBus\Infrastructure;
use Ipark\FrameworkBundle\Common\SecurityHelper;
use Prooph\Common\Messaging\NoOpMessageConverter;
use Prooph\ServiceBus\CommandBus;
use Prooph\ServiceBus\Plugin\Auditing\CommandAuditor;
use Prooph\ServiceBus\Plugin\Auditing\RawMessageSerializer;
use Prooph\ServiceBus\Plugin\Auditing\SecretMessageSerializer;
use Prooph\ServiceBus\Plugin\Router\RegexRouter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CommandBusFactory
{
public function __invoke(ContainerInterface $container)
{
$commandBus = new CommandBus();
$commandBus->utilize($this->getRouter($container));
$commandBus->utilize($this->getCommandAuditor($container));
$commandBus->utilize($container->get('ipark_application.messaging.event_store.transaction_manager'));
return $commandBus;
}
/**
* @param ContainerInterface $container
* @return RegexRouter
*/
private function getRouter(ContainerInterface $container)
{
$handlerLocator = $container->get('ipark_application.messaging.service_bus.lazy_handler_locator');
return new RegexRouter([
RegexRouter::ALL => function ($command) use ($handlerLocator) {
$handler = $handlerLocator->locate($command);
$handler($command);
}
]);
}
private function getCommandAuditor(ContainerInterface $container)
{
return new CommandAuditor(
$container->get('monolog.logger.ipm'),
$this->getMessageSerializer(
$container->get('ipark_framework.common.security_helper'),
$container->get('request_stack')
)
);
}
private function getMessageSerializer(SecurityHelper $securityHelper, RequestStack $requestStack)
{
$user = $securityHelper->getUser();
$request = $requestStack->getMasterRequest();
$user_id = 'not-set';
$user_ip = 'not-set';
if ($user !== null) {
$user_id = $user->getId();
}
if ($request !== null) {
$user_ip = $request->getClientIp();
}
$serializer = new RawMessageSerializer(new NoOpMessageConverter(), [
'user_id' => $user_id,
'user_ip' => $user_ip,
]);
return new SecretMessageSerializer($serializer, ['password']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment