AddGroupsCompilerPass::parseGroups()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5
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 AddGroupsCompilerPass implements CompilerPassInterface
9
{
10
    const SERVICE_ID_PREFIX = 'liip_monitor.check.';
11
12 41
    public function process(ContainerBuilder $container)
13
    {
14 41
        if (!$container->hasParameter('liip_monitor.checks')) {
15 3
            return;
16
        }
17
18 38
        $checkConfig = $container->getParameter('liip_monitor.checks');
19
20 38
        list($checks, $checkCollections) = $this->parseGroups($container, $checkConfig['groups']);
21
22 38
        $this->addGroupTags($container, $checks, 'liip_monitor.check');
23 38
        $this->addGroupTags($container, $checkCollections, 'liip_monitor.check_collection');
24 38
    }
25
26
    /**
27
     * @return array
28
     */
29 38
    private function parseGroups(ContainerBuilder $container, array $data)
30
    {
31 38
        $checks = [];
32 38
        $checkCollections = [];
33
34 38
        foreach ($data as $group => $groupChecks) {
35 38
            foreach (array_keys($groupChecks) as $checkName) {
36 38
                $serviceId = self::SERVICE_ID_PREFIX.$checkName;
37 38
                $checkDefinition = $container->getDefinition($serviceId);
38
39 38
                if ($checkDefinition->hasTag('liip_monitor.check')) {
40 21
                    $checks[$checkName][] = $group;
41 18
                } elseif ($checkDefinition->hasTag('liip_monitor.check_collection')) {
42 18
                    $checkCollections[$checkName][] = $group;
43
                }
44
            }
45
        }
46
47 38
        return [$checks, $checkCollections];
48
    }
49
50
    /**
51
     * This Method completes the service definitions of each check for a configured group.
52
     *
53
     * For every configured check (per group) a parameter has been generated in LiipMonitorExtension::setParameters.
54
     * So the finally generated parameters have to be injected into each check service definition.
55
     * (see the preg_match part).
56
     *
57
     * @param string $tag
58
     */
59 38
    private function addGroupTags(ContainerBuilder $container, array $checks, $tag)
60
    {
61 38
        foreach ($checks as $checkName => $groups) {
62 38
            $serviceId = self::SERVICE_ID_PREFIX.$checkName;
63 38
            $serviceDefinition = $container->getDefinition($serviceId);
64 38
            $serviceDefinition->clearTag($tag);
65
66 38
            foreach ($groups as $group) {
67 38
                $tmpDefinition = clone $serviceDefinition;
68 38
                $tmpDefinition->addTag($tag, ['group' => $group, 'alias' => $checkName]);
69
70 38
                foreach ($tmpDefinition->getArguments() as $argumentIndex => $argument) {
71 36
                    if (is_string($argument) && preg_match('/^%%(.*)%%$/', $argument, $matches)) {
72 36
                        $newArgument = $container->getParameter($matches[1].'.'.$group);
73 36
                        $tmpDefinition->replaceArgument($argumentIndex, $newArgument);
74
                    }
75
                }
76
77 38
                $container->setDefinition($serviceId.'.'.$group, $tmpDefinition);
78
            }
79
80 38
            $container->removeDefinition($serviceId);
81
        }
82 38
    }
83
}
84