Test Setup Failed
Push — master ( a11866...358c21 )
by Dominik
02:10
created

getDbsServiceDefinition()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 8.9713
cc 3
eloc 14
nc 1
nop 1
crap 3
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 Pimple\Container;
12
use Pimple\ServiceProviderInterface;
13
use Doctrine\DBAL\DriverManager;
14
use Doctrine\DBAL\Configuration;
15
use Doctrine\Common\EventManager;
16
use Symfony\Bridge\Doctrine\Logger\DbalLogger;
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['db.default_options'] = $this->getDbDefaultOptions();
26
        $container['dbs.options.initializer'] = $this->getDbsOptionsInitializerServiceDefinition($container);
27
        $container['dbs'] = $this->getDbsServiceDefinition($container);
28
        $container['dbs.config'] = $this->getDbsConfigServiceDefinition($container);
29
        $container['dbs.event_manager'] = $this->getDbsEventManagerServiceDefinition($container);
30
        $container['db'] = $this->getDbServiceDefinition($container);
31
        $container['db.config'] = $this->getDbConfigServiceDefinition($container);
32
        $container['db.event_manager'] = $this->getDbEventManagerServiceDefinition($container);
33 3
    }
34 3
35
    /**
36 3
     * @return array
37 3
     */
38
    private function getDbDefaultOptions(): array
39
    {
40 3
        return [
41
            'driver' => 'pdo_mysql',
42 3
            'dbname' => null,
43 2
            'host' => 'localhost',
44 2
            'user' => 'root',
45
            'password' => null,
46
        ];
47
    }
48 3
49 3
    /**
50 3
     * @param Container $container
51
     *
52 3
     * @return \Closure
53 3
     */
54 View Code Duplication
    private function getDbsOptionsInitializerServiceDefinition(Container $container): \Closure
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...
55
    {
56 3
        return $container->protect(function () use ($container) {
57 3
            static $initialized = false;
58
59 3
            if ($initialized) {
60 3
                return;
61
            }
62 3
63 3
            $initialized = true;
64 3
65
            if (!isset($container['dbs.options'])) {
66 3
                $container['dbs.options'] = [
67 3
                    'default' => isset($container['db.options']) ? $container['db.options'] : [],
68
                ];
69 1
            }
70 1
71
            $tmp = $container['dbs.options'];
72
            foreach ($tmp as $name => &$options) {
73 3
                $options = array_replace($container['db.default_options'], $options);
74 3
75 3
                if (!isset($container['dbs.default'])) {
76
                    $container['dbs.default'] = $name;
77
                }
78 3
            }
79
80
            $container['dbs.options'] = $tmp;
81 3
        });
82 3
    }
83
84 3
    /**
85 3
     * @param Container $container
86 3
     *
87
     * @return \Closure
88 3
     */
89 3
    private function getDbsServiceDefinition(Container $container): \Closure
90 3
    {
91 3
        return function () use ($container) {
92 3
            $container['dbs.options.initializer']();
93 2
94 2
            $dbs = new Container();
95
            foreach ($container['dbs.options'] as $name => $options) {
96
                if ($container['dbs.default'] === $name) {
97
                    // we use shortcuts here in case the default has been overridden
98 3
                    $config = $container['db.config'];
99 3
                    $manager = $container['db.event_manager'];
100
                } else {
101
                    $config = $container['dbs.config'][$name];
102 3
                    $manager = $container['dbs.event_manager'][$name];
103
                }
104
105 3
                $dbs[$name] = function () use ($options, $config, $manager) {
106 3
                    return DriverManager::getConnection($options, $config, $manager);
107
                };
108 3
            }
109 3
110 3
            return $dbs;
111 3
        };
112 3
    }
113
114
    /**
115 3
     * @param Container $container
116
     *
117
     * @return \Closure
118
     */
119 2
    private function getDbsConfigServiceDefinition(Container $container): \Closure
120 2
    {
121
        return function () use ($container) {
122 2
            $container['dbs.options.initializer']();
123
124
            $addLogger = isset($container['logger']) && null !== $container['logger']
125 3
                && class_exists('Symfony\Bridge\Doctrine\Logger\DbalLogger');
126 3
            $stopwatch = $container['stopwatch'] ?? null;
127
128 3
            $configs = new Container();
129
            foreach ($container['dbs.options'] as $name => $options) {
130
                $configs[$name] = function () use ($addLogger, $container, $stopwatch) {
131 3
                    $config = new Configuration();
132 3
                    if ($addLogger) {
133
                        $config->setSQLLogger(
134 3
                            new DbalLogger($container['logger'], $stopwatch)
135
                        );
136 3
                    }
137
138
                    return $config;
139
                };
140
            }
141
142
            return $configs;
143
        };
144
    }
145
146
    /**
147
     * @param Container $container
148
     *
149
     * @return \Closure
150
     */
151 View Code Duplication
    private function getDbsEventManagerServiceDefinition(Container $container): \Closure
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...
152
    {
153
        return function () use ($container) {
154
            $container['dbs.options.initializer']();
155
156
            $managers = new Container();
157
            foreach ($container['dbs.options'] as $name => $options) {
158
                $managers[$name] = function () {
159
                    return new EventManager();
160
                };
161
            }
162
163
            return $managers;
164
        };
165
    }
166
167
    /***
168
     * @param Container $container
169
     * @return \Closure
170
     */
171 View Code Duplication
    private function getDbServiceDefinition(Container $container): \Closure
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...
172
    {
173
        return function () use ($container) {
174
            $dbs = $container['dbs'];
175
176
            return $dbs[$container['dbs.default']];
177
        };
178
    }
179
180
    /***
181
     * @param Container $container
182
     * @return \Closure
183
     */
184 View Code Duplication
    private function getDbConfigServiceDefinition(Container $container): \Closure
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...
185
    {
186
        return function () use ($container) {
187
            $dbs = $container['dbs.config'];
188
189
            return $dbs[$container['dbs.default']];
190
        };
191
    }
192
193
    /***
194
     * @param Container $container
195
     * @return \Closure
196
     */
197 View Code Duplication
    private function getDbEventManagerServiceDefinition(Container $container): \Closure
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...
198
    {
199
        return function () use ($container) {
200
            $dbs = $container['dbs.event_manager'];
201
202
            return $dbs[$container['dbs.default']];
203
        };
204
    }
205
}
206