Completed
Pull Request — master (#115)
by Sascha
02:17
created

LiipMonitorExtension::load()   C

Complexity

Conditions 11
Paths 32

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 20
Bugs 3 Features 11
Metric Value
dl 0
loc 57
rs 6.4824
c 20
b 3
f 11
cc 11
eloc 33
nc 32
nop 2

How to fix   Long Method    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
        $loader->load('command.xml');
24
25
        $configuration = new Configuration();
26
        $config = $this->processConfiguration($configuration, $configs);
27
28
        if (null === $config['view_template']) {
29
            $config['view_template'] = __DIR__.'/../Resources/views/health/index.html.php';
30
        }
31
32
        if ($config['enable_controller']) {
33
            $container->setParameter(sprintf('%s.view_template', $this->getAlias()), $config['view_template']);
34
            $loader->load('controller.xml');
35
        }
36
37
        if ($config['mailer']['enabled']) {
38
            $loader->load('helper/swift_mailer.xml');
39
40
            foreach ($config['mailer'] as $key => $value) {
41
                $container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value);
42
            }
43
        }
44
45
        $container->setParameter(sprintf('%s.default_group', $this->getAlias()), $config['default_group']);
46
47
        if (empty($config['checks'])) {
48
            return;
49
        }
50
51
        $checksLoaded = array();
52
        $containerParams = array();
53
        foreach ($config['checks']['groups'] as $group => $checks) {
54
            if (empty($checks)) {
55
                continue;
56
            }
57
58
            foreach ($checks as $check => $values) {
59
                if (empty($values)) {
60
                    continue;
61
                }
62
63
                $containerParams['groups'][$group][$check] = $values;
64
                $this->setParameters($container, $check, $group, $values);
65
66
                if (!in_array($check, $checksLoaded)) {
67
                    $loader->load('checks/'.$check.'.xml');
68
                    $checksLoaded[] = $check;
69
                }
70
            }
71
        }
72
73
        $container->setParameter(sprintf('%s.checks', $this->getAlias()), $containerParams);
74
    }
75
76
    /**
77
     * @param ContainerBuilder $container
78
     * @param string           $checkName
79
     * @param string           $group
80
     * @param array            $values
81
     */
82
    private function setParameters(ContainerBuilder $container, $checkName, $group, $values)
83
    {
84
        $prefix = sprintf('%s.check.%s', $this->getAlias(), $checkName);
85
        switch ($checkName) {
86
            case 'class_exists':
87
            case 'cpu_performance':
88
            case 'php_extensions':
89
            case 'php_version':
90
            case 'php_flags':
91
            case 'readable_directory':
92
            case 'writable_directory':
93
            case 'process_running':
94
            case 'doctrine_dbal':
95
            case 'http_service':
96
            case 'guzzle_http_service':
97
            case 'memcache':
98
            case 'redis':
99
            case 'rabbit_mq':
100
            case 'stream_wrapper_exists':
101
            case 'file_ini':
102
            case 'file_json':
103
            case 'file_xml':
104
            case 'file_yaml':
105
            case 'expressions':
106
                $container->setParameter($prefix.'.'.$group, $values);
107
                continue;
108
109
            case 'symfony_version':
110
                continue;
111
112
            case 'opcache_memory':
113
                if (!class_exists('ZendDiagnostics\Check\OpCacheMemory')) {
114
                    throw new \InvalidArgumentException('Please require at least "v1.0.4" of "ZendDiagnostics"');
115
                }
116
        }
117
118
        if (is_array($values)) {
119
            foreach ($values as $key => $value) {
120
                $container->setParameter($prefix.'.'.$key.'.'.$group, $value);
121
            }
122
        }
123
    }
124
}
125