123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace WorkflowBundle\EventListener;
- use Doctrine\Common\EventSubscriber;
- use Doctrine\ORM\Event\LifecycleEventArgs;
- use WorkflowBundle\Utils\DoctrineEvents;
- use WorkflowBundle\Utils\WorkFlowEntityClasses;
- use OldSound\RabbitMqBundle\RabbitMq\Producer;
- class DoctrineEventSubscriber implements EventSubscriber
- {
-
- /**
- * @var Producer
- */
- private $producer;
-
-
- /**
- * @param Producer $producer
- */
- public function __construct(Producer $producer)
- {
- $this->producer = $producer;
- }
- /**
- * @return array
- */
- public function getSubscribedEvents()
- {
- return DoctrineEvents::getConstants();
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function prePersist(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::PRE_PERSIST);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postPersist(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::POST_PERSIST);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function preUpdate(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::PRE_UPDATE);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postUpdate(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::POST_UPDATE);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function preRemove(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::PRE_REMOVE);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postRemove(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::POST_REMOVE);
- }
- /**
- * @param LifecycleEventArgs $args
- * @param string $eventName
- */
- public function execute(LifecycleEventArgs $args, $eventName = DoctrineEvents::PRE_PERSIST)
- {
- $entity = $args->getEntity();
- $entityManager = $args->getEntityManager();
- $entityClass = get_class($entity);
-
- // la $entity esta dentro de las entidades con workflow
- if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
- $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
- $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
- foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
- $actions = $doctrine2WorkFlowAction->getActions();
- foreach ($actions as $action) {
- $msg = array(
- 'id' => uniqid(),
- 'cmd' => $action->getCmd($entity),
- );
- $this->producer->publish(serialize($msg));
- }
- }
- }
- }
- }
|