Test Failed
Push — master ( f50264...02e3d1 )
by Vladimir
05:15
created

TviMonitorExtension::configurePluginFinder()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.9332
cc 4
nc 6
nop 2
crap 4
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;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @author Vladimir Turnaev <[email protected]>
22
 */
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...
23
class TviMonitorExtension extends Extension implements CompilerPassInterface
24
{
25
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
     * @var Configuration
27
     */
28
    private $configuration;
0 ignored issues
show
Coding Style introduced by
Private member variable "configuration" must be prefixed with an underscore
Loading history...
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $configs should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $container should have a doc-comment as per coding-style.
Loading history...
31
     * Loads the services based on your application configuration.
32
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
33 57
    public function load(array $configs, ContainerBuilder $container)
34
    {
35
        /*
36
        dump($configs); exit;
37
        //*/
38
39 57
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
40
41 57
        $loader->load('service.yml');
42 57
        $loader->load('command.yml');
43
44 57
45 57
        $this->configurePluginFinder($configs, $container);
46 9
47 48
        $pluginFinder = $container->get('tvi_monitor.checks.plugin_finder');
48 1
        $configuration = new Configuration($pluginFinder);
49
50
        $config = $this->processConfiguration($configuration, $configs);
51 57
52 57
        /*
53
        dump($config);
54 57
        exit;
55
        //*/
56 57
57
        $this->configureUIViewTemplate($config, $container);
58 57
        $this->configureTags($config, $container);
59
        $this->configureGroups($config, $container);
60
        $this->configureReporters($config, $container, $loader);
61
62
        $this->configureChecks($config, $container, $loader, $configuration->getCheckPlugins());
63
64
        $loader->load('controller.yml');
65 51
        $loader->load('generator.yml');
66 51
67 51
        $this->configuration = $configuration;
68 51
    }
69
70 51
    public function getConfiguration(array $config, ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getConfiguration()
Loading history...
71
    {
72 51
        return $this->configuration;
73 51
    }
74
75 51
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $container should have a doc-comment as per coding-style.
Loading history...
76 51
     * {@inheritdoc}
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78 1
    public function process(ContainerBuilder $container)
79
    {
80 1
    }
81
82
    private function configurePluginFinder(array $configs, ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configurePluginFinder()
Loading history...
Coding Style introduced by
Private method name "TviMonitorExtension::configurePluginFinder" must be prefixed with an underscore
Loading history...
83
    {
84
        $checksSearchPaths = [];
85
        if (isset($configs[1]['checks_search_paths'])) {
86 50
            $checksSearchPaths = $configs[1]['checks_search_paths'] ?? [];
87
        } elseif (isset($configs[0]['checks_search_paths'])) {
88 50
            $checksSearchPaths = $configs[0]['checks_search_paths'] ?? [];
89
        }
90 51
91
        $checkPluginFinderDefinition = $container->getDefinition('tvi_monitor.checks.plugin_finder');
92 51
        $checkPluginFinderDefinition->setArguments([$checksSearchPaths]);
93 51
94
        foreach ($container->findTaggedServiceIds(DiTags::CHECK_PLUGIN_SEARCH_PATH) as $id => $null) {
95 51
            $service = new \Symfony\Component\DependencyInjection\Reference($id);
96
            $checkPluginFinderDefinition->addMethodCall('addCheckPluginFinderPath', [$service]);
97 51
        }
98 51
    }
99
100 51
    private function configureUIViewTemplate(array $config, ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configureUIViewTemplate()
Loading history...
Coding Style introduced by
Private method name "TviMonitorExtension::configureUIViewTemplate" must be prefixed with an underscore
Loading history...
101
    {
102 51
        $container->setParameter(sprintf('%s.ui.view.template', $this->getAlias()), $config['ui_view_template']);
103 51
    }
104
105
    private function configureTags(array $config, ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configureTags()
Loading history...
Coding Style introduced by
Private method name "TviMonitorExtension::configureTags" must be prefixed with an underscore
Loading history...
106
    {
107
        $container->setParameter(sprintf('%s.conf.tags', $this->getAlias()), $config['tags']);
108
    }
109
110 51
    private function configureGroups(array $config, ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configureGroups()
Loading history...
Coding Style introduced by
Private method name "TviMonitorExtension::configureGroups" must be prefixed with an underscore
Loading history...
111
    {
112 51
        $container->setParameter(sprintf('%s.conf.groups', $this->getAlias()), $config['groups']);
113
    }
114 51
115 42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $container should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $loader should have a doc-comment as per coding-style.
Loading history...
116 42
     * @param string[] $checkPlugins
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $checkPlugins does not match actual variable name $config
Loading history...
117 42
     *
118
     * @throws \Exception
119 42
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
120 42
    private function configureChecks(array $config, ContainerBuilder $container, YamlFileLoader $loader, array $checkPlugins)
0 ignored issues
show
Coding Style introduced by
Private method name "TviMonitorExtension::configureChecks" must be prefixed with an underscore
Loading history...
121
    {
122 42
        $containerParams = [];
123 42
124 42
        if (isset($config['checks'])) {
125
            $config['checks'] = array_filter($config['checks'], static function ($i) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
126 42
                return $i;
127
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
128 42
129 42
            $containerParams = [];
130
            $checksLoaded = [];
131 42
132 42
            foreach ($config['checks'] as $checkName => &$checkSettings) {
133
                $checkPlugin = $checkPlugins[$checkName];
134
                $service = $checkPlugin['service'];
135 42
136 40
                $checkServicePath = $checkPlugin['checkServicePath'];
137
138 40
                if (!\in_array($checkServicePath, $checksLoaded, true)) {
139 40
                    $checksLoaded[] = $checkServicePath;
140
141 40
                    $loader->load($checkServicePath);
142 1
                    $checkPlugin['pligin']->checkRequirements($checkSettings);
143 1
                }
144 1
145
                if (isset($checkSettings['items'])) {
146
                    $items = $checkSettings['items'];
147 40
148
                    foreach ($items as $itemName => &$item) {
149
                        $item['tags'] = array_unique(array_merge($item['tags'], $checkSettings['tags']));
150
151
                        if (null === $item['label'] && null !== $checkSettings['label']) {
152
                            $val = $checkSettings['label'];
153 40
                            $val = sprintf($val, $itemName);
154 2
                            $item['label'] = $val;
155 2
                        }
156
157
                        if (null === $item['descr'] && null !== $checkSettings['descr']) {
158 40
                            $val = $checkSettings['descr'];
159 1
                            $val = sprintf($val, $itemName);
160 1
                            $item['descr'] = $val;
161
                        }
162
163 40
                        if (empty($item['group']) && !empty($checkSettings['group'])) {
164 38
                            $val = $checkSettings['group'];
165
                            $item['group'] = $val;
166
                        }
167 40
168
                        if (empty($item['importance']) && !empty($checkSettings['importance'])) {
169
                            $val = $checkSettings['importance'];
170 40
                            $item['importance'] = $val;
171
                        }
172 38
173 37
                        if (empty($item['group']) && empty($checkSettings['group'])) {
174
                            $item['group'] = $item['_group'];
175
                        }
176 38
177 42
                        unset($item['_group']);
178
                    }
179
180
                    $containerParams[$service]['_multi'] = $items;
181
                } else {
182 51
                    if (empty($checkSettings['group'])) {
183 51
                        $checkSettings['group'] = $checkSettings['_group'];
184
                    }
185
186
                    unset($checkSettings['_group']);
187
                    $containerParams[$service]['_singl'] = $checkSettings;
188 51
                }
189
            }
190 51
        }
191
192 51
        $container->setParameter(sprintf('%s.conf.checks', $this->getAlias()), $containerParams);
193 2
    }
194
195 2
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $container should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $loader should have a doc-comment as per coding-style.
Loading history...
196 2
     * @throws \Exception
197
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
198
    private function configureReporters(array $config, ContainerBuilder $container, YamlFileLoader $loader)
0 ignored issues
show
Coding Style introduced by
Private method name "TviMonitorExtension::configureReporters" must be prefixed with an underscore
Loading history...
199 51
    {
200
        $loader->load('reporter/reporters.yml');
201
202
        if (isset($config['reporters']['mailer'])) {
203
            $loader->load('reporter/mailer.yml');
204
205
            foreach ($config['reporters']['mailer'] as $key => $value) {
206
                $container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value);
207
            }
208
        }
209
    }
210
}
211