Passed
Push — master ( 96dbeb...546414 )
by Dominik
02:18
created

getDbsConfigDefinition()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
ccs 22
cts 22
cp 1
rs 8.5806
cc 4
eloc 21
nc 1
nop 1
crap 4
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.default_cache'] = ['driver' => 'array'];
30 3
        $container['doctrine.dbal.dbs.event_manager'] = $this->getDbsEventManagerDefinition($container);
31 3
        $container['doctrine.dbal.db'] = $this->getDbDefinition($container);
32 3
        $container['doctrine.dbal.db.config'] = $this->getDbConfigDefinition($container);
33 3
        $container['doctrine.dbal.db.event_manager'] = $this->getDbEventManagerDefinition($container);
34 3
    }
35
36
    /**
37
     * @return array
38
     */
39 3
    private function getDbDefaultOptions(): array
40
    {
41
        return [
42 3
            'connection' => [
43
                'driver' => 'pdo_mysql',
44
                'dbname' => null,
45
                'host' => 'localhost',
46
                'user' => 'root',
47
                'password' => null,
48
            ],
49
            'configuration' => [
50
                'result_cache' => 'array',
51
                'filter_schema_assets_expression' => null,
52
                'auto_commit' => true,
53
            ],
54
        ];
55
    }
56
57
    /**
58
     * @param Container $container
59
     *
60
     * @return callable
61
     */
62 View Code Duplication
    private function getDbsOptionsInitializerDefinition(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...
63
    {
64 3
        return $container->protect(function () use ($container) {
65 3
            static $initialized = false;
66
67 3
            if ($initialized) {
68 3
                return;
69
            }
70
71 3
            $initialized = true;
72
73 3
            if (!isset($container['doctrine.dbal.dbs.options'])) {
74 2
                $container['doctrine.dbal.dbs.options'] = [
75 2
                    'default' => $container['doctrine.dbal.db.options'] ?? [],
76
                ];
77
            }
78
79 3
            $tmp = $container['doctrine.dbal.dbs.options'];
80 3
            foreach ($tmp as $name => &$options) {
81 3
                $options = array_replace_recursive($container['doctrine.dbal.db.default_options'], $options);
82
83 3
                if (!isset($container['doctrine.dbal.dbs.default'])) {
84 3
                    $container['doctrine.dbal.dbs.default'] = $name;
85
                }
86
            }
87
88 3
            $container['doctrine.dbal.dbs.options'] = $tmp;
89 3
        });
90
    }
91
92
    /**
93
     * @param Container $container
94
     *
95
     * @return callable
96
     */
97
    private function getDbsDefinition(Container $container): callable
98
    {
99 3
        return function () use ($container) {
100 3
            $container['doctrine.dbal.dbs.options.initializer']();
101
102 3
            $dbs = new Container();
103 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
104 3
                if ($container['doctrine.dbal.dbs.default'] === $name) {
105
                    // we use shortcuts here in case the default has been overridden
106 3
                    $config = $container['doctrine.dbal.db.config'];
107 3
                    $manager = $container['doctrine.dbal.db.event_manager'];
108
                } else {
109 1
                    $config = $container['doctrine.dbal.dbs.config'][$name];
110 1
                    $manager = $container['doctrine.dbal.dbs.event_manager'][$name];
111
                }
112
113 3
                $dbs[$name] = function () use ($options, $config, $manager) {
114 3
                    return DriverManager::getConnection($options['connection'], $config, $manager);
115 3
                };
116
            }
117
118 3
            return $dbs;
119 3
        };
120
    }
121
122
    /**
123
     * @param Container $container
124
     *
125
     * @return callable
126
     */
127
    private function getDbsConfigDefinition(Container $container): callable
128
    {
129 3
        return function () use ($container) {
130 3
            $container['doctrine.dbal.dbs.options.initializer']();
131
132 3
            $addLogger = $container['logger'] ?? false;
133
134 3
            $configs = new Container();
135 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
136 3
                $configs[$name] = function () use ($addLogger, $container, $name, $options) {
137 3
                    $configOptions = $options['configuration'];
138
139 3
                    $config = new Configuration();
140
141 3
                    if ($addLogger) {
142 2
                        $config->setSQLLogger(new DoctrineDbalLogger($container['logger']));
143
                    }
144
145 3
                    if (is_string($configOptions['result_cache'])) {
146 3
                        $configOptions['result_cache'] = ['driver' => $configOptions['result_cache']];
147
                    }
148
149 3
                    $config->setResultCacheImpl(
150 3
                        $container['doctrine.cache.locator'](
151 3
                            sprintf('%s_%s', $name, 'result'),
152 3
                            $configOptions['result_cache']
153
                        )
154
                    );
155 3
                    $config->setFilterSchemaAssetsExpression($configOptions['filter_schema_assets_expression']);
156 3
                    $config->setAutoCommit($configOptions['auto_commit']);
157
158 3
                    return $config;
159 3
                };
160
            }
161
162 3
            return $configs;
163 3
        };
164
    }
165
166
    /**
167
     * @param Container $container
168
     *
169
     * @return callable
170
     */
171 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...
172
    {
173 3
        return function () use ($container) {
174 3
            $container['doctrine.dbal.dbs.options.initializer']();
175
176 3
            $managers = new Container();
177 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
178 3
                $managers[$name] = function () {
179 3
                    return new EventManager();
180 3
                };
181
            }
182
183 3
            return $managers;
184 3
        };
185
    }
186
187
    /***
188
     * @param Container $container
189
     * @return callable
190
     */
191
    private function getDbDefinition(Container $container): callable
192
    {
193 3
        return function () use ($container) {
194 2
            $dbs = $container['doctrine.dbal.dbs'];
195
196 2
            return $dbs[$container['doctrine.dbal.dbs.default']];
197 3
        };
198
    }
199
200
    /***
201
     * @param Container $container
202
     * @return callable
203
     */
204
    private function getDbConfigDefinition(Container $container): callable
205
    {
206 3
        return function () use ($container) {
207 3
            $dbs = $container['doctrine.dbal.dbs.config'];
208
209 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
210 3
        };
211
    }
212
213
    /***
214
     * @param Container $container
215
     * @return callable
216
     */
217
    private function getDbEventManagerDefinition(Container $container): callable
218
    {
219 3
        return function () use ($container) {
220 3
            $dbs = $container['doctrine.dbal.dbs.event_manager'];
221
222 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
223 3
        };
224
    }
225
}
226