Completed
Pull Request — master (#123)
by Sascha
04:41
created

GroupRunnersCompilerPass::process()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.5806
cc 4
eloc 22
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
        $container->setParameter('liip_monitor.runners', $runners);
46
    }
47
48
    /**
49
     * @param array $services
50
     *
51
     * @return array
52
     */
53
    private function getGroups(array $services)
54
    {
55
        $groups = array();
56
        foreach ($services as $serviceId => $tags) {
57
            foreach ($tags as $attributes) {
58
                if (!empty($attributes['group'])) {
59
                    $groups[$attributes['group']] = true;
60
                }
61
            }
62
        }
63
64
        return array_keys($groups);
65
    }
66
67
    /**
68
     * @param ContainerBuilder $container
69
     *
70
     * @return array
71
     */
72
    private function getGroupsFromParameter(ContainerBuilder $container)
73
    {
74
        $groups = array();
75
76
        if ($container->hasParameter('liip_monitor.checks')) {
77
            $checks = $container->getParameter('liip_monitor.checks');
78
            foreach (array_keys($checks['groups']) as $group) {
79
                $groups[] = $group;
80
            }
81
        }
82
83
        return $groups;
84
    }
85
}
86