Explorar el Código

Refactory EventSubscriber. Traducciones. Formateo de algunos métodos. php annotations

Espinoza Guillermo hace 6 años
padre
commit
72d6e1f32b

+ 58 - 37
Event/EventSubscriber.php

@@ -16,12 +16,20 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 
 class EventSubscriber implements EventSubscriberInterface
 {
-
+    
+    /**
+     * @var ContainerInterface
+     */
     private $container;
+    
+    /**
+     * @var ProducerService
+     */
     private $producerService;
 
     /**
      * @param ContainerInterface $container
+     * @param ProducerService $producerService
      */
     public function __construct(ContainerInterface $container, ProducerService $producerService)
     {
@@ -29,57 +37,69 @@ class EventSubscriber implements EventSubscriberInterface
         $this->producerService = $producerService;
     }
 
+    /**
+     * @return array
+     */
     public static function getSubscribedEvents()
     {
         return array(
-                'workflow.leave' => array('leave'),
-                'workflow.transition' => array('transition'),
-                'workflow.enter' => array('enter'),
-                'workflow.guard' => array('guard')
+            'workflow.leave' => array('leave'),
+            'workflow.transition' => array('transition'),
+            'workflow.enter' => array('enter'),
+            'workflow.guard' => array('guard'),
         );
     }
 
+    /**
+     * @param Event $event
+     */
     public function leave(Event $event)
     {
         foreach ($event->getTransition()->getFroms() as $place) {
             $actions = $this->getActions($event, 'leave', $place);
-
-            foreach($actions as $k => $action) {
-                $params = array('entity'=>$event->getSubject());
+            foreach ($actions as $k => $action) {
+                $params = array(
+                    'entity' => $event->getSubject(),
+                );
                 $this->completeAction($action, $params);
-
             }
-            
         }
-
     }
 
+    /**
+     * @param Event $event
+     */
     public function transition(Event $event)
     {
         $transitionName = $event->getTransition()->getName();
-
         $actions = $this->getActions($event, 'transition', $transitionName);
-
-        foreach($actions as $k => $action) {
-            $params = array('entity'=>$event->getSubject());
+        foreach ($actions as $k => $action) {
+            $params = array(
+                'entity' => $event->getSubject(),
+            );
             $this->completeAction($action, $params);
-
         }
-
     }
-
+    
+    /**
+     * @param Event $event
+     */
     public function enter(Event $event)
     {
         foreach ($event->getTransition()->getTos() as $place) {
             $actions = $this->getActions($event, 'enter', $place);
-
-            foreach($actions as $k => $action) {
-                $params = array('entity'=>$event->getSubject());
+            foreach ($actions as $k => $action) {
+                $params = array(
+                    'entity' => $event->getSubject(),
+                );
                 $this->completeAction($action, $params);
             }
         }
     }
     
+    /**
+     * @param Event $event
+     */
     public function guard(GuardEvent $event)
     {
        //$event->setBlocked(true);
@@ -100,32 +120,33 @@ class EventSubscriber implements EventSubscriberInterface
         $object = $event->getSubject();
         $objectClass = (string) get_class($object);
         $logger->info("EVENT {$eventName}:{$eventReference} => {$objectClass }_id_{$object->getId()}");
-
-       $criteria = array("workflowName" => $event->getWorkflowName(), "eventReference" => $eventReference
-       );
-        $actions = $em->getRepository("WorkflowBundle:Action")->findBy($criteria);
-
-       $new_actions = array();
-       foreach($actions as $action){
-
-               if(in_array($eventName, $action->getEvent()) AND is_a($object, $action->getObjectClass())){
-                       $new_actions[] = $action;
-               }       
-       }
-       $actions = $new_actions;
+        
+        $workflowName = $object->getWorkflowName();
+        $actions = $em->getRepository("WorkflowBundle:Action")
+                    ->findByWorkflowAndEventRef($workflowName, $eventReference);
+        
+        $new_actions = array();
+        foreach ($actions as $action) {
+            if (in_array($eventName, $action->getEvent()) AND is_a($object, $action->getObjectClass())) {
+                $new_actions[] = $action;
+            }
+        }
+        $actions = $new_actions;
         $logger->info("EVENT Found ". count($actions) . " actions to apply", $criteria);
 
         return $actions;
     }
 
+    /**
+     * @param Action $action
+     * @param array $params
+     */
     public function completeAction($action, $params)
     {
-
         $logger = $this->container->get('logger');
-
         $object = $params['entity'];
         $routing_key = "";
-        if(getenv("AMQP_KEY") !== false){
+        if (getenv("AMQP_KEY") !== false) {
              $routing_key = getenv("AMQP_KEY");
         }
 

+ 30 - 6
Repository/ActionRepository.php

@@ -16,8 +16,10 @@ class ActionRepository extends EntityRepository
     public function findAllByEventAndObjectClass($eventName, $objectClass)
     {
         $qb = $this->createQueryBuilder('Action')
-                   ->andWhere('Action.event LIKE :eventName')->setParameter('eventName', "%{$eventName}%")
-                   ->andWhere('Action.objectClass = :objectClass')->setParameter('objectClass', $objectClass)
+                   ->andWhere('Action.event LIKE :eventName')
+                   ->setParameter('eventName', "%{$eventName}%")
+                   ->andWhere('Action.objectClass LIKE :objectClass')
+                   ->setParameter('objectClass', str_replace('\\', '\\\\', $objectClass))
                 ;
         
         return $qb->getQuery()->getResult();
@@ -34,15 +36,37 @@ class ActionRepository extends EntityRepository
     public function findAllByEventAndWorkflowName($eventName, $workflowName, $eventReference, $objectClass = null)
     {
         $qb = $this->createQueryBuilder('Action')
-                   ->andWhere('Action.event LIKE :eventName')->setParameter('eventName', "%{$eventName}%")
-                   ->andWhere('Action.workflowName = :workflowName')->setParameter('workflowName', $workflowName)
-                   ->andWhere('Action.eventReference = :eventReference')->setParameter('eventReference', $eventReference)
+                   ->andWhere('Action.event LIKE :eventName')
+                   ->setParameter('eventName', "%{$eventName}%")
+                   ->andWhere('Action.workflowName = :workflowName')
+                   ->setParameter('workflowName', $workflowName)
+                   ->andWhere('Action.eventReference = :eventReference')
+                   ->setParameter('eventReference', $eventReference)
                 ;
         if ($objectClass) {
-            $qb->andWhere('Action.objectClass = :objectClass')->setParameter('objectClass', $objectClass);
+            $qb->andWhere('Action.objectClass = :objectClass')
+                ->setParameter('objectClass', $objectClass);
         }
                    
         return $qb->getQuery()->getResult();
     }
+    
+    /**
+     * @param string $workflowName Nombre del workflow
+     * @param string $event Nombre del event
+     *
+     * @return array
+     */
+    public function findByWorkflowAndEventRef($workflowName, $event)
+    {
+        $qb = $this->createQueryBuilder('Action')
+                   ->andWhere('Action.workflowName LIKE :workflowName')
+                   ->setParameter('workflowName', "%{$workflowName}%")
+                   ->andWhere('Action.eventReference = :eventReference')
+                   ->setParameter('eventReference', $eventReference)
+                ;
+                   
+        return $qb->getQuery()->getResult();
+    }
 
 }

+ 1 - 2
Resources/translations/WorkflowLabel.es.yml

@@ -53,9 +53,8 @@ workflow:
       active: Activar
   onu_workflow_2:
     places:
-      created: Creado
       deleted: Eliminado
-      active: Habilitado
+      active: Activo
       disable: Deshabilitado
     transitions:
       created_active: Activar

+ 7 - 7
Services/ProducerService.php

@@ -74,7 +74,6 @@ class ProducerService
         $entityRepository = $entityManager->getRepository($entityClass);
         $entity = $entityRepository->find($entityId);
         if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
-	    
             $this->publishMessage($action, $entity);
         }
     }
@@ -82,14 +81,15 @@ class ProducerService
     /**
      * @param Action $action
      * @param Entity $entity
+     * @param string $routing_key
      */
     public function publishMessage(Action $action, $entity, $routing_key = false)
     {
-        if($routing_key === false){
-		$routing_key = "";
-		if(getenv("AMQP_KEY") !== false){
-		     $routing_key = getenv("AMQP_KEY");
-		}
+        if ($routing_key === false) {
+    		$routing_key = "";
+    		if (getenv("AMQP_KEY") !== false) {
+                $routing_key = getenv("AMQP_KEY");
+            }
         }
         $entityClass = get_class($entity);
         $actionName = $action->getName();
@@ -100,7 +100,7 @@ class ProducerService
             'entityClass' => $entityClass,
             'entityId' => $entityId,
         );
-	$this->producer->publish(serialize($msg), $routing_key);
+        $this->producer->publish(serialize($msg), $routing_key);
                 
         $msg = serialize($msg);
 

+ 4 - 4
Services/TaskLoggerService.php

@@ -45,7 +45,7 @@ class TaskLoggerService implements ConsumerInterface
 
             $filename = $this->createTaskLoggerCmdFile($taskloggerId, $content);
             $output = $this->runProcess($filename);
- 	    $output["process"] = $filename;
+     	    $output["process"] = $filename;
             
             $monologLoggerId = 'monolog.logger.devicelog';
             if ($this->serviceContainer->has($monologLoggerId)) {
@@ -92,11 +92,11 @@ class TaskLoggerService implements ConsumerInterface
      */
     public function runProcess($filename)
     {
-	$predir = getcwd();
-	chdir(dirname($filename));
+    	$predir = getcwd();
+    	chdir(dirname($filename));
         $process = new Process($filename);
         $process->run();
-	chdir($predir);
+    	chdir($predir);
 	
 
         return array(