Completed
Pull Request — master (#123)
by Sascha
26:40 queued 15:43
created

RunnerManager::getRunner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Liip\MonitorBundle\Helper;
4
5
use Liip\MonitorBundle\Runner;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
8
class RunnerManager
9
{
10
    /** @var ContainerInterface */
11
    private $container;
12
13
    /**
14
     * @param ContainerInterface $container
15
     */
16
    public function __construct(ContainerInterface $container)
17
    {
18
        $this->container = $container;
19
    }
20
21
    /**
22
     * @param string $group
23
     *
24
     * @return null|Runner
25
     */
26
    public function getRunner($group)
27
    {
28
        $runnerServiceId = $this->getRunnerServiceId($group);
29
30
        return $runnerServiceId ? $this->container->get($runnerServiceId) : null;
31
    }
32
33
    /**
34
     * @return array|Runner[] key/value $group/$runner
35
     */
36
    public function getRunners()
37
    {
38
        $runnerServiceIds = $this->container->getParameter('liip_monitor.runners');
39
40
        $runners = array();
41
42
        foreach ($runnerServiceIds as $serviceId) {
43
            if (preg_match('/liip_monitor.runner_(.+)/', $serviceId, $matches)) {
44
                $runners[$matches[1]] = $this->container->get($serviceId);
45
            }
46
        }
47
48
        return $runners;
49
    }
50
51
    /**
52
     * @return array|string[]
53
     */
54
    public function getGroups()
55
    {
56
        $runnerServiceIds = $this->container->getParameter('liip_monitor.runners');
57
58
        $groups = array();
59
60
        foreach ($runnerServiceIds as $serviceId) {
61
            if (preg_match('/liip_monitor.runner_(.+)/', $serviceId, $matches)) {
62
                $groups[] = $matches[1];
63
            }
64
        }
65
66
        return $groups;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getDefaultGroup()
73
    {
74
        return $this->container->getParameter('liip_monitor.default_group');
75
    }
76
77
    /**
78
     * @param string $group
79
     *
80
     * @return null|string
81
     */
82
    private function getRunnerServiceId($group)
83
    {
84
        if (null === $group) {
85
            $group = $this->getDefaultGroup();
86
        }
87
88
        $runnerServiceId = 'liip_monitor.runner_'.$group;
89
90
        return $this->container->has($runnerServiceId) ? $runnerServiceId : null;
91
    }
92
}