Completed
Push — master ( 20d667...bbf4c7 )
by Lukas Kahwe
9s
created

GroupRunnersCompilerPass::process()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
cc 4
nc 3
nop 1
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
    /**
11
     * @param ContainerBuilder $container
12
     */
13
    public function process(ContainerBuilder $container)
14
    {
15
        $noRunner = false === $container->hasDefinition('liip_monitor.runner');
16
        $noDefaultGroup = false === $container->hasParameter('liip_monitor.default_group');
17
18
        if ($noRunner || $noDefaultGroup) {
19
            return;
20
        }
21
22
        $definition = $container->getDefinition('liip_monitor.runner');
23
        $container->removeDefinition('liip_monitor.runner');
24
25
        $defaultGroup = $container->getParameter('liip_monitor.default_group');
26
27
        $checkServices = $container->findTaggedServiceIds('liip_monitor.check');
28
        $checkCollectionServices = $container->findTaggedServiceIds('liip_monitor.check_collection');
29
30
        $groups = array_merge(
31
            array($defaultGroup),
32
            $this->getGroups($checkServices),
33
            $this->getGroups($checkCollectionServices),
34
            $this->getGroupsFromParameter($container)
35
        );
36
        $groups = array_unique($groups);
37
38
        $runners = array();
39
        foreach ($groups as $group) {
40
            $container->setDefinition('liip_monitor.runner_'.$group, clone $definition);
41
            $runners[] = 'liip_monitor.runner_'.$group;
42
        }
43
44
        $container->setAlias('liip_monitor.runner', 'liip_monitor.runner_'.$defaultGroup);
45
        $runner = $container->getAlias('liip_monitor.runner');
46
        $runner->setPublic(true);
47
48
        $container->setParameter('liip_monitor.runners', $runners);
49
    }
50
51
    /**
52
     * @param array $services
53
     *
54
     * @return array
55
     */
56
    private function getGroups(array $services)
57
    {
58
        $groups = array();
59
        foreach ($services as $serviceId => $tags) {
60
            foreach ($tags as $attributes) {
61
                if (!empty($attributes['group'])) {
62
                    $groups[$attributes['group']] = true;
63
                }
64
            }
65
        }
66
67
        return array_keys($groups);
68
    }
69
70
    /**
71
     * @param ContainerBuilder $container
72
     *
73
     * @return array
74
     */
75
    private function getGroupsFromParameter(ContainerBuilder $container)
76
    {
77
        $groups = array();
78
79
        if ($container->hasParameter('liip_monitor.checks')) {
80
            $checks = $container->getParameter('liip_monitor.checks');
81
            foreach (array_keys($checks['groups']) as $group) {
82
                $groups[] = $group;
83
            }
84
        }
85
86
        return $groups;
87
    }
88
}
89