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
|
7 |
|
public function __construct(ContainerInterface $container) |
14
|
|
|
{ |
15
|
7 |
|
$this->container = $container; |
16
|
7 |
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string|null $group |
20
|
|
|
* |
21
|
|
|
* @return Runner|null |
22
|
|
|
*/ |
23
|
4 |
|
public function getRunner($group) |
24
|
|
|
{ |
25
|
4 |
|
$runnerServiceId = $this->getRunnerServiceId($group); |
26
|
|
|
|
27
|
4 |
|
return $runnerServiceId ? $this->container->get($runnerServiceId) : null; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return array|Runner[] key/value $group/$runner |
32
|
|
|
*/ |
33
|
1 |
View Code Duplication |
public function getRunners() |
|
|
|
|
34
|
|
|
{ |
35
|
1 |
|
$runnerServiceIds = $this->container->getParameter('liip_monitor.runners'); |
36
|
|
|
|
37
|
1 |
|
$runners = []; |
38
|
|
|
|
39
|
1 |
|
foreach ($runnerServiceIds as $serviceId) { |
40
|
1 |
|
if (preg_match('/liip_monitor.runner_(.+)/', $serviceId, $matches)) { |
41
|
1 |
|
$runners[$matches[1]] = $this->container->get($serviceId); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
return $runners; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array|string[] |
50
|
|
|
*/ |
51
|
1 |
View Code Duplication |
public function getGroups() |
|
|
|
|
52
|
|
|
{ |
53
|
1 |
|
$runnerServiceIds = $this->container->getParameter('liip_monitor.runners'); |
54
|
|
|
|
55
|
1 |
|
$groups = []; |
56
|
|
|
|
57
|
1 |
|
foreach ($runnerServiceIds as $serviceId) { |
58
|
1 |
|
if (preg_match('/liip_monitor.runner_(.+)/', $serviceId, $matches)) { |
59
|
1 |
|
$groups[] = $matches[1]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return $groups; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
2 |
|
public function getDefaultGroup() |
70
|
|
|
{ |
71
|
2 |
|
return $this->container->getParameter('liip_monitor.default_group'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string|null $group |
76
|
|
|
* |
77
|
|
|
* @return string|null |
78
|
|
|
*/ |
79
|
4 |
|
private function getRunnerServiceId($group) |
80
|
|
|
{ |
81
|
4 |
|
if (null === $group) { |
82
|
1 |
|
$group = $this->getDefaultGroup(); |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
$runnerServiceId = 'liip_monitor.runner_'.$group; |
86
|
|
|
|
87
|
4 |
|
return $this->container->has($runnerServiceId) ? $runnerServiceId : null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getReporters() |
94
|
|
|
{ |
95
|
|
|
$runners = $this->getRunners(); |
96
|
|
|
$reporters = []; |
97
|
|
|
|
98
|
|
|
foreach ($runners as $runner) { |
99
|
|
|
$reporters += array_keys($runner->getReporters() + $runner->getAdditionalReporters()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $reporters; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.