DoctrineEventSubscriber.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Services\ProducerService;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. class DoctrineEventSubscriber implements EventSubscriber
  9. {
  10. /**
  11. * @var ProducerService
  12. */
  13. private $producerService;
  14. /**
  15. * @param ContainerInterface $serviceContainer
  16. */
  17. public function __construct(ProducerService $producerService)
  18. {
  19. /* @var $this->producerService ProducerService */
  20. $this->producerService = $producerService;
  21. }
  22. /**
  23. * @return array
  24. */
  25. public function getSubscribedEvents()
  26. {
  27. return array(
  28. DoctrineEvents::PRE_PERSIST,
  29. DoctrineEvents::POST_PERSIST,
  30. DoctrineEvents::PRE_UPDATE,
  31. DoctrineEvents::POST_UPDATE,
  32. DoctrineEvents::PRE_REMOVE,
  33. DoctrineEvents::POST_REMOVE,
  34. );
  35. }
  36. /**
  37. * @param LifecycleEventArgs $args
  38. */
  39. public function prePersist(LifecycleEventArgs $args)
  40. {
  41. $this->execute($args, DoctrineEvents::PRE_PERSIST);
  42. }
  43. /**
  44. * @param LifecycleEventArgs $args
  45. */
  46. public function postPersist(LifecycleEventArgs $args)
  47. {
  48. $this->execute($args, DoctrineEvents::POST_PERSIST);
  49. }
  50. /**
  51. * @param LifecycleEventArgs $args
  52. */
  53. public function preUpdate(LifecycleEventArgs $args)
  54. {
  55. $this->execute($args, DoctrineEvents::PRE_UPDATE);
  56. }
  57. /**
  58. * @param LifecycleEventArgs $args
  59. */
  60. public function postUpdate(LifecycleEventArgs $args)
  61. {
  62. $this->execute($args, DoctrineEvents::POST_UPDATE);
  63. }
  64. /**
  65. * @param LifecycleEventArgs $args
  66. */
  67. public function preRemove(LifecycleEventArgs $args)
  68. {
  69. $this->execute($args, DoctrineEvents::PRE_REMOVE);
  70. }
  71. /**
  72. * @param LifecycleEventArgs $args
  73. */
  74. public function postRemove(LifecycleEventArgs $args)
  75. {
  76. $this->execute($args, DoctrineEvents::POST_REMOVE);
  77. }
  78. /**
  79. * @param LifecycleEventArgs $args
  80. * @param string $eventName
  81. */
  82. public function execute(LifecycleEventArgs $args, $eventName = DoctrineEvents::PRE_PERSIST)
  83. {
  84. $this->producerService->execute($args->getEntity(), $eventName);
  85. }
  86. }