Passed
Push — master ( 0ec4b3...bff059 )
by Vladimir
05:55
created

TviMonitorExtension::configureUIViewTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
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...
26
     * Loads the services based on your application configuration.
27
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
28 54
    public function load(array $configs, ContainerBuilder $container)
29
    {
30
        /*
31
        dump($configs); exit;
32
        //*/
33
34 54
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
35
36 54
        $loader->load('service.yml');
37 54
        $loader->load('command.yml');
38
39 54
        $checksSearchPaths = [];
40 54
        if (isset($configs[1]['checks_search_paths'])) {
41 9
            $checksSearchPaths = $configs[1]['checks_search_paths'] ?? [];
42 45
        } elseif (isset($configs[0]['checks_search_paths'])) {
43 1
            $checksSearchPaths = $configs[0]['checks_search_paths'] ?? [];
44
        }
45
46 54
        $checkPluginFinderDefinition = $container->getDefinition('tvi_monitor.checks.plugin_finder');
47 54
        $checkPluginFinderDefinition->setArguments([$checksSearchPaths]);
48
49 54
        $pluginFinder = $container->get('tvi_monitor.checks.plugin_finder');
50
51 54
        $configuration = new Configuration($pluginFinder);
52
53 54
        $config = $this->processConfiguration($configuration, $configs);
54
55
        /*
56
        dump($config); exit;
57
        //*/
58
59 48
        $this->configureUIViewTemplate($config, $container);
60 48
        $this->configureTags($config, $container);
61 48
        $this->configureGroups($config, $container);
62 48
        $this->configureReporters($config, $container, $loader);
63
64 48
        $this->configureChecks($config, $container, $loader, $configuration->getCheckPlugins());
65
66 48
        $loader->load('controller.yml');
67 48
        $loader->load('generator.yml');
68 48
    }
69
70
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $container should have a doc-comment as per coding-style.
Loading history...
71
     * {@inheritdoc}
72
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
73 47
    public function process(ContainerBuilder $container)
74
    {
75 47
    }
76
77 48
    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...
78
    {
79 48
        $container->setParameter(sprintf('%s.ui.view.template', $this->getAlias()), $config['ui_view_template']);
80 48
    }
81
82 48
    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...
83
    {
84 48
        $container->setParameter(sprintf('%s.conf.tags', $this->getAlias()), $config['tags']);
85 48
    }
86
87 48
    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...
88
    {
89 48
        $container->setParameter(sprintf('%s.conf.groups', $this->getAlias()), $config['groups']);
90 48
    }
91
92
    /**
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...
93
     * @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...
94
     *
95
     * @throws \Exception
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97 48
    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...
98
    {
99 48
        $containerParams = [];
100
101 48
        if (isset($config['checks'])) {
102 39
            $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...
103 39
                return $i;
104 39
            });
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...
105
106 39
            $containerParams = [];
107 39
            $checksLoaded = [];
108
109 39
            foreach ($config['checks'] as $checkName => &$checkSettings) {
110 39
                $checkPlugin = $checkPlugins[$checkName];
111 39
                $service = $checkPlugin['service'];
112
113 39
                $checkServicePath = $checkPlugin['checkServicePath'];
114
115 39
                if (!\in_array($checkServicePath, $checksLoaded, true)) {
116 39
                    $checksLoaded[] = $checkServicePath;
117
118 39
                    $loader->load($checkServicePath);
119 39
                    $checkPlugin['pligin']->checkRequirements($checkSettings);
120
                }
121
122 39
                if (isset($checkSettings['items'])) {
123 37
                    $items = $checkSettings['items'];
124
125 37
                    foreach ($items as $itemName => &$item) {
126 37
                        $item['tags'] = array_unique(array_merge($item['tags'], $checkSettings['tags']));
127
128 37
                        if (null === $item['label'] && null !== $checkSettings['label']) {
129 1
                            $val = $checkSettings['label'];
130 1
                            $val = sprintf($val, $itemName);
131 1
                            $item['label'] = $val;
132
                        }
133
134 37
                        if (null === $item['descr'] && null !== $checkSettings['descr']) {
135
                            $val = $checkSettings['descr'];
136
                            $val = sprintf($val, $itemName);
137
                            $item['descr'] = $val;
138
                        }
139
140 37
                        if (empty($item['group']) && !empty($checkSettings['group'])) {
141 2
                            $val = $checkSettings['group'];
142 2
                            $item['group'] = $val;
143
                        }
144
145 37
                        if (empty($item['importance']) && !empty($checkSettings['importance'])) {
146 1
                            $val = $checkSettings['importance'];
147 1
                            $item['importance'] = $val;
148
                        }
149
150 37
                        if (empty($item['group']) && empty($checkSettings['group'])) {
151 35
                            $item['group'] = $item['_group'];
152
                        }
153
154 37
                        unset($item['_group']);
155
                    }
156
157 37
                    $containerParams[$service]['_multi'] = $items;
158
                } else {
159 37
                    if (empty($checkSettings['group'])) {
160 36
                        $checkSettings['group'] = $checkSettings['_group'];
161
                    }
162
163 37
                    unset($checkSettings['_group']);
164 39
                    $containerParams[$service]['_singl'] = $checkSettings;
165
                }
166
            }
167
        }
168
169 48
        $container->setParameter(sprintf('%s.conf.checks', $this->getAlias()), $containerParams);
170 48
    }
171
172
    /**
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...
173
     * @throws \Exception
174
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
175 48
    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...
176
    {
177 48
        $loader->load('reporter/reporters.yml');
178
179 48
        if (isset($config['reporters']['mailer'])) {
180 2
            $loader->load('reporter/mailer.yml');
181
182 2
            foreach ($config['reporters']['mailer'] as $key => $value) {
183 2
                $container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value);
184
            }
185
        }
186 48
    }
187
}
188