ProducerService.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. $doctrine2WorkFlowActionRepository = $this->getEntityManager()->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
  44. $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
  45. foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
  46. $actions = $doctrine2WorkFlowAction->getActions();
  47. foreach ($actions as $action) {
  48. $this->publishMessage($action, $entity);
  49. }
  50. }
  51. }
  52. }
  53. /**
  54. * @param array $actionName
  55. * @param array $entityClass
  56. * @param array $entityId
  57. */
  58. public function executeAction($actionName, $entityClass, $entityId)
  59. {
  60. $entityManager = $this->getEntityManager();
  61. $actionRepository = $entityManager->getRepository('WorkflowBundle:Action');
  62. $action = $actionRepository->findOneBy(array(
  63. 'name' => $actionName,
  64. 'objectClass' => $entityClass,
  65. ));
  66. $entityRepository = $entityManager->getRepository($entityClass);
  67. $entity = $entityRepository->find($entityId);
  68. if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
  69. $this->publishMessage($action, $entity);
  70. }
  71. }
  72. /**
  73. * @param Action $action
  74. * @param Entity $entity
  75. */
  76. public function publishMessage(Action $action, $entity)
  77. {
  78. $msg = array(
  79. 'id' => uniqid(),
  80. 'content' => $action->render($entity),
  81. );
  82. $this->producer->publish(serialize($msg));
  83. }
  84. }