소스 검색

Some fixes for reducing NPath complexity

Marc 11 년 전
부모
커밋
c122d6dceb
2개의 변경된 파일133개의 추가작업 그리고 46개의 파일을 삭제
  1. 52 22
      Module/JobClass.php
  2. 81 24
      Module/WorkerClass.php

+ 52 - 22
Module/JobClass.php

@@ -9,8 +9,8 @@
 
 namespace Mmoreram\GearmanBundle\Module;
 
-use Mmoreram\GearmanBundle\Driver\Gearman\Job;
-use Mmoreram\GearmanBundle\Driver\Gearman\Work;
+use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
+use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
 use Symfony\Component\DependencyInjection\ContainerAware;
 use ReflectionMethod;
 
@@ -84,53 +84,83 @@ class JobClass extends ContainerAware
     /**
      * Construct method
      *
-     * @param Job              $methodAnnotation  MethodAnnotation class
+     * @param JobAnnotation    $jobAnnotation  jobAnnotation class
      * @param ReflectionMethod $method            ReflextionMethod class
      * @param string           $callableNameClass Callable name class
      * @param array            $servers           Array of servers defined for Worker
      * @param array            $defaultSettings   Default settings for Worker
      */
-    public function __construct( Job $methodAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
+    public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
     {
-        $this->callableName = is_null($methodAnnotation->name)
+        $this->callableName = is_null($jobAnnotation->name)
                             ? $method->getName()
-                            : $methodAnnotation->name;
+                            : $jobAnnotation->name;
 
         $this->methodName = $method->getName();
 
         $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
-        $this->description  = is_null($methodAnnotation->description)
+        $this->description  = is_null($jobAnnotation->description)
                             ? 'No description is defined'
-                            : $methodAnnotation->description;
+                            : $jobAnnotation->description;
 
-
-        $this->iterations   = is_null($methodAnnotation->iterations)
-                            ? (int) $defaultSettings['iterations']
-                            : $methodAnnotation->iterations;
-
-
-        $this->defaultMethod    = is_null($methodAnnotation->defaultMethod)
-                                ? $defaultSettings['method']
-                                : $methodAnnotation->defaultMethod;
+        $this
+            ->loadSettings($jobAnnotation, $defaultSettings)
+            ->loadServers($jobAnnotation, $servers);
+    }
 
 
+    /**
+     * Load settings
+     * 
+     * @param JobAnnotation $jobAnnotation JobAnnotation class
+     * @param array         $servers       Array of servers defined for Worker
+     * 
+     * @return JobClass self Object
+     */
+    private function loadServers(JobAnnotation $jobAnnotation, array $servers)
+    {
         /**
          * By default, this job takes default servers defined in its worker
          */
         $this->servers = $servers;
 
-
         /**
          * If is configured some servers definition in the worker, overwrites
          */
-        if ($methodAnnotation->servers) {
+        if ($jobAnnotation->servers) {
 
-            $this->servers  = ( is_array($methodAnnotation->servers) && !isset($methodAnnotation->servers['host']) )
-                            ? $methodAnnotation->servers
-                            : array($methodAnnotation->servers);
+            $this->servers  = ( is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']) )
+                            ? $jobAnnotation->servers
+                            : array($jobAnnotation->servers);
         }
+
+        return $this;
     }
 
+
+    /**
+     * Load settings
+     * 
+     * @param WorkAnnotation $JobAnnotation   JobAnnotation class
+     * @param array          $defaultSettings Default settings for Worker
+     * 
+     * @return JobClass self Object
+     */
+    private function loadSettings(JobAnnotation $jobAnnotation, array $defaultSettings)
+    {
+        $this->iterations   = is_null($jobAnnotation->iterations)
+                            ? (int) $defaultSettings['iterations']
+                            : $jobAnnotation->iterations;
+
+
+        $this->defaultMethod    = is_null($jobAnnotation->defaultMethod)
+                                ? $defaultSettings['method']
+                                : $jobAnnotation->defaultMethod;
+
+        return $this;
+    }
+
+
     /**
      * Retrieve all Job data in cache format
      *

+ 81 - 24
Module/WorkerClass.php

@@ -10,10 +10,10 @@
 namespace Mmoreram\GearmanBundle\Module;
 
 use Doctrine\Common\Annotations\Reader;
-use Mmoreram\GearmanBundle\Driver\Gearman\Work;
 use Mmoreram\GearmanBundle\Module\JobCollection;
 use Mmoreram\GearmanBundle\Module\JobClass as Job;
 use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
+use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
 use ReflectionClass;
 use ReflectionMethod;
 
@@ -99,6 +99,14 @@ class WorkerClass
     private $servers;
 
 
+    /**
+     * @var array
+     * 
+     * settings
+     */
+    private $settings;
+
+
     /**
      * @var JobCollection
      * 
@@ -110,48 +118,54 @@ class WorkerClass
     /**
      * Retrieves all jobs available from worker
      *
-     * @param Work            $classAnnotation ClassAnnotation class
+     * @param WorkAnnotation  $workAnnotation  workAnnotation class
      * @param ReflectionClass $reflectionClass Reflexion class
      * @param Reader          $reader          ReaderAnnotation class
      * @param array           $servers         Array of servers defined for Worker
      * @param array           $defaultSettings Default settings for Worker
      */
-    public function __construct(Work $classAnnotation, ReflectionClass $reflectionClass, Reader $reader, array $servers, array $defaultSettings)
+    public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $reflectionClass, Reader $reader, array $servers, array $defaultSettings)
     {
         $this->namespace = $reflectionClass->getNamespaceName();
 
         /**
          * Setting worker callable name
          */
-        $this->callableName = is_null($classAnnotation->name)
+        $this->callableName = is_null($workAnnotation->name)
                             ? $reflectionClass->getName()
-                            : $this->namespace .'\\' .$classAnnotation->name;
+                            : $this->namespace .'\\' .$workAnnotation->name;
 
         $this->callableName = str_replace('\\', '', $this->callableName);
 
         /**
          * Setting worker description
          */
-        $this->description  = is_null($classAnnotation->description)
+        $this->description  = is_null($workAnnotation->description)
                             ? 'No description is defined'
-                            : $classAnnotation->description;
+                            : $workAnnotation->description;
 
         $this->fileName = $reflectionClass->getFileName();
         $this->className = $reflectionClass->getName();
-        $this->service = $classAnnotation->service;
+        $this->service = $workAnnotation->service;
 
-        $this->iterations   = is_null($classAnnotation->iterations)
-                            ? (int) $defaultSettings['iterations']
-                            : $classAnnotation->iterations;
-
-        $defaultSettings['iterations'] = $this->iterations;
-
-        $this->defaultMethod    = is_null($classAnnotation->defaultMethod)
-                                ? $defaultSettings['method']
-                                : $classAnnotation->defaultMethod;
+        
+        $this
+            ->loadSettings($workAnnotation, $defaultSettings)
+            ->loadServers($workAnnotation, $servers)
+            ->createJobCollection($reflectionClass, $reader);
+    }
 
-        $defaultSettings['method'] = $this->defaultMethod;
 
+    /**
+     * Load settings
+     * 
+     * @param WorkAnnotation $workAnnotation WorkAnnotation class
+     * @param array          $servers        Array of servers defined for Worker
+     * 
+     * @return WorkerClass self Object
+     */
+    private function loadServers(WorkAnnotation $workAnnotation, array $servers)
+    {
         /**
          * By default, this worker takes default servers definition
          */
@@ -160,16 +174,57 @@ class WorkerClass
         /**
          * If is configured some servers definition in the worker, overwrites
          */
-        if ($classAnnotation->servers) {
+        if ($workAnnotation->servers) {
 
-            $this->servers  = ( is_array($classAnnotation->servers) && !isset($classAnnotation->servers['host']) )
-                            ? $classAnnotation->servers
-                            : array($classAnnotation->servers);
+            $this->servers  = ( is_array($workAnnotation->servers) && !isset($workAnnotation->servers['host']) )
+                            ? $workAnnotation->servers
+                            : array($workAnnotation->servers);
         }
 
-        $this->jobCollection = new JobCollection;
+        return $this;
+    }
 
 
+    /**
+     * Load settings
+     * 
+     * @param WorkAnnotation $workAnnotation  WorkAnnotation class
+     * @param array          $defaultSettings Default settings for Worker
+     * 
+     * @return WorkerClass self Object
+     */
+    private function loadSettings(WorkAnnotation $workAnnotation, array $defaultSettings)
+    {
+        $this->iterations   = is_null($workAnnotation->iterations)
+                            ? (int) $defaultSettings['iterations']
+                            : $workAnnotation->iterations;
+
+        $defaultSettings['iterations'] = $this->iterations;
+
+        $this->defaultMethod    = is_null($workAnnotation->defaultMethod)
+                                ? $defaultSettings['method']
+                                : $workAnnotation->defaultMethod;
+
+        $defaultSettings['method'] = $this->defaultMethod;
+
+        $this->settings = $defaultSettings;
+
+        return $this;
+    }
+
+
+    /**
+     * Creates job collection of worker
+     * 
+     * @param ReflectionClass $reflectionClass Reflexion class
+     * @param Reader          $reader          ReaderAnnotation class
+     * 
+     * @return WorkerClass self Object
+     */
+    private function createJobCollection(ReflectionClass $reflectionClass, Reader $reader)
+    {
+        $this->jobCollection = new JobCollection;
+
         /**
          * For each defined method, we parse it
          */
@@ -191,11 +246,13 @@ class WorkerClass
                     /**
                      * Creates new Job
                      */
-                    $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, $defaultSettings);
+                    $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, $this->settings);
                     $this->jobCollection->add($job);
                 }
             }
         }
+
+        return $this;
     }