Passed
Push — master ( 4e48bc...38114c )
by Dominik
03:10
created

DoctrineDbalServiceProvider::getCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ServiceProvider;
6
7
use Chubbyphp\ServiceProvider\Logger\DoctrineDbalLogger;
8
use Doctrine\Common\Cache\ApcuCache;
9
use Doctrine\Common\Cache\ArrayCache;
10
use Doctrine\Common\Cache\Cache;
11
use Doctrine\Common\EventManager;
12
use Doctrine\DBAL\Configuration;
13
use Doctrine\DBAL\DriverManager;
14
use Doctrine\DBAL\Types\Type;
15
use Pimple\Container;
16
use Pimple\ServiceProviderInterface;
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'] = $this->getDbDefinition($container);
26 3
        $container['doctrine.dbal.db.cache_factory.apcu'] = $this->getDbApcuCacheFactoryDefinition($container);
27 3
        $container['doctrine.dbal.db.cache_factory.array'] = $this->getDbArrayCacheFactoryDefinition($container);
28 3
        $container['doctrine.dbal.db.config'] = $this->getDbConfigDefinition($container);
29 3
        $container['doctrine.dbal.db.default_options'] = $this->getDbDefaultOptions();
30 3
        $container['doctrine.dbal.db.event_manager'] = $this->getDbEventManagerDefinition($container);
31 3
        $container['doctrine.dbal.dbs'] = $this->getDbsDefinition($container);
32 3
        $container['doctrine.dbal.dbs.config'] = $this->getDbsConfigDefinition($container);
33 3
        $container['doctrine.dbal.dbs.event_manager'] = $this->getDbsEventManagerDefinition($container);
34 3
        $container['doctrine.dbal.dbs.options.initializer'] = $this->getDbsOptionsInitializerDefinition($container);
35 3
        $container['doctrine.dbal.types'] = [];
36 3
    }
37
38
    /**
39
     * @param Container $container
40
     *
41
     * @return callable
42
     */
43
    private function getDbDefinition(Container $container): callable
44
    {
45 3
        return function () use ($container) {
46 2
            $dbs = $container['doctrine.dbal.dbs'];
47
48 2
            return $dbs[$container['doctrine.dbal.dbs.default']];
49 3
        };
50
    }
51
52
    /**
53
     * @param Container $container
54
     *
55
     * @return callable
56
     */
57
    private function getDbApcuCacheFactoryDefinition(Container $container): callable
58
    {
59 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...
60 1
            return new ApcuCache();
61 3
        });
62
    }
63
64
    /**
65
     * @param Container $container
66
     *
67
     * @return callable
68
     */
69
    private function getDbArrayCacheFactoryDefinition(Container $container): callable
70
    {
71 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...
72 1
            return new ArrayCache();
73 3
        });
74
    }
75
76
    /**
77
     * @param Container $container
78
     *
79
     * @return callable
80
     */
81
    private function getDbConfigDefinition(Container $container): callable
82
    {
83 3
        return function () use ($container) {
84 3
            $dbs = $container['doctrine.dbal.dbs.config'];
85
86 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
87 3
        };
88
    }
89
90
    /**
91
     * @return array
92
     */
93 3
    private function getDbDefaultOptions(): array
94
    {
95
        return [
96 3
            'configuration' => [
97
                'auto_commit' => true,
98
                'cache.result' => 'array',
99
                'filter_schema_assets_expression' => null,
100
            ],
101
            'connection' => [
102
                'charset' => 'utf8mb4',
103
                'dbname' => null,
104
                'driver' => 'pdo_mysql',
105
                'host' => 'localhost',
106
                'password' => null,
107
                'path' => null,
108
                'port' => 3306,
109
                'user' => 'root',
110
            ],
111
        ];
112
    }
113
114
    /**
115
     * @param Container $container
116
     *
117
     * @return callable
118
     */
119
    private function getDbEventManagerDefinition(Container $container): callable
120
    {
121 3
        return function () use ($container) {
122 3
            $dbs = $container['doctrine.dbal.dbs.event_manager'];
123
124 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
125 3
        };
126
    }
127
128
    /**
129
     * @param Container $container
130
     *
131
     * @return callable
132
     */
133
    private function getDbsDefinition(Container $container): callable
134
    {
135 3
        return function () use ($container) {
136 3
            $container['doctrine.dbal.dbs.options.initializer']();
137
138 3
            $dbs = new Container();
139 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
140 3
                if ($container['doctrine.dbal.dbs.default'] === $name) {
141 3
                    $config = $container['doctrine.dbal.db.config'];
142 3
                    $manager = $container['doctrine.dbal.db.event_manager'];
143
                } else {
144 1
                    $config = $container['doctrine.dbal.dbs.config'][$name];
145 1
                    $manager = $container['doctrine.dbal.dbs.event_manager'][$name];
146
                }
147
148 3
                $dbs[$name] = function () use ($options, $config, $manager) {
149 3
                    return DriverManager::getConnection($options['connection'], $config, $manager);
150 3
                };
151
            }
152
153 3
            return $dbs;
154 3
        };
155
    }
156
157
    /**
158
     * @param Container $container
159
     *
160
     * @return callable
161
     */
162
    private function getDbsConfigDefinition(Container $container): callable
163
    {
164 3
        return function () use ($container) {
165 3
            $container['doctrine.dbal.dbs.options.initializer']();
166
167 3
            $addLogger = $container['logger'] ?? false;
168
169 3
            $configs = new Container();
170 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
171 3
                $configs[$name] = function () use ($addLogger, $container, $name, $options) {
172 3
                    $configOptions = $options['configuration'];
173
174 3
                    $config = new Configuration();
175
176 3
                    if ($addLogger) {
177 2
                        $config->setSQLLogger(new DoctrineDbalLogger($container['logger']));
178
                    }
179
180 3
                    $config->setResultCacheImpl(static::getCache($container, $configOptions['cache.result']));
181
182 3
                    $config->setFilterSchemaAssetsExpression($configOptions['filter_schema_assets_expression']);
183 3
                    $config->setAutoCommit($configOptions['auto_commit']);
184
185 3
                    return $config;
186 3
                };
187
            }
188
189 3
            return $configs;
190 3
        };
191
    }
192
193
    /**
194
     * @param Container $container
195
     *
196
     * @return callable
197
     */
198 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...
199
    {
200 3
        return function () use ($container) {
201 3
            $container['doctrine.dbal.dbs.options.initializer']();
202
203 3
            $managers = new Container();
204 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
205 3
                $managers[$name] = function () {
206 3
                    return new EventManager();
207 3
                };
208
            }
209
210 3
            return $managers;
211 3
        };
212
    }
213
214
    /**
215
     * @param Container $container
216
     *
217
     * @return callable
218
     */
219
    private function getDbsOptionsInitializerDefinition(Container $container): callable
220
    {
221 3
        return $container->protect(function () use ($container) {
222 3
            static $initialized = false;
223
224 3
            if ($initialized) {
225 3
                return;
226
            }
227
228 3
            $initialized = true;
229
230 3
            foreach ((array) $container['doctrine.dbal.types'] as $typeName => $typeClass) {
231 1
                if (Type::hasType($typeName)) {
232 1
                    Type::overrideType($typeName, $typeClass);
233
                } else {
234 1
                    Type::addType($typeName, $typeClass);
235
                }
236
            }
237
238 3
            if (!isset($container['doctrine.dbal.dbs.options'])) {
239 2
                $container['doctrine.dbal.dbs.options'] = [
240 2
                    'default' => $container['doctrine.dbal.db.options'] ?? [],
241
                ];
242
            }
243
244 3
            $tmp = $container['doctrine.dbal.dbs.options'];
245 3
            foreach ($tmp as $name => &$options) {
246 3
                $options = array_replace_recursive($container['doctrine.dbal.db.default_options'], $options);
247
248 3
                if (!isset($container['doctrine.dbal.dbs.default'])) {
249 3
                    $container['doctrine.dbal.dbs.default'] = $name;
250
                }
251
            }
252
253 3
            $container['doctrine.dbal.dbs.options'] = $tmp;
254 3
        });
255
    }
256
257
    /**
258
     * @param Container    $container
259
     * @param string|array $cacheDefinition
260
     *
261
     * @return Cache
262
     *
263
     * @throws \InvalidArgumentException
264
     */
265 3
    public static function getCache(Container $container, $cacheDefinition): Cache
266
    {
267 3
        $cacheType = 'array';
268 3
        $cacheOptions = [];
269
270 3
        if (is_string($cacheDefinition)) {
271 2
            $cacheType = $cacheDefinition;
272 1
        } elseif (is_array($cacheDefinition)) {
273 1
            $cacheType = $cacheDefinition['type'];
274 1
            $cacheOptions = $cacheDefinition['options'] ?? [];
275
        }
276
277 3
        $cacheFactoryKey = sprintf('doctrine.dbal.db.cache_factory.%s', $cacheType);
278
279 3
        $cacheFactory = $container[$cacheFactoryKey];
280
281 3
        return $cacheFactory($cacheOptions);
282
    }
283
}
284