Passed
Push — master ( 5778d3...f5b0ef )
by Dominik
02:17
created

getDbApcuCacheFactoryDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ServiceProvider\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' => ['type' => '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($this->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
     * @param string|array $cacheDefinition
196
     *
197
     * @return Cache
198
     */
199 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...
200
    {
201 3
        $cacheType = $cacheDefinition['type'];
202 3
        $cacheOptions = $cacheDefinition['options'] ?? [];
203
204 3
        $cacheFactory = $container[sprintf('doctrine.dbal.db.cache_factory.%s', $cacheType)];
205
206 3
        return $cacheFactory($cacheOptions);
207
    }
208
209
    /**
210
     * @param Container $container
211
     *
212
     * @return callable
213
     */
214 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...
215
    {
216 3
        return function () use ($container) {
217 3
            $container['doctrine.dbal.dbs.options.initializer']();
218
219 3
            $managers = new Container();
220 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
221 3
                $managers[$name] = function () {
222 3
                    return new EventManager();
223 3
                };
224
            }
225
226 3
            return $managers;
227 3
        };
228
    }
229
230
    /**
231
     * @param Container $container
232
     *
233
     * @return callable
234
     */
235
    private function getDbsOptionsInitializerDefinition(Container $container): callable
236
    {
237 3
        return $container->protect(function () use ($container) {
238 3
            static $initialized = false;
239
240 3
            if ($initialized) {
241 3
                return;
242
            }
243
244 3
            $initialized = true;
245
246 3
            foreach ((array) $container['doctrine.dbal.types'] as $typeName => $typeClass) {
247 1
                if (Type::hasType($typeName)) {
248 1
                    Type::overrideType($typeName, $typeClass);
249
                } else {
250 1
                    Type::addType($typeName, $typeClass);
251
                }
252
            }
253
254 3
            if (!isset($container['doctrine.dbal.dbs.options'])) {
255 2
                $container['doctrine.dbal.dbs.options'] = [
256 2
                    'default' => $container['doctrine.dbal.db.options'] ?? [],
257
                ];
258
            }
259
260 3
            $tmp = $container['doctrine.dbal.dbs.options'];
261 3
            foreach ($tmp as $name => &$options) {
262 3
                $options = array_replace_recursive($container['doctrine.dbal.db.default_options'], $options);
263
264 3
                if (!isset($container['doctrine.dbal.dbs.default'])) {
265 3
                    $container['doctrine.dbal.dbs.default'] = $name;
266
                }
267
            }
268
269 3
            $container['doctrine.dbal.dbs.options'] = $tmp;
270 3
        });
271
    }
272
}
273