Test Setup Failed
Push — master ( 42e323...580240 )
by Dominik
03:08
created

DoctrineDbalServiceProvider   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 184
Duplicated Lines 8.15 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
lcom 1
cbo 5
dl 15
loc 184
ccs 76
cts 76
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 11 1
A getDbDefaultOptions() 0 10 1
B getDbsDefinition() 0 24 3
B getDbsOptionsInitializerDefinition() 0 29 5
A getDbsConfigDefinition() 0 22 3
A getDbsEventManagerDefinition() 15 15 2
A getDbDefinition() 0 8 1
A getDbConfigDefinition() 0 8 1
A getDbEventManagerDefinition() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Fabien Potencier <[email protected]> (https://github.com/silexphp/Silex-Providers)
7
 */
8
9
namespace Chubbyphp\ServiceProvider;
10
11
use Chubbyphp\ServiceProvider\Logger\DoctrineDbalLogger;
12
use Pimple\Container;
13
use Pimple\ServiceProviderInterface;
14
use Doctrine\DBAL\DriverManager;
15
use Doctrine\DBAL\Configuration;
16
use Doctrine\Common\EventManager;
17
18
final class DoctrineDbalServiceProvider implements ServiceProviderInterface
19
{
20
    /**
21
     * @param Container $container
22
     */
23 3
    public function register(Container $container)
24
    {
25 3
        $container['doctrine.dbal.db.default_options'] = $this->getDbDefaultOptions();
26 3
        $container['doctrine.dbal.dbs.options.initializer'] = $this->getDbsOptionsInitializerDefinition($container);
27 3
        $container['doctrine.dbal.dbs'] = $this->getDbsDefinition($container);
28 3
        $container['doctrine.dbal.dbs.config'] = $this->getDbsConfigDefinition($container);
29 3
        $container['doctrine.dbal.dbs.event_manager'] = $this->getDbsEventManagerDefinition($container);
30 3
        $container['doctrine.dbal.db'] = $this->getDbDefinition($container);
31 3
        $container['doctrine.dbal.db.config'] = $this->getDbConfigDefinition($container);
32 3
        $container['doctrine.dbal.db.event_manager'] = $this->getDbEventManagerDefinition($container);
33 3
    }
34
35
    /**
36
     * @return array
37
     */
38 3
    private function getDbDefaultOptions(): array
39
    {
40
        return [
41 3
            'driver' => 'pdo_mysql',
42
            'dbname' => null,
43
            'host' => 'localhost',
44
            'user' => 'root',
45
            'password' => null,
46
        ];
47
    }
48
49
    /**
50
     * @param Container $container
51
     *
52
     * @return callable
53
     */
54
    private function getDbsOptionsInitializerDefinition(Container $container): callable
55
    {
56 3
        return $container->protect(function () use ($container) {
57 3
            static $initialized = false;
58
59 3
            if ($initialized) {
60 3
                return;
61
            }
62
63 3
            $initialized = true;
64
65 3
            if (!isset($container['doctrine.dbal.dbs.options'])) {
66 2
                $container['doctrine.dbal.dbs.options'] = [
67 2
                    'default' => $container['doctrine.dbal.db.options'] ?? [],
68
                ];
69
            }
70
71 3
            $tmp = $container['doctrine.dbal.dbs.options'];
72 3
            foreach ($tmp as $name => &$options) {
73 3
                $options = array_replace($container['doctrine.dbal.db.default_options'], $options);
74
75 3
                if (!isset($container['doctrine.dbal.dbs.default'])) {
76 3
                    $container['doctrine.dbal.dbs.default'] = $name;
77
                }
78
            }
79
80 3
            $container['doctrine.dbal.dbs.options'] = $tmp;
81 3
        });
82
    }
83
84
    /**
85
     * @param Container $container
86
     *
87
     * @return callable
88
     */
89
    private function getDbsDefinition(Container $container): callable
90
    {
91 3
        return function () use ($container) {
92 3
            $container['doctrine.dbal.dbs.options.initializer']();
93
94 3
            $dbs = new Container();
95 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
96 3
                if ($container['doctrine.dbal.dbs.default'] === $name) {
97
                    // we use shortcuts here in case the default has been overridden
98 3
                    $config = $container['doctrine.dbal.db.config'];
99 3
                    $manager = $container['doctrine.dbal.db.event_manager'];
100
                } else {
101 1
                    $config = $container['doctrine.dbal.dbs.config'][$name];
102 1
                    $manager = $container['doctrine.dbal.dbs.event_manager'][$name];
103
                }
104
105 3
                $dbs[$name] = function () use ($options, $config, $manager) {
106 3
                    return DriverManager::getConnection($options, $config, $manager);
107 3
                };
108
            }
109
110 3
            return $dbs;
111 3
        };
112
    }
113
114
    /**
115
     * @param Container $container
116
     *
117
     * @return callable
118
     */
119
    private function getDbsConfigDefinition(Container $container): callable
120
    {
121 3
        return function () use ($container) {
122 3
            $container['doctrine.dbal.dbs.options.initializer']();
123
124 3
            $addLogger = $container['logger'] ?? false;
125 3
126 3
            $configs = new Container();
127
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
128 3
                $configs[$name] = function () use ($addLogger, $container) {
129 3
                    $config = new Configuration();
130 3
                    if ($addLogger) {
131 3
                        $config->setSQLLogger(new DoctrineDbalLogger($container['logger']));
132 3
                    }
133 2
134 2
                    return $config;
135
                };
136
            }
137
138 3
            return $configs;
139 3
        };
140
    }
141
142 3
    /**
143 3
     * @param Container $container
144
     *
145
     * @return callable
146
     */
147 View Code Duplication
    private function getDbsEventManagerDefinition(Container $container): callable
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        return function () use ($container) {
150
            $container['doctrine.dbal.dbs.options.initializer']();
151
152
            $managers = new Container();
153 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
154 3
                $managers[$name] = function () {
155
                    return new EventManager();
156 3
                };
157 3
            }
158 3
159 3
            return $managers;
160 3
        };
161
    }
162
163 3
    /***
164 3
     * @param Container $container
165
     * @return callable
166
     */
167
    private function getDbDefinition(Container $container): callable
168
    {
169
        return function () use ($container) {
170
            $dbs = $container['doctrine.dbal.dbs'];
171
172
            return $dbs[$container['doctrine.dbal.dbs.default']];
173 3
        };
174 2
    }
175
176 2
    /***
177 3
     * @param Container $container
178
     * @return callable
179
     */
180
    private function getDbConfigDefinition(Container $container): callable
181
    {
182
        return function () use ($container) {
183
            $dbs = $container['doctrine.dbal.dbs.config'];
184
185
            return $dbs[$container['doctrine.dbal.dbs.default']];
186 3
        };
187 3
    }
188
189 3
    /***
190 3
     * @param Container $container
191
     * @return callable
192
     */
193
    private function getDbEventManagerDefinition(Container $container): callable
194
    {
195
        return function () use ($container) {
196
            $dbs = $container['doctrine.dbal.dbs.event_manager'];
197
198
            return $dbs[$container['doctrine.dbal.dbs.default']];
199 3
        };
200 3
    }
201
}
202