WorkerRunnerManager::getRunners()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Gendoria\CommandQueue\Worker;
4
5
use Exception;
6
use InvalidArgumentException;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Worker runner manager has capabilities of managing workers.
11
 *
12
 * @author Tomasz Struczyński <[email protected]>
13
 */
14
class WorkerRunnerManager
15
{
16
    /**
17
     * Worker runner services configuration.
18
     * 
19
     * @var array
20
     */
21
    private $runners = array();
22
    
23
    /**
24
     * Register runner.
25
     * 
26
     * @param string $name Worker name.
27
     * @param WorkerRunnerInterface $runner Worker runner.
28
     * @param array $options Worker options.
29
     */
30 5
    public function addRunner($name, WorkerRunnerInterface $runner, array $options = array())
31
    {
32 5
        $this->runners[$name] = array(
33 5
            'runner' => $runner,
34 5
            'options' => $options,
35
        );
36 5
    }
37
    
38
    /**
39
     * Return true, if worker with given name is registered, false otherwise.
40
     * 
41
     * @param string $name
42
     * @return boolean
43
     */
44 5
    public function has($name)
45
    {
46 5
        return array_key_exists($name, $this->runners);
47
    }
48
    
49
    /**
50
     * Run worker.
51
     * 
52
     * @param string $name
53
     * @param OutputInterface $output
54
     * @return void
55
     * @throws InvalidArgumentException Thrown, if worker cannto be found for provided name.
56
     * @throws Exception Can be thrown, if runner resulted with an error.
57
     */
58 3
    public function run($name, OutputInterface $output = null)
59
    {
60 3
        if (!$this->has($name)) {
61 1
            throw new InvalidArgumentException("No runner service registered for provided name.");
62
        }
63
        /* @var $runner WorkerRunnerInterface */
64 2
        $runner = $this->runners[$name]['runner'];
65 2
        $runner->run($this->runners[$name]['options'], $output);
66 2
    }
67
    
68
    /**
69
     * Get registered runners.
70
     * 
71
     * @return string[]
72
     */
73 3
    public function getRunners()
74
    {
75 3
        return array_keys($this->runners);
76
    }
77
78
}
79