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

GroupRunnersCompilerPass::getGroups()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 4
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($defaultGroup);
31
        $groups = array_merge($groups, $this->getGroups($checkServices));
32
        $groups = array_merge($groups, $this->getGroups($checkCollectionServices));
33
        $groups = array_merge($groups, $this->getGroupsFromParameter($container));
34
        $groups = array_unique($groups);
35
36
        $runners = array();
37
        foreach ($groups as $group) {
38
            $container->setDefinition('liip_monitor.runner_'.$group, clone $definition);
39
            $runners[] = 'liip_monitor.runner_'.$group;
40
        }
41
42
        $container->setAlias('liip_monitor.runner', 'liip_monitor.runner_'.$defaultGroup);
43
        $container->setParameter('liip_monitor.runners', $runners);
44
    }
45
46
    /**
47
     * @param array $services
48
     *
49
     * @return array
50
     */
51
    private function getGroups(array $services)
52
    {
53
        $groups = array();
54
        foreach ($services as $serviceId => $tags) {
55
            foreach ($tags as $attributes) {
56
                if (!empty($attributes['group'])) {
57
                    $groups[$attributes['group']] = true;
58
                }
59
            }
60
        }
61
62
        return array_keys($groups);
63
    }
64
65
    /**
66
     * @param ContainerBuilder $container
67
     *
68
     * @return array
69
     */
70
    private function getGroupsFromParameter(ContainerBuilder $container)
71
    {
72
        $groups = array();
73
74
        if ($container->hasParameter('liip_monitor.checks')) {
75
            $checks = $container->getParameter('liip_monitor.checks');
76
            foreach (array_keys($checks['groups']) as $group) {
77
                $groups[] = $group;
78
            }
79
        }
80
81
        return $groups;
82
    }
83
}
84