GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#158)
by Bernardo Vieira da
12:24
created

WorkerClass   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 329
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 84.72%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 329
ccs 61
cts 72
cp 0.8472
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 48 5
A loadServers() 0 14 4
A loadIterations() 0 6 2
A loadDefaultMethod() 0 6 2
A loadMinimumExecutionTime() 0 6 2
A loadTimeout() 0 6 2
A createJobCollection() 0 38 4
A toArray() 0 17 1
1
<?php
2
3
/**
4
 * Gearman Bundle for Symfony2
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * Feel free to edit as you please, and have fun.
10
 *
11
 * @author Marc Morera <[email protected]>
12
 */
13
14
namespace Mmoreram\GearmanBundle\Module;
15
16
use Doctrine\Common\Annotations\Reader;
17
use ReflectionClass;
18
19
use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
20
use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
21
use Mmoreram\GearmanBundle\Module\JobClass as Job;
22
23
/**
24
 * Worker class
25
 *
26
 * This class provide all worker definition.
27
 *
28
 * @since 2.3.1
29
 */
30
class WorkerClass
31
{
32
    /**
33
     * @var string
34
     *
35
     * Default description when is not defined
36
     */
37
    const DEFAULT_DESCRIPTION = 'No description is defined';
38
39
    /**
40
     * @var string
41
     *
42
     * Namespace of worker class
43
     */
44
    private $namespace;
45
46
    /**
47
     * @var string
48
     *
49
     * Class name of worker
50
     */
51
    private $className;
52
53
    /**
54
     * @var string
55
     *
56
     * Filename of worker
57
     */
58
    private $fileName;
59
60
    /**
61
     * @var string
62
     *
63
     * Callable name for this job.
64
     * If is setted on annotations, this value will be used.
65
     * Otherwise, natural method name will be used.
66
     */
67
    private $callableName;
68
69
    /**
70
     * @var string
71
     *
72
     * Service alias if this worker is wanted to be built by dependency injection
73
     */
74
    private $service;
75
76
    /**
77
     * @var string
78
     *
79
     * Description of Job
80
     */
81
    private $description;
82
83
    /**
84
     * @var integer
85
     *
86
     * Number of iterations this job will be alive before die
87
     */
88
    private $iterations;
89
90
    /**
91
     * @var string
92
     *
93
     * Default method this job will be call into Gearman client
94
     */
95
    private $defaultMethod;
96
97
    /**
98
     * @var int
99
     *
100
     * Job minimum execution time
101
     */
102
    private $minimumExecutionTime;
103
104
    /**
105
     * @var int
106
     *
107
     * Timeout for idle job
108
     */
109
    private $timeout;
110
111
    /**
112
     * @var array
113
     *
114
     * Collection of servers to connect
115
     */
116
    private $servers;
117
118
    /**
119
     * @var JobCollection
120
     *
121
     * All jobs inside Worker
122
     */
123
    private $jobCollection;
124
125
    /**
126
     * The prefix for all job names
127
     *
128
     * @var string $jobPrefix
129
     */
130
    private $jobPrefix = null;
131
132
    /**
133
     * Retrieves all jobs available from worker
134
     *
135
     * @param WorkAnnotation  $workAnnotation  workAnnotation class
136
     * @param ReflectionClass $reflectionClass Reflexion class
137
     * @param Reader          $reader          Reader class
138
     * @param array           $servers         Array of servers defined for Worker
139
     * @param array           $defaultSettings Default settings for Worker
140
     */
141 3
    public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $reflectionClass, Reader $reader, array $servers, array $defaultSettings)
142
    {
143
144 3
        $this->namespace = $reflectionClass->getNamespaceName();
145
146
        /**
147
         * If WorkAnnotation name field is defined, workers_name_prepend_namespace value
148
         * in defaultSettings array must be checked.
149
         *
150
         * If true, namespace must be prepended to workAnnotation name for callableName
151
         * Otherwise, only workAnnotation value is set as callableName
152
         */
153 3
        $callableNameNamespace = $defaultSettings['workers_name_prepend_namespace']
154 3
            ? $this->namespace
155 3
            : '';
156
157
        /**
158
         * Setting worker callable name
159
         */
160 3
        $this->callableName = is_null($workAnnotation->name)
161 2
            ? $reflectionClass->getName()
162 1
            : $callableNameNamespace . $workAnnotation->name;
163
164 3
        $this->callableName = str_replace('\\', '', $this->callableName);
165
166
        /**
167
         * Setting worker description
168
         */
169 3
        $this->description = is_null($workAnnotation->description)
170 2
            ? self::DEFAULT_DESCRIPTION
171 1
            : $workAnnotation->description;
172
173 3
        $this->fileName = $reflectionClass->getFileName();
174 3
        $this->className = $reflectionClass->getName();
175 3
        $this->service = $workAnnotation->service;
176
177 3
        if (isset($defaultSettings['job_prefix'])) {
178
179
            $this->jobPrefix = $defaultSettings['job_prefix'];
180
        }
181
182 3
        $this->servers = $this->loadServers($workAnnotation, $servers);
183 3
        $this->iterations = $this->loadIterations($workAnnotation, $defaultSettings);
184 3
        $this->defaultMethod = $this->loadDefaultMethod($workAnnotation, $defaultSettings);
185 3
        $this->minimumExecutionTime = $this->loadMinimumExecutionTime($workAnnotation, $defaultSettings);
186 3
        $this->timeout = $this->loadTimeout($workAnnotation, $defaultSettings);
187 3
        $this->jobCollection = $this->createJobCollection($reflectionClass, $reader);
188 3
    }
189
190
    /**
191
     * Load servers
192
     *
193
     * If any server is defined in JobAnnotation, this one is used.
194
     * Otherwise is used servers set in Class
195
     *
196
     * @param WorkAnnotation $workAnnotation WorkAnnotation class
197
     * @param array          $servers        Array of servers defined for Worker
198
     *
199
     * @return array Servers
200
     */
201 3
    private function loadServers(WorkAnnotation $workAnnotation, array $servers)
202
    {
203
        /**
204
         * If is configured some servers definition in the worker, overwrites
205
         */
206 3
        if ($workAnnotation->servers) {
207
208 2
            $servers = (is_array($workAnnotation->servers) && !isset($workAnnotation->servers['host']))
209 1
                ? $workAnnotation->servers
210 2
                : array($workAnnotation->servers);
211
        }
212
213 3
        return $servers;
214
    }
215
216
    /**
217
     * Load iterations
218
     *
219
     * If iterations is defined in WorkAnnotation, this one is used.
220
     * Otherwise is used set in Class
221
     *
222
     * @param WorkAnnotation $workAnnotation  WorkAnnotation class
223
     * @param array          $defaultSettings Default settings for Worker
224
     *
225
     * @return integer Iteration
226
     */
227 3
    private function loadIterations(WorkAnnotation $workAnnotation, array $defaultSettings)
228
    {
229 3
        return is_null($workAnnotation->iterations)
230 2
            ? (int) $defaultSettings['iterations']
231 3
            : (int) $workAnnotation->iterations;
232
    }
233
234
    /**
235
     * Load defaultMethod
236
     *
237
     * If defaultMethod is defined in WorkAnnotation, this one is used.
238
     * Otherwise is used set in Class
239
     *
240
     * @param WorkAnnotation $workAnnotation  WorkAnnotation class
241
     * @param array          $defaultSettings Default settings for Worker
242
     *
243
     * @return string Default method
244
     */
245 3
    private function loadDefaultMethod(WorkAnnotation $workAnnotation, array $defaultSettings)
246
    {
247 3
        return is_null($workAnnotation->defaultMethod)
248 2
            ? $defaultSettings['method']
249 3
            : $workAnnotation->defaultMethod;
250
    }
251
252
    /**
253
     * Load minimumExecutionTime
254
     *
255
     * If minimumExecutionTime is defined in JobAnnotation, this one is used.
256
     * Otherwise is used set in Class
257
     *
258
     * @param WorkAnnotation $workAnnotation
259
     * @param array $defaultSettings
260
     *
261
     * @return int
262
     */
263 3
    private function loadMinimumExecutionTime(WorkAnnotation $workAnnotation, array $defaultSettings)
264
    {
265 3
        return is_null($workAnnotation->minimumExecutionTime)
266 3
            ? (int) $defaultSettings['minimum_execution_time']
267 3
            : (int) $workAnnotation->minimumExecutionTime;
268
    }
269
270
    /**
271
     * Load timeout
272
     *
273
     * If timeout is defined in JobAnnotation, this one is used.
274
     * Otherwise is used set in Class
275
     *
276
     * @param WorkAnnotation $workAnnotation
277
     * @param array $defaultSettings
278
     *
279
     * @return int
280
     */
281 3
    private function loadTimeout(WorkAnnotation $workAnnotation, array $defaultSettings)
282
    {
283 3
        return is_null($workAnnotation->timeout)
284 3
            ? (int) $defaultSettings['timeout']
285 3
            : (int) $workAnnotation->timeout;
286
    }
287
288
    /**
289
     * Creates job collection of worker
290
     *
291
     * @param ReflectionClass $reflectionClass Reflexion class
292
     * @param Reader          $reader          ReaderAnnotation class
293
     *
294
     * @return WorkerClass self Object
295
     */
296 3
    private function createJobCollection(ReflectionClass $reflectionClass, Reader $reader)
297
    {
298 3
        $jobCollection = new JobCollection;
299
300
        /**
301
         * For each defined method, we parse it
302
         */
303 3
        foreach ($reflectionClass->getMethods() as $reflectionMethod) {
304
305
            $methodAnnotations = $reader->getMethodAnnotations($reflectionMethod);
306
307
            /**
308
             * Every annotation found is parsed
309
             */
310
            foreach ($methodAnnotations as $methodAnnotation) {
311
312
                /**
313
                 * Annotation is only loaded if is typeof JobAnnotation
314
                 */
315
                if ($methodAnnotation instanceof JobAnnotation) {
316
                    /**
317
                     * Creates new Job
318
                     */
319
                    $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, array(
320
                        'jobPrefix'            => $this->jobPrefix,
321
                        'iterations'           => $this->iterations,
322
                        'method'               => $this->defaultMethod,
323
                        'minimumExecutionTime' => $this->minimumExecutionTime,
324
                        'timeout'              => $this->timeout,
325
                    ));
326
327
                    $jobCollection->add($job);
328
                }
329
            }
330
        }
331
332 3
        return $jobCollection;
333
    }
334
335
    /**
336
     * Retrieve all Worker data in cache format
337
     *
338
     * @return array
339
     */
340 3
    public function toArray()
341
    {
342
        return array(
343
344 3
            'namespace'            => $this->namespace,
345 3
            'className'            => $this->className,
346 3
            'fileName'             => $this->fileName,
347 3
            'callableName'         => $this->callableName,
348 3
            'description'          => $this->description,
349 3
            'service'              => $this->service,
350 3
            'servers'              => $this->servers,
351 3
            'iterations'           => $this->iterations,
352 3
            'minimumExecutionTime' => $this->minimumExecutionTime,
353 3
            'timeout'              => $this->timeout,
354 3
            'jobs'                 => $this->jobCollection->toArray(),
355
        );
356
    }
357
358
}
359