Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.