DoctrineEventSubscriber.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace WorkflowBundle\EventListener;
  3. use Doctrine\Common\EventSubscriber;
  4. use Doctrine\ORM\Event\LifecycleEventArgs;
  5. use WorkflowBundle\Utils\DoctrineEvents;
  6. use WorkflowBundle\Utils\WorkFlowEntityClasses;
  7. use OldSound\RabbitMqBundle\RabbitMq\Producer;
  8. class DoctrineEventSubscriber implements EventSubscriber
  9. {
  10. /**
  11. * @var Producer
  12. */
  13. private $producer;
  14. /**
  15. * @param Producer $producer
  16. */
  17. public function __construct(Producer $producer)
  18. {
  19. $this->producer = $producer;
  20. }
  21. /**
  22. * @return array
  23. */
  24. public function getSubscribedEvents()
  25. {
  26. return DoctrineEvents::getConstants();
  27. }
  28. /**
  29. * @param LifecycleEventArgs $args
  30. */
  31. public function prePersist(LifecycleEventArgs $args)
  32. {
  33. $this->execute($args, DoctrineEvents::PRE_PERSIST);
  34. }
  35. /**
  36. * @param LifecycleEventArgs $args
  37. */
  38. public function postPersist(LifecycleEventArgs $args)
  39. {
  40. $this->execute($args, DoctrineEvents::POST_PERSIST);
  41. }
  42. /**
  43. * @param LifecycleEventArgs $args
  44. */
  45. public function preUpdate(LifecycleEventArgs $args)
  46. {
  47. $this->execute($args, DoctrineEvents::PRE_UPDATE);
  48. }
  49. /**
  50. * @param LifecycleEventArgs $args
  51. */
  52. public function postUpdate(LifecycleEventArgs $args)
  53. {
  54. $this->execute($args, DoctrineEvents::POST_UPDATE);
  55. }
  56. /**
  57. * @param LifecycleEventArgs $args
  58. */
  59. public function preRemove(LifecycleEventArgs $args)
  60. {
  61. $this->execute($args, DoctrineEvents::PRE_REMOVE);
  62. }
  63. /**
  64. * @param LifecycleEventArgs $args
  65. */
  66. public function postRemove(LifecycleEventArgs $args)
  67. {
  68. $this->execute($args, DoctrineEvents::POST_REMOVE);
  69. }
  70. /**
  71. * @param LifecycleEventArgs $args
  72. * @param string $eventName
  73. */
  74. public function execute(LifecycleEventArgs $args, $eventName = DoctrineEvents::PRE_PERSIST)
  75. {
  76. $entity = $args->getEntity();
  77. $entityManager = $args->getEntityManager();
  78. $entityClass = get_class($entity);
  79. // la $entity esta dentro de las entidades con workflow
  80. if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
  81. $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
  82. $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
  83. foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
  84. $actions = $doctrine2WorkFlowAction->getActions();
  85. foreach ($actions as $action) {
  86. $msg = array(
  87. 'id' => uniqid(),
  88. 'cmd' => $action->getCmd($entity),
  89. );
  90. $this->producer->publish(serialize($msg));
  91. }
  92. }
  93. }
  94. }
  95. }