AddCheckPluginsCompilerPass::addCheckPlugin()   B
last analyzed

Complexity

Conditions 7
Paths 36

Size

Total Lines 36
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 36
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
    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 50
    public function process(ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function process()
Loading history...
32
    {
33 50
        $this->processChecks($container);
34 50
    }
35
36 50
    protected function processChecks(ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function processChecks()
Loading history...
37
    {
38 50
        $checkConfigs = $container->getParameter('tvi_monitor.conf.checks');
39
40 50
        $container->setParameter('tvi_monitor.checks.conf', null);
41
42 50
        $checkServiceIds = $container->findTaggedServiceIds(DiTags::CHECK_PLUGIN);
43
44 50
        foreach ($checkServiceIds as $checkServiceId => $null) {
45 42
            $checkDefinitionTpl = $container->getDefinition($checkServiceId);
46
47 42
            $container->removeDefinition($checkServiceId);
48
49 42
            $checkTag = $checkDefinitionTpl->getTag(DiTags::CHECK_PLUGIN);
50 42
            $checkPluginAlias = $checkTag[0]['alias'];
51
52 42
            $checkConfig = $checkConfigs[$checkPluginAlias];
53
54
55 42
            if (isset($checkConfig['_singl'])) {
56 38
                $this->addCheckPlugin($container, $checkDefinitionTpl, $checkConfig['_singl'], $checkPluginAlias);
57
            }
58
59 42
            if (isset($checkConfig['_multi'])) {
60 40
                foreach ($checkConfig['_multi'] as $pref => $conf) {
61 42
                    $this->addCheckPlugin($container, $checkDefinitionTpl, $conf, $checkPluginAlias, $pref);
62
                }
63
            }
64
        }
65
66 50
        $registryDefinition = $container->getDefinition('tvi_monitor.checks.manager');
67
68 50
        $tagConfs = $container->getParameter('tvi_monitor.conf.tags');
69 50
        $container->setParameter('tvi_monitor.tags', null);
70
71 50
        $groupConfs = $container->getParameter('tvi_monitor.conf.groups');
72 50
        $container->setParameter('tvi_monitor.conf.groups', null);
73
74 50
        $registryDefinition->addMethodCall('add', [$this->checkServiceConfs, $tagConfs, $groupConfs]);
75 50
    }
76
77 42
    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...
78
    {
79 42
        $checkPluginDefinition = clone $checkPluginDefinitionTpl;
80
81 42
        if (null !== $checkPluginPref) {
82 40
            $checkPluginAlias .= '.'.$checkPluginPref;
83
        }
84
85 42
        if (isset($conf['importance'])) {
86 1
            $conf['tags'][] = $conf['importance'];
87
        }
88
89 42
        foreach ($checkPluginDefinition->getArguments() as $argumentIndex => $argument) {
90 33
            $argument = str_replace('%%', '', $argument);
91
92 33
            if (array_key_exists($argument, $conf['check'])) {
93 33
                $checkPluginDefinition->replaceArgument($argumentIndex, $conf['check'][$argument]);
94
            }
95
        }
96
97 42
        $methodCalls = $checkPluginDefinition->getMethodCalls();
98
99 42
        foreach ($methodCalls as &$methodCall) {
100 42
            if ('setAdditionParams' === $methodCall[0]) {
101 42
                $conf['id'] = $checkPluginAlias;
102 42
                $methodCall[1][0] = $conf;
103
            }
104
        }
105
106 42
        $checkPluginDefinition->setMethodCalls($methodCalls);
107
108 42
        $checkServiceId = sprintf(self::SERVICE_ID_FORMAT, sha1($checkPluginAlias));
109
110 42
        $container->setDefinition($checkServiceId, $checkPluginDefinition);
111
112 42
        $this->checkServiceConfs[$checkPluginAlias] = ['serviceId' => $checkServiceId, 'group' => $conf['group'], 'tags' => $conf['tags']];
113 42
    }
114
}
115