ProducerService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace WorkflowBundle\Services;
  3. use OldSound\RabbitMqBundle\RabbitMq\Producer;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use WorkflowBundle\Entity\Action;
  6. use WorkflowBundle\Utils\DoctrineEvents;
  7. use WorkflowBundle\Utils\WorkFlowEntityClasses;
  8. class ProducerService
  9. {
  10. /**
  11. * @var Producer
  12. */
  13. private $producer;
  14. /**
  15. * @var ContainerInterface
  16. */
  17. private $serviceContainer;
  18. /**
  19. * @param ContainerInterface $serviceContainer
  20. */
  21. public function __construct(ContainerInterface $serviceContainer)
  22. {
  23. $this->serviceContainer = $serviceContainer;
  24. /* @var $this->producer Producer */
  25. $this->producer = $serviceContainer->get('old_sound_rabbit_mq.flowdat_tasklogger_producer');
  26. }
  27. /**
  28. * @return EntityManagerInterface
  29. */
  30. public function getEntityManager()
  31. {
  32. return $this->serviceContainer->get('doctrine.orm.entity_manager');
  33. }
  34. /**
  35. * @param Entity $entity
  36. * @param string $eventName
  37. */
  38. public function execute($entity, $eventName = DoctrineEvents::PRE_PERSIST)
  39. {
  40. $entityClass = get_class($entity);
  41. // la $entity esta dentro de las entidades con workflow
  42. if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
  43. $actionRepository = $this->getEntityManager()->getRepository('WorkflowBundle:Action');
  44. $actions = $actionRepository->findAllByEventAndObjectClass($eventName, $entityClass);
  45. foreach ($actions as $action) {
  46. $routing_key = "";
  47. if(getenv("AMQP_KEY") !== false){
  48. $routing_key = getenv("AMQP_KEY");
  49. }
  50. $this->publishMessage($action, $entity, $routing_key);
  51. }
  52. }
  53. }
  54. /**
  55. * @param array $actionName
  56. * @param array $entityClass
  57. * @param array $entityId
  58. */
  59. public function executeAction($actionName, $entityClass, $entityId)
  60. {
  61. $entityManager = $this->getEntityManager();
  62. $actionRepository = $entityManager->getRepository('WorkflowBundle:Action');
  63. $action = $actionRepository->findOneBy(array(
  64. 'name' => $actionName,
  65. 'objectClass' => $entityClass,
  66. ));
  67. $entityRepository = $entityManager->getRepository($entityClass);
  68. $entity = $entityRepository->find($entityId);
  69. if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
  70. $routing_key = "";
  71. if(getenv("AMQP_KEY") !== false){
  72. $routing_key = getenv("AMQP_KEY");
  73. }
  74. $this->publishMessage($action, $entity);
  75. }
  76. }
  77. /**
  78. * @param Action $action
  79. * @param Entity $entity
  80. */
  81. public function publishMessage(Action $action, $entity, $routing_key = "")
  82. {
  83. $entityClass = get_class($entity);
  84. $actionName = $action->getName();
  85. $entityId = $entity->getId();
  86. $msg = array(
  87. 'id' => uniqid(),
  88. 'content' => $action->render($entity),
  89. 'entityClass' => $entityClass,
  90. 'entityId' => $entityId,
  91. );
  92. $this->producer->publish(serialize($msg), $routing_key);
  93. $msg = serialize($msg);
  94. $logger = $this->serviceContainer->get('logger');
  95. $logger->info("PUBLISH-RABBIT {$actionName} | {$entityClass}_id_{$entityId} | msg: {$msg}");
  96. }
  97. }