Completed
Pull Request — master (#255)
by Evgenij
17:18
created

configureDoctrineMigrationsCheck()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6.9683

Importance

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