WorkerRunnerManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Gendoria\CommandQueueBundle\Worker;
4
5
use Gendoria\CommandQueue\Worker\WorkerRunnerManager as BaseRunnerManager;
6
use InvalidArgumentException;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
10
/**
11
 * Description of WorkerRunnerManager
12
 *
13
 * @author Tomasz Struczyński <[email protected]>
14
 */
15
class WorkerRunnerManager extends BaseRunnerManager
16
{
17
    /**
18
     * Worker runner services configuration.
19
     * 
20
     * @var array
21
     */
22
    private $runnerServices = array();
23
    
24
    /**
25
     * Container.
26
     * 
27
     * @var ContainerInterface
28
     */
29
    private $container;
30
    
31 5
    public function __construct(ContainerInterface $container)
32
    {
33 5
        $this->container = $container;
34 5
    }
35
36
    /**
37
     * Register runner service.
38
     * 
39
     * @param string $name Worker name.
40
     * @param string $id Service ID.
41
     * @param array $options Worker options.
42
     * @throws InvalidArgumentException Thrown, when there is no worker runner service registered in container.
43
     */
44 3
    public function addRunnerService($name, $id, array $options = array())
45
    {
46 3
        if (!$this->container->has($id)) {
47 1
            throw new InvalidArgumentException("Service container does not have required service registered.");
48
        }
49 2
        $this->runnerServices[$name] = array(
50 2
            'id' => $id,
51 2
            'options' => $options,
52
        );
53 2
    }
54
    
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function has($name)
59
    {
60 3
        if (parent::has($name)) {
61 1
            return true;
62
        }
63 3
        return !empty($this->runnerServices[$name]);
64
    }
65
    
66
    /**
67
     * {@inheritdoc}
68
     */
69 2
    public function run($name, OutputInterface $output = null)
70
    {
71 2
        if (!$this->has($name)) {
72 1
            throw new \InvalidArgumentException("No runner service registered for provided name.");
73
        }
74 1
        if (!parent::has($name)) {
75 1
            $runner = $this->container->get($this->runnerServices[$name]['id']);
76 1
            $this->addRunner($name, $runner, $this->runnerServices[$name]['options']);
77 1
        }
78 1
        parent::run($name, $output);
79 1
    }
80
    
81
    /**
82
     * {@inheritdoc}
83
     */
84 1
    public function getRunners()
85
    {
86 1
        return array_merge(array_keys($this->runnerServices), parent::getRunners());
87
    }
88
89
}
90