ProducerService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. $this->publishMessage($action, $entity);
  47. }
  48. }
  49. }
  50. /**
  51. * @param array $actionName
  52. * @param array $entityClass
  53. * @param array $entityId
  54. */
  55. public function executeAction($actionName, $entityClass, $entityId)
  56. {
  57. $entityManager = $this->getEntityManager();
  58. $actionRepository = $entityManager->getRepository('WorkflowBundle:Action');
  59. $action = $actionRepository->findOneBy(array(
  60. 'name' => $actionName,
  61. 'objectClass' => $entityClass,
  62. ));
  63. $entityRepository = $entityManager->getRepository($entityClass);
  64. $entity = $entityRepository->find($entityId);
  65. if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
  66. $this->publishMessage($action, $entity);
  67. }
  68. }
  69. /**
  70. * @param Action $action
  71. * @param Entity $entity
  72. */
  73. public function publishMessage(Action $action, $entity, $routing_key = false)
  74. {
  75. if($routing_key === false){
  76. $routing_key = "";
  77. if(getenv("AMQP_KEY") !== false){
  78. $routing_key = getenv("AMQP_KEY");
  79. }
  80. }
  81. $entityClass = get_class($entity);
  82. $actionName = $action->getName();
  83. $entityId = $entity->getId();
  84. $msg = array(
  85. 'id' => uniqid(),
  86. 'content' => $action->render($entity),
  87. 'entityClass' => $entityClass,
  88. 'entityId' => $entityId,
  89. );
  90. $this->producer->publish(serialize($msg), $routing_key);
  91. $msg = serialize($msg);
  92. $logger = $this->serviceContainer->get('logger');
  93. $logger->info("PUBLISH-RABBIT {$actionName} | {$entityClass}_id_{$entityId} | msg: {$msg}");
  94. }
  95. }