Test Failed
Pull Request — master (#2)
by Dominik
03:24
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\Psr\DoctrineDbalConnectionRegistry;
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
                return new DoctrineDbalConnectionRegistry($container);
26
            },
27
            'doctrine.dbal.db' => static function (ContainerInterface $container) {
28
                /** @var Container $dbs */
29
                $dbs = $container->get('doctrine.dbal.dbs');
30
31
                return $dbs->get($container->get('doctrine.dbal.dbs.default'));
32
            },
33
            'doctrine.dbal.db.cache_factory.apcu' => static function () {
34
                return static function () {
35
                    return new ApcuCache();
36
                };
37
            },
38
            'doctrine.dbal.db.cache_factory.array' => static function () {
39
                return static function () {
40
                    return new ArrayCache();
41
                };
42
            },
43
            'doctrine.dbal.db.config' => static function (ContainerInterface $container) {
44
                /** @var Container $dbsConfigs */
45
                $dbsConfigs = $container->get('doctrine.dbal.dbs.config');
46
47
                return $dbsConfigs->get($container->get('doctrine.dbal.dbs.default'));
48
            },
49
            'doctrine.dbal.db.default_options' => static function () {
50
                return [
51
                    'configuration' => [
52
                        'auto_commit' => true,
53
                        'cache.result' => ['type' => 'array'],
54
                        'filter_schema_assets_expression' => null, // @deprecated
55
                        'schema_assets_filter' => null,
56
                    ],
57
                    'connection' => [
58
                        'charset' => 'utf8mb4',
59
                        'dbname' => null,
60
                        'driver' => 'pdo_mysql',
61
                        'host' => 'localhost',
62
                        'password' => null,
63
                        'path' => null,
64
                        'port' => 3306,
65
                        'user' => 'root',
66
                    ],
67
                ];
68
            },
69
            'doctrine.dbal.db.event_manager' => static function (ContainerInterface $container) {
70
                /** @var Container $dbEvents */
71
                $dbEvents = $container->get('doctrine.dbal.dbs.event_manager');
72
73
                return $dbEvents->get($container->get('doctrine.dbal.dbs.default'));
74
            },
75
            'doctrine.dbal.dbs' => static function (ContainerInterface $container) {
76
                $container->get('doctrine.dbal.dbs.options.initializer')();
77
78
                $dbs = new Container();
79
                foreach ($container->get('doctrine.dbal.dbs.options') as $name => $options) {
80
                    if ($container->get('doctrine.dbal.dbs.default') === $name) {
81
                        $config = $container->get('doctrine.dbal.db.config');
82
                        $manager = $container->get('doctrine.dbal.db.event_manager');
83
                    } else {
84
                        $config = $container->get('doctrine.dbal.dbs.config')->get($name);
85
                        $manager = $container->get('doctrine.dbal.dbs.event_manager')->get($name);
86
                    }
87
88
                    $dbs->factory($name, static function () use ($options, $config, $manager) {
89
                        return DriverManager::getConnection($options['connection'], $config, $manager);
90
                    });
91
                }
92
93
                return $dbs;
94
            },
95
            'doctrine.dbal.dbs.config' => static function (ContainerInterface $container) {
96
                $container->get('doctrine.dbal.dbs.options.initializer')();
97
98
                $logger = $container->has('logger') ? $container->get('logger') : null;
99
100
                $cache = function (array $cacheDefinition) use ($container): Cache {
101
                    $cacheType = $cacheDefinition['type'];
102
                    $cacheOptions = $cacheDefinition['options'] ?? [];
103
104
                    $cacheFactory = $container->get(sprintf('doctrine.dbal.db.cache_factory.%s', $cacheType));
105
106
                    return $cacheFactory($cacheOptions);
107
                };
108
109
                $configs = new Container();
110
                foreach ($container->get('doctrine.dbal.dbs.options') as $name => $options) {
111
                    $configs->factory($name, function () use ($logger, $cache, $options) {
112
                        $configOptions = $options['configuration'];
113
114
                        $config = new Configuration();
115
116
                        if (null !== $logger) {
117
                            $config->setSQLLogger(new DoctrineDbalLogger($logger));
118
                        }
119
120
                        $config->setResultCacheImpl($cache($configOptions['cache.result']));
121
122
                        if (null !== $configOptions['filter_schema_assets_expression']) {
123
                            // @deprecated
124
                            $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

124
                            /** @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...
125
                        }
126
127
                        if (null !== $configOptions['schema_assets_filter']) {
128
                            $config->setSchemaAssetsFilter($configOptions['schema_assets_filter']);
129
                        }
130
131
                        $config->setAutoCommit($configOptions['auto_commit']);
132
133
                        return $config;
134
                    });
135
                }
136
137
                return $configs;
138
            },
139
            'doctrine.dbal.dbs.event_manager' => static function (ContainerInterface $container) {
140
                $container->get('doctrine.dbal.dbs.options.initializer')();
141
142
                $managers = new Container();
143
                foreach ($container->get('doctrine.dbal.dbs.name') as $name) {
144
                    $managers->factory((string) $name, static function () {
145
                        return new EventManager();
146
                    });
147
                }
148
149
                return $managers;
150
            },
151
            'doctrine.dbal.dbs.name' => static function (ContainerInterface $container) {
152
                $container->get('doctrine.dbal.dbs.options.initializer')();
153
154
                return array_keys($container->get('doctrine.dbal.dbs.options'));
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