Completed
Push — master ( a2df97...e9a9f0 )
by Vladimir
06:27
created

AddCheckPluginsCompilerPass::addCheckPlugin()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 37
ccs 19
cts 19
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 36
nop 5
crap 7
1
<?php
2
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Tvi\MonitorBundle\DependencyInjection\DiTags;
18
19
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
 * @author Vladimir Turnaev <[email protected]>
21
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
22
class AddCheckPluginsCompilerPass implements CompilerPassInterface
23
{
24
    public const SERVICE_ID_FORMAT = 'tvi_monitor.check.%s';
25
26
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
     * @var array
28
     */
29
    protected $checkServiceConfs = [];
30
31 47
    public function process(ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function process()
Loading history...
32
    {
33 47
        $this->processChecks($container);
34 47
    }
35
36 47
    protected function processChecks(ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function processChecks()
Loading history...
37
    {
38 47
        $checkConfigs = $container->getParameter('tvi_monitor.conf.checks');
39
40 47
        $container->setParameter('tvi_monitor.checks.conf', null);
41
42 47
        $checkServiceIds = $container->findTaggedServiceIds(DiTags::CHECK_PLUGIN);
43
44 47
        foreach ($checkServiceIds as $checkServiceId => $null) {
45 39
            $checkDefinitionTpl = $container->getDefinition($checkServiceId);
46
47 39
            $container->removeDefinition($checkServiceId);
48
49 39
            $checkTag = $checkDefinitionTpl->getTag(DiTags::CHECK_PLUGIN);
50 39
            $checkPluginAlias = $checkTag[0]['alias'];
51
52 39
            $checkConfig = $checkConfigs[$checkPluginAlias];
53
54
55 39
            if (isset($checkConfig['_singl'])) {
56
57 37
                $this->addCheckPlugin($container, $checkDefinitionTpl, $checkConfig['_singl'], $checkPluginAlias);
58
            }
59
60 39
            if (isset($checkConfig['_multi'])) {
61 37
                foreach ($checkConfig['_multi'] as $pref => $conf) {
62 39
                    $this->addCheckPlugin($container, $checkDefinitionTpl, $conf, $checkPluginAlias, $pref);
63
                }
64
            }
65
        }
66
67 47
        $registryDefinition = $container->getDefinition('tvi_monitor.checks.manager');
68
69 47
        $tagConfs = $container->getParameter('tvi_monitor.conf.tags');
70 47
        $container->setParameter('tvi_monitor.tags', null);
71
72 47
        $groupConfs = $container->getParameter('tvi_monitor.conf.groups');
73 47
        $container->setParameter('tvi_monitor.conf.groups', null);
74
75 47
        $registryDefinition->addMethodCall('add', [$this->checkServiceConfs, $tagConfs, $groupConfs]);
76 47
    }
77
78 39
    protected function addCheckPlugin(ContainerBuilder $container, Definition $checkPluginDefinitionTpl, array $conf, string $checkPluginAlias, string $checkPluginPref = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addCheckPlugin()
Loading history...
79
    {
80 39
        $checkPluginDefinition = clone $checkPluginDefinitionTpl;
81
82 39
        if (null !== $checkPluginPref) {
83 37
            $checkPluginAlias .= '.'.$checkPluginPref;
84
        }
85
86 39
        if(isset($conf['importance'])) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
87 1
            $conf['tags'][] = $conf['importance'];
88
        }
89
90 39
        foreach ($checkPluginDefinition->getArguments() as $argumentIndex => $argument) {
91 30
            $argument = str_replace('%%', '', $argument);
92
93 30
            if (array_key_exists($argument, $conf['check'])) {
94 30
                $checkPluginDefinition->replaceArgument($argumentIndex, $conf['check'][$argument]);
95
            }
96
        }
97
98 39
        $methodCalls = $checkPluginDefinition->getMethodCalls();
99
100 39
        foreach ($methodCalls as &$methodCall) {
101 39
            if ('setAdditionParams' === $methodCall[0]) {
102 39
                $conf['id'] = $checkPluginAlias;
103 39
                $methodCall[1][0] = $conf;
104
            }
105
        }
106
107
108 39
        $checkPluginDefinition->setMethodCalls($methodCalls);
109
110 39
        $checkServiceId = sprintf(self::SERVICE_ID_FORMAT, sha1($checkPluginAlias));
111
112 39
        $container->setDefinition($checkServiceId, $checkPluginDefinition);
113
114 39
        $this->checkServiceConfs[$checkPluginAlias] = ['serviceId' => $checkServiceId, 'group' => $conf['group'], 'tags' => $conf['tags']];
115 39
    }
116
}
117