Test Failed
Pull Request — master (#2)
by Dominik
02:37
created

DoctrineDbalFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 109
c 1
b 0
f 0
dl 0
loc 186
rs 10
wmc 16

1 Method

Rating   Name   Duplication   Size   Complexity  
D __invoke() 0 184 16
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\DoctrineDbServiceProvider\Factory;
6
7
use Chubbyphp\Container\Container;
8
use Chubbyphp\Container\ContainerInterface;
9
use Chubbyphp\DoctrineDbServiceProvider\Logger\DoctrineDbalLogger;
10
use Chubbyphp\DoctrineDbServiceProvider\Registry\DoctrineDbalPsrConnectionRegistry;
11
use Doctrine\Common\Cache\ApcuCache;
12
use Doctrine\Common\Cache\ArrayCache;
13
use Doctrine\Common\Cache\Cache;
14
use Doctrine\Common\EventManager;
15
use Doctrine\DBAL\Configuration;
16
use Doctrine\DBAL\DriverManager;
17
use Doctrine\DBAL\Types\Type;
18
19
final class DoctrineDbalFactory
20
{
21
    public function __invoke(): array
22
    {
23
        return [
24
            'doctrine.dbal.connection_registry' => static function (ContainerInterface $container) {
25
                $container->get('doctrine.dbal.dbs.options.initializer')();
26
27
                return new DoctrineDbalPsrConnectionRegistry(
28
                    $container,
29
                    array_keys($container->get('doctrine.dbal.dbs.options'))
30
                );
31
            },
32
            'doctrine.dbal.db' => static function (ContainerInterface $container) {
33
                /** @var Container $dbs */
34
                $dbs = $container->get('doctrine.dbal.dbs');
35
36
                return $dbs->get($container->get('doctrine.dbal.dbs.default'));
37
            },
38
            'doctrine.dbal.db.cache_factory.apcu' => static function () {
39
                return static function () {
40
                    return new ApcuCache();
41
                };
42
            },
43
            'doctrine.dbal.db.cache_factory.array' => static function () {
44
                return static function () {
45
                    return new ArrayCache();
46
                };
47
            },
48
            'doctrine.dbal.db.config' => static function (ContainerInterface $container) {
49
                /** @var Container $dbsConfigs */
50
                $dbsConfigs = $container->get('doctrine.dbal.dbs.config');
51
52
                return $dbsConfigs->get($container->get('doctrine.dbal.dbs.default'));
53
            },
54
            'doctrine.dbal.db.default_options' => static function () {
55
                return [
56
                    'configuration' => [
57
                        'auto_commit' => true,
58
                        'cache.result' => ['type' => 'array'],
59
                        'filter_schema_assets_expression' => null, // @deprecated
60
                        'schema_assets_filter' => null,
61
                    ],
62
                    'connection' => [
63
                        'charset' => 'utf8mb4',
64
                        'dbname' => null,
65
                        'driver' => 'pdo_mysql',
66
                        'host' => 'localhost',
67
                        'password' => null,
68
                        'path' => null,
69
                        'port' => 3306,
70
                        'user' => 'root',
71
                    ],
72
                ];
73
            },
74
            'doctrine.dbal.db.event_manager' => static function (ContainerInterface $container) {
75
                /** @var Container $dbEvents */
76
                $dbEvents = $container->get('doctrine.dbal.dbs.event_manager');
77
78
                return $dbEvents->get($container->get('doctrine.dbal.dbs.default'));
79
            },
80
            'doctrine.dbal.dbs' => static function (ContainerInterface $container) {
81
                $container->get('doctrine.dbal.dbs.options.initializer')();
82
83
                $dbs = new Container();
84
                foreach ($container->get('doctrine.dbal.dbs.options') as $name => $options) {
85
                    if ($container->get('doctrine.dbal.dbs.default') === $name) {
86
                        $config = $container->get('doctrine.dbal.db.config');
87
                        $manager = $container->get('doctrine.dbal.db.event_manager');
88
                    } else {
89
                        $config = $container->get('doctrine.dbal.dbs.config')->get($name);
90
                        $manager = $container->get('doctrine.dbal.dbs.event_manager')->get($name);
91
                    }
92
93
                    $dbs->factory($name, static function () use ($options, $config, $manager) {
94
                        return DriverManager::getConnection($options['connection'], $config, $manager);
95
                    });
96
                }
97
98
                return $dbs;
99
            },
100
            'doctrine.dbal.dbs.config' => static function (ContainerInterface $container) {
101
                $container->get('doctrine.dbal.dbs.options.initializer')();
102
103
                $logger = $container->has('logger') ? $container->get('logger') : null;
104
105
                $cache = function (array $cacheDefinition) use ($container): Cache {
106
                    $cacheType = $cacheDefinition['type'];
107
                    $cacheOptions = $cacheDefinition['options'] ?? [];
108
109
                    $cacheFactory = $container->get(sprintf('doctrine.dbal.db.cache_factory.%s', $cacheType));
110
111
                    return $cacheFactory($cacheOptions);
112
                };
113
114
                $configs = new Container();
115
                foreach ($container->get('doctrine.dbal.dbs.options') as $name => $options) {
116
                    $configs->factory($name, function () use ($logger, $cache, $options) {
117
                        $configOptions = $options['configuration'];
118
119
                        $config = new Configuration();
120
121
                        if (null !== $logger) {
122
                            $config->setSQLLogger(new DoctrineDbalLogger($logger));
123
                        }
124
125
                        $config->setResultCacheImpl($cache($configOptions['cache.result']));
126
127
                        if (null !== $configOptions['filter_schema_assets_expression']) {
128
                            // @deprecated
129
                            $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

129
                            /** @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...
130
                        }
131
132
                        if (null !== $configOptions['schema_assets_filter']) {
133
                            $config->setSchemaAssetsFilter($configOptions['schema_assets_filter']);
134
                        }
135
136
                        $config->setAutoCommit($configOptions['auto_commit']);
137
138
                        return $config;
139
                    });
140
                }
141
142
                return $configs;
143
            },
144
            'doctrine.dbal.dbs.event_manager' => static function (ContainerInterface $container) {
145
                $container->get('doctrine.dbal.dbs.options.initializer')();
146
147
                $managers = new Container();
148
                foreach (array_keys($container->get('doctrine.dbal.dbs.options')) as $name) {
149
                    $managers->factory($name, static function () {
150
                        return new EventManager();
151
                    });
152
                }
153
154
                return $managers;
155
            },
156
            'doctrine.dbal.dbs.options.initializer' => static function (ContainerInterface $container) {
157
                return static function () use ($container): void {
158
                    static $initialized = false;
159
160
                    if ($initialized) {
161
                        return;
162
                    }
163
164
                    $initialized = true;
165
166
                    foreach ((array) $container->get('doctrine.dbal.types') as $typeName => $typeClass) {
167
                        if (Type::hasType($typeName)) {
168
                            Type::overrideType($typeName, $typeClass);
169
                        } else {
170
                            Type::addType($typeName, $typeClass);
171
                        }
172
                    }
173
174
                    if (!$container->has('doctrine.dbal.dbs.options')) {
175
                        $container->factory('doctrine.dbal.dbs.options', static function (ContainerInterface $container) {
176
                            return [
177
                                'default' => $container->has('doctrine.dbal.db.options')
178
                                    ? $container->get('doctrine.dbal.db.options')
179
                                    : [],
180
                            ];
181
                        });
182
                    }
183
184
                    $tmp = $container->get('doctrine.dbal.dbs.options');
185
                    foreach ($tmp as $name => &$options) {
186
                        $options = array_replace_recursive(
187
                            $container->get('doctrine.dbal.db.default_options'),
188
                            $options
189
                        );
190
191
                        if (!$container->has('doctrine.dbal.dbs.default')) {
192
                            $container->factory('doctrine.dbal.dbs.default', function () use ($name) {
193
                                return $name;
194
                            });
195
                        }
196
                    }
197
198
                    $container->factory('doctrine.dbal.dbs.options', function () use ($tmp) {
199
                        return $tmp;
200
                    });
201
                };
202
            },
203
            'doctrine.dbal.types' => function () {
204
                return [];
205
            },
206
        ];
207
    }
208
}
209