Completed
Push — master ( 8ae834...5a6d20 )
by Tomasz
10:40
created

WorkerRunnerManager::addRunner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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
    public function addRunner($name, WorkerRunnerInterface $runner, array $options = array())
31
    {
32
        $this->runners[$name] = array(
33
            'runner' => $runner,
34
            'options' => $options,
35
        );
36
    }
37
    
38
    /**
39
     * Return true, if worker with given name is registered, false otherwise.
40
     * 
41
     * @param string $name
42
     * @return boolean
43
     */
44
    public function has($name)
45
    {
46
        return array_key_exists($name, $this->runners);
47
    }
48
    
49
    /**
50
     * Run worker.
51
     * 
52
     * @param string $name
53
     * @param OutputInterface $output
54
     * @throws InvalidArgumentException Thrown, if worker cannto be found for provided name.
55
     * @throws Exception Can be thrown, if runner resulted with an error.
56
     */
57
    public function run($name, OutputInterface $output = null)
58
    {
59
        if (!$this->has($name)) {
60
            throw new InvalidArgumentException("No runner service registered for provided name.");
61
        }
62
        /* @var $runner WorkerRunnerInterface */
63
        $runner = $this->runners[$name]['runner'];
64
        $runner->run($this->runners[$name]['options'], $output);
65
    }
66
    
67
    /**
68
     * Get registered runners.
69
     * 
70
     * @return string[]
1 ignored issue
show
Documentation introduced by
Should the return type not be array<integer|string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
71
     */
72
    public function getRunners()
73
    {
74
        return array_keys($this->runners);
75
    }
76
77
}
78