Completed
Push — master ( 6806a6...277dd8 )
by Dominik
03:26
created

DoctrineDbalServiceProvider::getCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\DoctrineDbServiceProvider\ServiceProvider;
6
7
use Chubbyphp\DoctrineDbServiceProvider\Logger\DoctrineDbalLogger;
8
use Chubbyphp\DoctrineDbServiceProvider\Registry\DoctrineDbalConnectionRegistry;
9
use Doctrine\Common\Cache\ApcuCache;
10
use Doctrine\Common\Cache\ArrayCache;
11
use Doctrine\Common\Cache\Cache;
12
use Doctrine\Common\EventManager;
13
use Doctrine\DBAL\Configuration;
14
use Doctrine\DBAL\DriverManager;
15
use Doctrine\DBAL\Types\Type;
16
use Pimple\Container;
17
use Pimple\ServiceProviderInterface;
18
19
final class DoctrineDbalServiceProvider implements ServiceProviderInterface
20
{
21
    /**
22
     * @param Container $container
23
     */
24 3
    public function register(Container $container)
25
    {
26 3
        $container['doctrine.dbal.connection_registry'] = $this->getDbConnectionRegistryDefintion($container);
27 3
        $container['doctrine.dbal.db'] = $this->getDbDefinition($container);
28 3
        $container['doctrine.dbal.db.cache_factory.apcu'] = $this->getDbApcuCacheFactoryDefinition($container);
29 3
        $container['doctrine.dbal.db.cache_factory.array'] = $this->getDbArrayCacheFactoryDefinition($container);
30 3
        $container['doctrine.dbal.db.config'] = $this->getDbConfigDefinition($container);
31 3
        $container['doctrine.dbal.db.default_options'] = $this->getDbDefaultOptions();
32 3
        $container['doctrine.dbal.db.event_manager'] = $this->getDbEventManagerDefinition($container);
33 3
        $container['doctrine.dbal.dbs'] = $this->getDbsDefinition($container);
34 3
        $container['doctrine.dbal.dbs.config'] = $this->getDbsConfigDefinition($container);
35 3
        $container['doctrine.dbal.dbs.event_manager'] = $this->getDbsEventManagerDefinition($container);
36 3
        $container['doctrine.dbal.dbs.options.initializer'] = $this->getDbsOptionsInitializerDefinition($container);
37 3
        $container['doctrine.dbal.types'] = [];
38 3
    }
39
40
    /**
41
     * @param Container $container
42
     *
43
     * @return callable
44
     */
45
    private function getDbConnectionRegistryDefintion(Container $container): callable
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47 3
        return function ($container) {
48 1
            return new DoctrineDbalConnectionRegistry($container);
49 3
        };
50
    }
51
52
    /**
53
     * @param Container $container
54
     *
55
     * @return callable
56
     */
57
    private function getDbDefinition(Container $container): callable
58
    {
59 3
        return function () use ($container) {
60 2
            $dbs = $container['doctrine.dbal.dbs'];
61
62 2
            return $dbs[$container['doctrine.dbal.dbs.default']];
63 3
        };
64
    }
65
66
    /**
67
     * @param Container $container
68
     *
69
     * @return callable
70
     */
71
    private function getDbApcuCacheFactoryDefinition(Container $container): callable
72
    {
73 3
        return $container->protect(function (array $options) use ($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74 1
            return new ApcuCache();
75 3
        });
76
    }
77
78
    /**
79
     * @param Container $container
80
     *
81
     * @return callable
82
     */
83
    private function getDbArrayCacheFactoryDefinition(Container $container): callable
84
    {
85 3
        return $container->protect(function (array $options) use ($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86 1
            return new ArrayCache();
87 3
        });
88
    }
89
90
    /**
91
     * @param Container $container
92
     *
93
     * @return callable
94
     */
95
    private function getDbConfigDefinition(Container $container): callable
96
    {
97 3
        return function () use ($container) {
98 3
            $dbs = $container['doctrine.dbal.dbs.config'];
99
100 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
101 3
        };
102
    }
103
104
    /**
105
     * @return array
106
     */
107 3
    private function getDbDefaultOptions(): array
108
    {
109
        return [
110 3
            'configuration' => [
111
                'auto_commit' => true,
112
                'cache.result' => ['type' => 'array'],
113
                'filter_schema_assets_expression' => null,
114
            ],
115
            'connection' => [
116
                'charset' => 'utf8mb4',
117
                'dbname' => null,
118
                'driver' => 'pdo_mysql',
119
                'host' => 'localhost',
120
                'password' => null,
121
                'path' => null,
122
                'port' => 3306,
123
                'user' => 'root',
124
            ],
125
        ];
126
    }
127
128
    /**
129
     * @param Container $container
130
     *
131
     * @return callable
132
     */
133
    private function getDbEventManagerDefinition(Container $container): callable
134
    {
135 3
        return function () use ($container) {
136 3
            $dbs = $container['doctrine.dbal.dbs.event_manager'];
137
138 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
139 3
        };
140
    }
141
142
    /**
143
     * @param Container $container
144
     *
145
     * @return callable
146
     */
147
    private function getDbsDefinition(Container $container): callable
148
    {
149 3
        return function () use ($container) {
150 3
            $container['doctrine.dbal.dbs.options.initializer']();
151
152 3
            $dbs = new Container();
153 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
154 3
                if ($container['doctrine.dbal.dbs.default'] === $name) {
155 3
                    $config = $container['doctrine.dbal.db.config'];
156 3
                    $manager = $container['doctrine.dbal.db.event_manager'];
157
                } else {
158 1
                    $config = $container['doctrine.dbal.dbs.config'][$name];
159 1
                    $manager = $container['doctrine.dbal.dbs.event_manager'][$name];
160
                }
161
162 3
                $dbs[$name] = function () use ($options, $config, $manager) {
163 3
                    return DriverManager::getConnection($options['connection'], $config, $manager);
164 3
                };
165
            }
166
167 3
            return $dbs;
168 3
        };
169
    }
170
171
    /**
172
     * @param Container $container
173
     *
174
     * @return callable
175
     */
176
    private function getDbsConfigDefinition(Container $container): callable
177
    {
178 3
        return function () use ($container) {
179 3
            $container['doctrine.dbal.dbs.options.initializer']();
180
181 3
            $addLogger = $container['logger'] ?? false;
182
183 3
            $configs = new Container();
184 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
185 3
                $configs[$name] = function () use ($addLogger, $container, $name, $options) {
186 3
                    $configOptions = $options['configuration'];
187
188 3
                    $config = new Configuration();
189
190 3
                    if ($addLogger) {
191 2
                        $config->setSQLLogger(new DoctrineDbalLogger($container['logger']));
192
                    }
193
194 3
                    $config->setResultCacheImpl($this->getCache($container, $configOptions['cache.result']));
195
196 3
                    $config->setFilterSchemaAssetsExpression($configOptions['filter_schema_assets_expression']);
197 3
                    $config->setAutoCommit($configOptions['auto_commit']);
198
199 3
                    return $config;
200 3
                };
201
            }
202
203 3
            return $configs;
204 3
        };
205
    }
206
207
    /**
208
     * @param Container    $container
209
     * @param string|array $cacheDefinition
210
     *
211
     * @return Cache
212
     */
213 3 View Code Duplication
    private function getCache(Container $container, $cacheDefinition): Cache
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...
214
    {
215 3
        $cacheType = $cacheDefinition['type'];
216 3
        $cacheOptions = $cacheDefinition['options'] ?? [];
217
218 3
        $cacheFactory = $container[sprintf('doctrine.dbal.db.cache_factory.%s', $cacheType)];
219
220 3
        return $cacheFactory($cacheOptions);
221
    }
222
223
    /**
224
     * @param Container $container
225
     *
226
     * @return callable
227
     */
228
    private function getDbsEventManagerDefinition(Container $container): callable
229
    {
230 3
        return function () use ($container) {
231 3
            $container['doctrine.dbal.dbs.options.initializer']();
232
233 3
            $managers = new Container();
234 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
235 3
                $managers[$name] = function () {
236 3
                    return new EventManager();
237 3
                };
238
            }
239
240 3
            return $managers;
241 3
        };
242
    }
243
244
    /**
245
     * @param Container $container
246
     *
247
     * @return callable
248
     */
249
    private function getDbsOptionsInitializerDefinition(Container $container): callable
250
    {
251 3
        return $container->protect(function () use ($container) {
252 3
            static $initialized = false;
253
254 3
            if ($initialized) {
255 3
                return;
256
            }
257
258 3
            $initialized = true;
259
260 3
            foreach ((array) $container['doctrine.dbal.types'] as $typeName => $typeClass) {
261 1
                if (Type::hasType($typeName)) {
262 1
                    Type::overrideType($typeName, $typeClass);
263
                } else {
264 1
                    Type::addType($typeName, $typeClass);
265
                }
266
            }
267
268 3
            if (!isset($container['doctrine.dbal.dbs.options'])) {
269 2
                $container['doctrine.dbal.dbs.options'] = [
270 2
                    'default' => $container['doctrine.dbal.db.options'] ?? [],
271
                ];
272
            }
273
274 3
            $tmp = $container['doctrine.dbal.dbs.options'];
275 3
            foreach ($tmp as $name => &$options) {
276 3
                $options = array_replace_recursive($container['doctrine.dbal.db.default_options'], $options);
277
278 3
                if (!isset($container['doctrine.dbal.dbs.default'])) {
279 3
                    $container['doctrine.dbal.dbs.default'] = $name;
280
                }
281
            }
282
283 3
            $container['doctrine.dbal.dbs.options'] = $tmp;
284 3
        });
285
    }
286
}
287