EventSubscriber.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace WorkflowBundle\Event;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Workflow\Event\Event;
  6. use Symfony\Component\Workflow\Event\GuardEvent;
  7. use FTTHBundle\Entity\ONULogOLT;
  8. use WorkflowBundle\Services\ProducerService;
  9. # Ayuda:
  10. # http://blog.eleven-labs.com/en/symfony-workflow-component/
  11. # https://github.com/lexik/LexikWorkflowBundle
  12. # https://github.com/fduch/workflow-bundle
  13. class EventSubscriber implements EventSubscriberInterface
  14. {
  15. /**
  16. * @var ContainerInterface
  17. */
  18. private $container;
  19. /**
  20. * @var ProducerService
  21. */
  22. private $producerService;
  23. /**
  24. * @param ContainerInterface $container
  25. * @param ProducerService $producerService
  26. */
  27. public function __construct(ContainerInterface $container, ProducerService $producerService)
  28. {
  29. $this->container = $container;
  30. $this->producerService = $producerService;
  31. }
  32. /**
  33. * @return array
  34. */
  35. public static function getSubscribedEvents()
  36. {
  37. return array(
  38. 'workflow.leave' => array('leave'),
  39. 'workflow.transition' => array('transition'),
  40. 'workflow.enter' => array('enter'),
  41. 'workflow.guard' => array('guard'),
  42. );
  43. }
  44. /**
  45. * @param Event $event
  46. */
  47. public function leave(Event $event)
  48. {
  49. foreach ($event->getTransition()->getFroms() as $place) {
  50. $actions = $this->getActions($event, 'leave', $place);
  51. foreach ($actions as $k => $action) {
  52. $params = array(
  53. 'entity' => $event->getSubject(),
  54. );
  55. $this->completeAction($action, $params);
  56. }
  57. }
  58. }
  59. /**
  60. * @param Event $event
  61. */
  62. public function transition(Event $event)
  63. {
  64. $transitionName = $event->getTransition()->getName();
  65. $actions = $this->getActions($event, 'transition', $transitionName);
  66. foreach ($actions as $k => $action) {
  67. $params = array(
  68. 'entity' => $event->getSubject(),
  69. );
  70. $this->completeAction($action, $params);
  71. }
  72. }
  73. /**
  74. * @param Event $event
  75. */
  76. public function enter(Event $event)
  77. {
  78. foreach ($event->getTransition()->getTos() as $place) {
  79. $actions = $this->getActions($event, 'enter', $place);
  80. foreach ($actions as $k => $action) {
  81. $params = array(
  82. 'entity' => $event->getSubject(),
  83. );
  84. $this->completeAction($action, $params);
  85. }
  86. }
  87. }
  88. /**
  89. * @param Event $event
  90. */
  91. public function guard(GuardEvent $event)
  92. {
  93. //$event->setBlocked(true);
  94. }
  95. /**
  96. * @param Event $event
  97. * @param string $eventName
  98. * @param string $eventReference
  99. *
  100. * @return array
  101. */
  102. public function getActions($event, $eventName, $eventReference)
  103. {
  104. $logger = $this->container->get('logger');
  105. $em = $this->container->get("doctrine.orm.entity_manager");
  106. $object = $event->getSubject();
  107. $objectClass = (string)get_class($object);
  108. if ($objectClass == 'FTTHBundle\\Entity\\ONU') {
  109. if (is_null($object->getLogOLT())) {
  110. $onuLogOlt = new ONULogOLT();
  111. $em->persist($onuLogOlt);
  112. $object->setLogOLT($onuLogOlt);
  113. }
  114. $object->getLogOLT()->setPending();
  115. $em->flush($object->getLogOLT());
  116. }
  117. $logger->info("EVENT {$eventName}:{$eventReference} => {$objectClass }_id_{$object->getId()}");
  118. $workflowName = $event->getWorkflowName();
  119. $actions = $em->getRepository("WorkflowBundle:Action")
  120. ->findByWorkflowAndEventRef($workflowName, $eventReference);
  121. $new_actions = array();
  122. foreach ($actions as $action) {
  123. if (in_array($eventName, $action->getEvent()) AND is_a($object, $action->getObjectClass())) {
  124. $new_actions[] = $action;
  125. }
  126. }
  127. $actions = $new_actions;
  128. $logger->info("EVENT Found " . count($actions) . " actions to apply", compact('workflowName', 'eventReference'));
  129. return $actions;
  130. }
  131. /**
  132. * @param Action $action
  133. * @param array $params
  134. */
  135. public function completeAction($action, $params)
  136. {
  137. $logger = $this->container->get('logger');
  138. $object = $params['entity'];
  139. $routing_key = "";
  140. if (getenv("AMQP_KEY") !== false) {
  141. $routing_key = getenv("AMQP_KEY");
  142. }
  143. $this->producerService->publishMessage($action, $object, $routing_key);
  144. $template = $action->getTemplate();
  145. $twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
  146. $tpl = $twig->createTemplate($template);
  147. $rendered = $tpl->render($params);
  148. $class = get_class($object);
  149. $logger->info("ACTION (action_id_{$action->getId()}) {$action->getName()} | Task: '{$rendered}' => {$class}_id_{$object->getId()}");
  150. // $string = file_get_contents("/var/flowdat/ftth/out.log");
  151. // $string .= "### Action id :".$action->getId().PHP_EOL;
  152. // $string .= "----------------------".PHP_EOL.$template.PHP_EOL;
  153. // $string .= "----------------------".PHP_EOL.$rendered.PHP_EOL.PHP_EOL;
  154. // file_put_contents("/var/flowdat/ftth/out.log", $string);
  155. }
  156. }