Completed
Pull Request — master (#123)
by Sascha
04:41
created

LiipMonitorExtension::setParameters()   D

Complexity

Conditions 26
Paths 70

Size

Total Lines 42
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 4.7029
cc 26
eloc 33
nc 70
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Liip\MonitorBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader;
9
10
class LiipMonitorExtension extends Extension
11
{
12
    /**
13
     * Loads the services based on your application configuration.
14
     *
15
     * @param array            $configs
16
     * @param ContainerBuilder $container
17
     */
18
    public function load(array $configs, ContainerBuilder $container)
19
    {
20
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
21
        $loader->load('runner.xml');
22
        $loader->load('helper.xml');
23
24
        $configuration = new Configuration();
25
        $config = $this->processConfiguration($configuration, $configs);
26
27
        if (null === $config['view_template']) {
28
            $config['view_template'] = __DIR__.'/../Resources/views/health/index.html.php';
29
        }
30
31
        if ($config['enable_controller']) {
32
            $container->setParameter(sprintf('%s.view_template', $this->getAlias()), $config['view_template']);
33
            $loader->load('controller.xml');
34
        }
35
36
        if ($config['mailer']['enabled']) {
37
            $loader->load('helper/swift_mailer.xml');
38
39
            foreach ($config['mailer'] as $key => $value) {
40
                $container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value);
41
            }
42
        }
43
44
        $container->setParameter(sprintf('%s.default_group', $this->getAlias()), $config['default_group']);
45
46
        if (empty($config['checks'])) {
47
            return;
48
        }
49
50
        $checksLoaded = array();
51
        $containerParams = array();
52
        foreach ($config['checks']['groups'] as $group => $checks) {
53
            if (empty($checks)) {
54
                continue;
55
            }
56
57
            foreach ($checks as $check => $values) {
58
                if (empty($values)) {
59
                    continue;
60
                }
61
62
                $containerParams['groups'][$group][$check] = $values;
63
                $this->setParameters($container, $check, $group, $values);
64
65
                if (!in_array($check, $checksLoaded)) {
66
                    $loader->load('checks/'.$check.'.xml');
67
                    $checksLoaded[] = $check;
68
                }
69
            }
70
        }
71
72
        $container->setParameter(sprintf('%s.checks', $this->getAlias()), $containerParams);
73
    }
74
75
    /**
76
     * @param ContainerBuilder $container
77
     * @param string           $checkName
78
     * @param string           $group
79
     * @param array            $values
80
     */
81
    private function setParameters(ContainerBuilder $container, $checkName, $group, $values)
82
    {
83
        $prefix = sprintf('%s.check.%s', $this->getAlias(), $checkName);
84
        switch ($checkName) {
85
            case 'class_exists':
86
            case 'cpu_performance':
87
            case 'php_extensions':
88
            case 'php_version':
89
            case 'php_flags':
90
            case 'readable_directory':
91
            case 'writable_directory':
92
            case 'process_running':
93
            case 'doctrine_dbal':
94
            case 'http_service':
95
            case 'guzzle_http_service':
96
            case 'memcache':
97
            case 'redis':
98
            case 'rabbit_mq':
99
            case 'stream_wrapper_exists':
100
            case 'file_ini':
101
            case 'file_json':
102
            case 'file_xml':
103
            case 'file_yaml':
104
            case 'expressions':
105
                $container->setParameter($prefix.'.'.$group, $values);
106
                continue;
107
108
            case 'symfony_version':
109
                continue;
110
111
            case 'opcache_memory':
112
                if (!class_exists('ZendDiagnostics\Check\OpCacheMemory')) {
113
                    throw new \InvalidArgumentException('Please require at least "v1.0.4" of "ZendDiagnostics"');
114
                }
115
        }
116
117
        if (is_array($values)) {
118
            foreach ($values as $key => $value) {
119
                $container->setParameter($prefix.'.'.$key.'.'.$group, $value);
120
            }
121
        }
122
    }
123
}
124