Completed
Push — master ( c2b032...2a92e2 )
by Kevin
07:57 queued 06:47
created

LiipMonitorExtension   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 82.52%

Importance

Changes 0
Metric Value
wmc 51
lcom 1
cbo 6
dl 0
loc 187
ccs 85
cts 103
cp 0.8252
rs 7.92
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 4 1
A __construct() 0 4 1
C load() 0 65 11
D setParameters() 0 49 30
A configureDoctrineMigrationsCheck() 0 21 5
A configureMailer() 0 10 3

How to fix   Complexity   

Complex Class

Complex classes like LiipMonitorExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LiipMonitorExtension, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Liip\MonitorBundle\DependencyInjection;
4
5
use Doctrine\Migrations\Configuration\Configuration as DoctrineMigrationConfiguration;
6
use Liip\MonitorBundle\DependencyInjection\DoctrineMigrations\DoctrineMigrationsLoader;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Loader;
11
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12
13
class LiipMonitorExtension extends Extension implements CompilerPassInterface
14
{
15
    /**
16
     * Loader for doctrine migrations to support both v2 and v3 major versions.
17
     *
18
     * @var DoctrineMigrationsLoader
19
     */
20
    private $migrationsLoader;
21
22
    /**
23
     * LiipMonitorExtension constructor.
24
     */
25 49
    public function __construct()
26
    {
27 49
        $this->migrationsLoader = new DoctrineMigrationsLoader();
28 49
    }
29
30
    /**
31
     * Loads the services based on your application configuration.
32
     */
33 49
    public function load(array $configs, ContainerBuilder $container)
34
    {
35 49
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
36 49
        $loader->load('runner.xml');
37 49
        $loader->load('helper.xml');
38 49
        $loader->load('commands.xml');
39
40 49
        $configuration = $this->getConfiguration($configs, $container);
41 49
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Documentation introduced by
$configuration is of type null|object, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
43 44
        if (null === $config['view_template']) {
44 44
            $config['view_template'] = __DIR__.'/../Resources/views/health/index.html.php';
45
        }
46
47 44
        if ($config['enable_controller']) {
48 2
            $container->setParameter(sprintf('%s.view_template', $this->getAlias()), $config['view_template']);
49 2
            $container->setParameter(sprintf('%s.failure_status_code', $this->getAlias()), $config['failure_status_code']);
50 2
            $loader->load('controller.xml');
51
        }
52
53 44
        $this->configureMailer($container, $config);
54
55 44
        $container->setParameter(sprintf('%s.default_group', $this->getAlias()), $config['default_group']);
56
57
        // symfony3 does not define templating.helper.assets unless php templating is included
58 44
        if ($container->has('templating.helper.assets')) {
59
            $pathHelper = $container->getDefinition('liip_monitor.helper');
60
            $pathHelper->replaceArgument(0, 'templating.helper.assets');
61
        }
62
63
        // symfony3 does not define templating.helper.router unless php templating is included
64 44
        if ($container->has('templating.helper.router')) {
65
            $pathHelper = $container->getDefinition('liip_monitor.helper');
66
            $pathHelper->replaceArgument(1, 'templating.helper.router');
67
        }
68
69 44
        if (empty($config['checks'])) {
70 7
            return;
71
        }
72
73 37
        $checksLoaded = [];
74 37
        $containerParams = [];
75 37
        foreach ($config['checks']['groups'] as $group => $checks) {
76 37
            if (empty($checks)) {
77
                continue;
78
            }
79
80 37
            foreach ($checks as $check => $values) {
81 37
                if (empty($values)) {
82 37
                    continue;
83
                }
84
85 37
                $containerParams['groups'][$group][$check] = $values;
86 37
                $this->setParameters($container, $check, $group, $values);
87
88 37
                if (!in_array($check, $checksLoaded)) {
89 37
                    $loader->load('checks/'.$check.'.xml');
90 37
                    $checksLoaded[] = $check;
91
                }
92
            }
93
        }
94
95 37
        $container->setParameter(sprintf('%s.checks', $this->getAlias()), $containerParams);
96 37
        $this->configureDoctrineMigrationsCheck($container, $containerParams);
97 37
    }
98
99 40
    public function process(ContainerBuilder $container)
100
    {
101 40
        $this->migrationsLoader->process($container);
102 40
    }
103
104
    /**
105
     * @param string $checkName
106
     * @param string $group
107
     * @param array  $values
108
     */
109 37
    private function setParameters(ContainerBuilder $container, $checkName, $group, $values)
110
    {
111 37
        $prefix = sprintf('%s.check.%s', $this->getAlias(), $checkName);
112 37
        switch ($checkName) {
113 37
            case 'class_exists':
114 36
            case 'cpu_performance':
115 35
            case 'php_extensions':
116 32
            case 'php_version':
117 31
            case 'php_flags':
118 30
            case 'readable_directory':
119 29
            case 'writable_directory':
120 28
            case 'process_running':
121 24
            case 'doctrine_dbal':
122 20
            case 'doctrine_mongodb':
123 20
            case 'http_service':
124 19
            case 'guzzle_http_service':
125 18
            case 'memcache':
126 17
            case 'memcached':
127 17
            case 'redis':
128 16
            case 'rabbit_mq':
129 15
            case 'stream_wrapper_exists':
130 14
            case 'file_ini':
131 13
            case 'file_json':
132 12
            case 'file_xml':
133 11
            case 'file_yaml':
134 10
            case 'expressions':
135 9
            case 'pdo_connections':
136 29
                $container->setParameter($prefix.'.'.$group, $values);
137 29
                break;
138
139 8
            case 'symfony_version':
140 7
            case 'opcache_memory':
141 2
                break;
142
143 6
            case 'doctrine_migrations':
144
                if (!class_exists(DoctrineMigrationConfiguration::class)) {
145
                    throw new \InvalidArgumentException('Please require at least "v2.0.0" of "Doctrine Migrations Library"');
146
                }
147
148
                $container->setParameter($prefix.'.'.$group, $values);
149
                break;
150
        }
151
152 37
        if (is_array($values)) {
153 33
            foreach ($values as $key => $value) {
154 33
                $container->setParameter($prefix.'.'.$key.'.'.$group, $value);
155
            }
156
        }
157 37
    }
158
159
    /**
160
     * Set up doctrine migration configuration services.
161
     *
162
     * @param ContainerBuilder $container The container
163
     * @param array            $params    Container params
164
     *
165
     * @return void
166
     */
167 37
    private function configureDoctrineMigrationsCheck(ContainerBuilder $container, array $params)
168
    {
169 37
        if (!$container->hasDefinition('liip_monitor.check.doctrine_migrations') || !isset($params['groups'])) {
170 37
            return;
171
        }
172
173
        foreach ($params['groups'] as $groupName => $groupChecks) {
174
            if (!isset($groupChecks['doctrine_migrations'])) {
175
                continue;
176
            }
177
178
            $services = $this->migrationsLoader->loadMigrationChecks(
179
                $container,
180
                $groupChecks['doctrine_migrations'],
181
                $groupName
182
            );
183
184
            $parameter = sprintf('%s.check.%s.%s', $this->getAlias(), 'doctrine_migrations', $groupName);
185
            $container->setParameter($parameter, $services);
186
        }
187
    }
188
189 44
    private function configureMailer(ContainerBuilder $container, array $config)
190
    {
191 44
        if (false === $config['mailer']['enabled']) {
192 43
            return;
193
        }
194
195 1
        foreach ($config['mailer'] as $key => $value) {
196 1
            $container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value);
197
        }
198 1
    }
199
}
200