Completed
Push — master ( 0585fd...6ce168 )
by Mickael
03:38
created

AbstractGearmanService::getJob()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 5
nop 1
crap 5.0342
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 1
    public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $defaultSettings)
48
    {
49 1
        $this->workers = $gearmanCacheWrapper->getWorkers();
50 1
        if (isset($defaultSettings['job_prefix'])) {
51
            $this->jobPrefix = $defaultSettings['job_prefix'];
52
        }
53 1
    }
54
55
    /**
56
     * Return worker containing a job with $jobName as name
57
     * If is not found, throws JobDoesNotExistException Exception
58
     *
59
     * @param string $jobName Name of job
60
     *
61
     * @return array
62
     *
63
     * @throws JobDoesNotExistException
64
     */
65 1
    public function getJob($jobName)
66
    {
67 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...
68 1
        foreach ($this->workers as $worker) {
69 1
            if (is_array($worker['jobs'])) {
70 1
                foreach ($worker['jobs'] as $job) {
71 1
                    if ($jobName === $job['realCallableName']) {
72 1
                        $worker['job'] = $job;
73 1
                        return $worker;
74
                    }
75
                }
76
            }
77
        }
78
        throw new JobDoesNotExistException();
79
    }
80
81
    /**
82
     * Return worker with $workerName as name and all its jobs
83
     * If is not found, throws WorkerDoesNotExistException Exception
84
     *
85
     * @param string $workerName Name of worker
86
     *
87
     * @return array
88
     *
89
     * @throws WorkerDoesNotExistException
90
     */
91
    public function getWorker($workerName)
92
    {
93
        foreach ($this->workers as $worker) {
94
            if ($workerName === $worker['callableName']) {
95
                return $worker;
96
            }
97
        }
98
        throw new WorkerDoesNotExistException();
99
    }
100
101
    /**
102
     * Return array of workers
103
     *
104
     * @return array all available workers
105
     */
106
    public function getWorkers()
107
    {
108
        return $this->workers;
109
    }
110
}
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...
111