Completed
Push — master ( d1f2d1...295638 )
by Vladimir
05:42
created

AddCheckPluginsCompilerPass   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 37
dl 0
loc 79
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

3 Methods

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