Completed
Push — master ( 355f41...200862 )
by Mickael
06:25
created

AbstractGearmanService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 97
ccs 12
cts 21
cp 0.5714
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
B getJob() 0 22 5
A getWorker() 0 11 3
A getWorkers() 0 4 1
1
<?php
2
3
/**
4
 * Gearman Bundle for Symfony2 / Symfony3
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 Mkk\GearmanBundle\Service\Abstracts;
15
16
use Mkk\GearmanBundle\Exceptions\JobDoesNotExistException;
17
use Mkk\GearmanBundle\Exceptions\WorkerDoesNotExistException;
18
use Mkk\GearmanBundle\Service\GearmanCacheWrapper;
19
20
/**
21
 * Gearman execute methods. All Worker methods
22
 *
23
 * @author Marc Morera <[email protected]>
24
 */
25
abstract class AbstractGearmanService
26
{
27
    /**
28
     * All workers
29
     *
30
     * @var array
31
     */
32
    protected $workers;
33
34
    /**
35
     * The prefix for all job names
36
     *
37
     * @var string $jobPrefix
38
     */
39
    protected $jobPrefix = null;
40
41
    /**
42
     * Construct method
43
     *
44
     * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper
45
     * @param array               $defaultSettings     The default settings for the bundle
46
     */
47 3
    public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $defaultSettings)
48
    {
49 3
        $this->workers = $gearmanCacheWrapper->getWorkers();
50
51 3
        if (isset($defaultSettings['job_prefix'])) {
52
53
            $this->jobPrefix = $defaultSettings['job_prefix'];
54
        }
55 3
    }
56
57
    /**
58
     * Return worker containing a job with $jobName as name
59
     * If is not found, throws JobDoesNotExistException Exception
60
     *
61
     * @param string $jobName Name of job
62
     *
63
     * @return array
64
     *
65
     * @throws JobDoesNotExistException
66
     */
67 1
    public function getJob($jobName)
68
    {
69 1
        $jobName = $this->jobPrefix . $jobName;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $jobName. This often makes code more readable.
Loading history...
70
71 1
        foreach ($this->workers as $worker) {
72
73 1
            if (is_array($worker['jobs'])) {
74
75 1
                foreach ($worker['jobs'] as $job) {
76
77 1
                    if ($jobName === $job['realCallableName']) {
78
79 1
                        $worker['job'] = $job;
80
81 1
                        return $worker;
82
                    }
83
                }
84
            }
85
        }
86
87
        throw new JobDoesNotExistException();
88
    }
89
90
    /**
91
     * Return worker with $workerName as name and all its jobs
92
     * If is not found, throws WorkerDoesNotExistException Exception
93
     *
94
     * @param string $workerName Name of worker
95
     *
96
     * @return array
97
     *
98
     * @throws WorkerDoesNotExistException
99
     */
100
    public function getWorker($workerName)
101
    {
102
        foreach ($this->workers as $worker) {
103
104
            if ($workerName === $worker['callableName']) {
105
                return $worker;
106
            }
107
        }
108
109
        throw new WorkerDoesNotExistException();
110
    }
111
112
    /**
113
     * Return array of workers
114
     *
115
     * @return array all available workers
116
     */
117
    public function getWorkers()
118
    {
119
        return $this->workers;
120
    }
121
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
122