Test Failed
Pull Request — master (#2)
by Dominik
03:43
created

getDbArrayCacheFactoryDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
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
/**
20
 * This provider is heavily inspired by
21
 * https://github.com/silexphp/Silex-Providers/blob/master/DoctrineServiceProvider.php.
22
 */
23
final class DoctrineDbalServiceProvider implements ServiceProviderInterface
24
{
25 3
    public function register(Container $container): void
26
    {
27 3
        $container['doctrine.dbal.connection_registry'] = $this->getDbConnectionRegistryDefintion($container);
28 3
        $container['doctrine.dbal.db'] = $this->getDbDefinition($container);
29 3
        $container['doctrine.dbal.db.cache_factory.apcu'] = $this->getDbApcuCacheFactoryDefinition($container);
30 3
        $container['doctrine.dbal.db.cache_factory.array'] = $this->getDbArrayCacheFactoryDefinition($container);
31 3
        $container['doctrine.dbal.db.config'] = $this->getDbConfigDefinition($container);
32 3
        $container['doctrine.dbal.db.default_options'] = $this->getDbDefaultOptions();
33 3
        $container['doctrine.dbal.db.event_manager'] = $this->getDbEventManagerDefinition($container);
34 3
        $container['doctrine.dbal.dbs'] = $this->getDbsDefinition($container);
35 3
        $container['doctrine.dbal.dbs.config'] = $this->getDbsConfigDefinition($container);
36 3
        $container['doctrine.dbal.dbs.event_manager'] = $this->getDbsEventManagerDefinition($container);
37 3
        $container['doctrine.dbal.dbs.name'] = $this->getDbsNameDefinition($container);
38 3
        $container['doctrine.dbal.dbs.options.initializer'] = $this->getDbsOptionsInitializerDefinition($container);
39 3
        $container['doctrine.dbal.types'] = [];
40
    }
41 3
42
    private function getDbConnectionRegistryDefintion(Container $container): callable
43
    {
44 1
        return static function () use ($container) {
45 3
            return new DoctrineDbalConnectionRegistry($container);
46
        };
47
    }
48 3
49
    private function getDbDefinition(Container $container): callable
50
    {
51 2
        return static function () use ($container) {
52
            $dbs = $container['doctrine.dbal.dbs'];
53 2
54 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
55
        };
56
    }
57 3
58
    private function getDbApcuCacheFactoryDefinition(Container $container): callable
59
    {
60 1
        return $container->protect(static function () {
61 3
            return new ApcuCache();
62
        });
63
    }
64 3
65
    private function getDbArrayCacheFactoryDefinition(Container $container): callable
66
    {
67 2
        return $container->protect(static function () {
68 3
            return new ArrayCache();
69
        });
70
    }
71 3
72
    private function getDbConfigDefinition(Container $container): callable
73
    {
74 3
        return static function () use ($container) {
75
            $dbs = $container['doctrine.dbal.dbs.config'];
76 3
77 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
78
        };
79
    }
80
81
    /**
82
     * @return array<string, array|string|float|int|bool>
83 3
     */
84
    private function getDbDefaultOptions(): array
85
    {
86 3
        return [
87
            'configuration' => [
88
                'auto_commit' => true,
89
                'cache.result' => ['type' => 'array'],
90
                'filter_schema_assets_expression' => null, // @deprecated
91
                'schema_assets_filter' => null,
92
            ],
93
            'connection' => [
94
                'charset' => 'utf8mb4',
95
                'dbname' => null,
96
                'driver' => 'pdo_mysql',
97
                'host' => 'localhost',
98
                'password' => null,
99
                'path' => null,
100
                'port' => 3306,
101
                'user' => 'root',
102
            ],
103
        ];
104
    }
105 3
106
    private function getDbEventManagerDefinition(Container $container): callable
107
    {
108 3
        return static function () use ($container) {
109
            $dbs = $container['doctrine.dbal.dbs.event_manager'];
110 3
111 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
112
        };
113
    }
114 3
115
    private function getDbsDefinition(Container $container): callable
116
    {
117 3
        return static function () use ($container) {
118
            $container['doctrine.dbal.dbs.options.initializer']();
119 3
120 3
            $dbs = new Container();
121 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
122 3
                if ($container['doctrine.dbal.dbs.default'] === $name) {
123 3
                    $config = $container['doctrine.dbal.db.config'];
124
                    $manager = $container['doctrine.dbal.db.event_manager'];
125 1
                } else {
126 1
                    $config = $container['doctrine.dbal.dbs.config'][$name];
127
                    $manager = $container['doctrine.dbal.dbs.event_manager'][$name];
128
                }
129
130 3
                $dbs[$name] = static function () use ($options, $config, $manager) {
131 3
                    return DriverManager::getConnection($options['connection'], $config, $manager);
132
                };
133
            }
134 3
135 3
            return $dbs;
136
        };
137
    }
138 3
139
    private function getDbsNameDefinition(Container $container): callable
140
    {
141 3
        return static function () use ($container) {
142
            $container['doctrine.dbal.dbs.options.initializer']();
143 3
144
            return array_keys($container['doctrine.dbal.dbs.options']);
145 3
        };
146 3
    }
147
148 3
    private function getDbsConfigDefinition(Container $container): callable
149
    {
150 3
        return function () use ($container) {
151
            $container['doctrine.dbal.dbs.options.initializer']();
152 3
153 2
            $addLogger = $container['logger'] ?? false;
154
155
            $configs = new Container();
156 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
157
                $configs[$name] = function () use ($addLogger, $container, $options) {
158 3
                    $configOptions = $options['configuration'];
159
160 1
                    $config = new Configuration();
161
162
                    if ($addLogger) {
163 3
                        $config->setSQLLogger(new DoctrineDbalLogger($container['logger']));
164 1
                    }
165
166
                    $config->setResultCacheImpl($this->getCache($container, $configOptions['cache.result']));
167 3
168
                    if (null !== $configOptions['filter_schema_assets_expression']) {
169 3
                        // @deprecated
170 3
                        $config->setFilterSchemaAssetsExpression($configOptions['filter_schema_assets_expression']);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

170
                        /** @scrutinizer ignore-deprecated */ $config->setFilterSchemaAssetsExpression($configOptions['filter_schema_assets_expression']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
171
                    }
172
173 3
                    if (null !== $configOptions['schema_assets_filter']) {
174 3
                        $config->setSchemaAssetsFilter($configOptions['schema_assets_filter']);
175
                    }
176
177 3
                    $config->setAutoCommit($configOptions['auto_commit']);
178
179 3
                    return $config;
180 3
                };
181
            }
182 3
183
            return $configs;
184 3
        };
185
    }
186
187 3
    private function getCache(Container $container, array $cacheDefinition): Cache
188
    {
189
        $cacheType = $cacheDefinition['type'];
190 3
        $cacheOptions = $cacheDefinition['options'] ?? [];
191
192 3
        $cacheFactory = $container[sprintf('doctrine.dbal.db.cache_factory.%s', $cacheType)];
193 3
194
        return $cacheFactory($cacheOptions);
195 3
    }
196 3
197
    private function getDbsEventManagerDefinition(Container $container): callable
198
    {
199 3
        return static function () use ($container) {
200 3
            $container['doctrine.dbal.dbs.options.initializer']();
201
202
            $managers = new Container();
203 3
            foreach (array_keys($container['doctrine.dbal.dbs.options']) as $name) {
204
                $managers[$name] = static function () {
205
                    return new EventManager();
206 3
                };
207
            }
208 3
209 3
            return $managers;
210
        };
211
    }
212 3
213
    private function getDbsOptionsInitializerDefinition(Container $container): callable
214 3
    {
215 1
        return $container->protect(static function () use ($container): void {
216 1
            static $initialized = false;
217
218 1
            if ($initialized) {
219
                return;
220
            }
221
222 3
            $initialized = true;
223 2
224 2
            foreach ((array) $container['doctrine.dbal.types'] as $typeName => $typeClass) {
225
                if (Type::hasType($typeName)) {
226
                    Type::overrideType($typeName, $typeClass);
227
                } else {
228 3
                    Type::addType($typeName, $typeClass);
229 3
                }
230 3
            }
231
232 3
            if (!isset($container['doctrine.dbal.dbs.options'])) {
233 3
                $container['doctrine.dbal.dbs.options'] = [
234
                    'default' => $container['doctrine.dbal.db.options'] ?? [],
235
                ];
236
            }
237 3
238 3
            $tmp = $container['doctrine.dbal.dbs.options'];
239
            foreach ($tmp as $name => &$options) {
240
                $options = array_replace_recursive($container['doctrine.dbal.db.default_options'], $options);
241
242
                if (!isset($container['doctrine.dbal.dbs.default'])) {
243
                    $container['doctrine.dbal.dbs.default'] = $name;
244
                }
245
            }
246
247
            $container['doctrine.dbal.dbs.options'] = $tmp;
248
        });
249
    }
250
}
251