Completed
Pull Request — master (#249)
by Kevin
08:26
created

GroupRunnersCompilerPass   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 97.44%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 74
ccs 38
cts 39
cp 0.9744
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroups() 0 13 4
A getGroupsFromParameter() 0 13 3
A process() 0 37 4
1
<?php
2
3
namespace Liip\MonitorBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
8
class GroupRunnersCompilerPass implements CompilerPassInterface
9
{
10 43
    public function process(ContainerBuilder $container)
11
    {
12 43
        $noRunner = false === $container->hasDefinition('liip_monitor.runner');
13 43
        $noDefaultGroup = false === $container->hasParameter('liip_monitor.default_group');
14
15 43
        if ($noRunner || $noDefaultGroup) {
16
            return;
17
        }
18
19 43
        $definition = $container->getDefinition('liip_monitor.runner');
20 43
        $container->removeDefinition('liip_monitor.runner');
21
22 43
        $defaultGroup = $container->getParameter('liip_monitor.default_group');
23
24 43
        $checkServices = $container->findTaggedServiceIds('liip_monitor.check');
25 43
        $checkCollectionServices = $container->findTaggedServiceIds('liip_monitor.check_collection');
26
27 43
        $groups = array_merge(
28 43
            [$defaultGroup],
29 43
            $this->getGroups($checkServices),
30 43
            $this->getGroups($checkCollectionServices),
31 43
            $this->getGroupsFromParameter($container)
32
        );
33 43
        $groups = array_unique($groups);
34
35 43
        $runners = [];
36 43
        foreach ($groups as $group) {
37 43
            $container->setDefinition('liip_monitor.runner_'.$group, clone $definition);
38 43
            $runners[] = 'liip_monitor.runner_'.$group;
39
        }
40
41 43
        $container->setAlias('liip_monitor.runner', 'liip_monitor.runner_'.$defaultGroup);
42 43
        $runner = $container->getAlias('liip_monitor.runner');
43 43
        $runner->setPublic(true);
44
45 43
        $container->setParameter('liip_monitor.runners', $runners);
46 43
    }
47
48
    /**
49
     * @return array
50
     */
51 43
    private function getGroups(array $services)
52
    {
53 43
        $groups = [];
54 43
        foreach ($services as $serviceId => $tags) {
55 40
            foreach ($tags as $attributes) {
56 40
                if (!empty($attributes['group'])) {
57 38
                    $groups[$attributes['group']] = true;
58
                }
59
            }
60
        }
61
62 43
        return array_keys($groups);
63
    }
64
65
    /**
66
     * @return array
67
     */
68 43
    private function getGroupsFromParameter(ContainerBuilder $container)
69
    {
70 43
        $groups = [];
71
72 43
        if ($container->hasParameter('liip_monitor.checks')) {
73 38
            $checks = $container->getParameter('liip_monitor.checks');
74 38
            foreach (array_keys($checks['groups']) as $group) {
75 38
                $groups[] = $group;
76
            }
77
        }
78
79 43
        return $groups;
80
    }
81
}
82