Passed
Push — master ( de4fd9...38d8bf )
by Dominik
03:06
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
/*
6
 * (c) Fabien Potencier <[email protected]> (https://github.com/silexphp/Silex-Providers)
7
 */
8
9
namespace Chubbyphp\ServiceProvider;
10
11
use Chubbyphp\ServiceProvider\Logger\DoctrineDbalLogger;
12
use Doctrine\Common\Cache\ApcuCache;
13
use Doctrine\Common\Cache\ArrayCache;
14
use Doctrine\Common\EventManager;
15
use Doctrine\DBAL\Configuration;
16
use Doctrine\DBAL\DriverManager;
17
use Pimple\Container;
18
use Pimple\ServiceProviderInterface;
19
20
final class DoctrineDbalServiceProvider implements ServiceProviderInterface
21
{
22
    /**
23
     * @param Container $container
24
     */
25 3
    public function register(Container $container)
26
    {
27 3
        $container['doctrine.dbal.db.default_options'] = $this->getDbDefaultOptions();
28 3
        $container['doctrine.dbal.dbs.options.initializer'] = $this->getDbsOptionsInitializerDefinition($container);
29 3
        $container['doctrine.dbal.dbs'] = $this->getDbsDefinition($container);
30 3
        $container['doctrine.dbal.dbs.config'] = $this->getDbsConfigDefinition($container);
31 3
        $container['doctrine.dbal.dbs.event_manager'] = $this->getDbsEventManagerDefinition($container);
32 3
        $container['doctrine.dbal.db'] = $this->getDbDefinition($container);
33 3
        $container['doctrine.dbal.db.config'] = $this->getDbConfigDefinition($container);
34 3
        $container['doctrine.dbal.db.event_manager'] = $this->getDbEventManagerDefinition($container);
35 3
        $container['doctrine.dbal.db.cache_factory.array'] = $this->getDbArrayCacheFactoryDefinition($container);
36 3
        $container['doctrine.dbal.db.cache_factory.apcu'] = $this->getDbApcuCacheFactoryDefinition($container);
37 3
    }
38
39
    /**
40
     * @return array
41
     */
42 3
    private function getDbDefaultOptions(): array
43
    {
44
        return [
45 3
            'connection' => [
46
                'driver' => 'pdo_mysql',
47
                'dbname' => null,
48
                'host' => 'localhost',
49
                'user' => 'root',
50
                'password' => null,
51
            ],
52
            'configuration' => [
53
                'result_cache' => null,
54
                'filter_schema_assets_expression' => null,
55
                'auto_commit' => true,
56
            ],
57
        ];
58
    }
59
60
    /**
61
     * @param Container $container
62
     *
63
     * @return callable
64
     */
65 View Code Duplication
    private function getDbsOptionsInitializerDefinition(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...
66
    {
67 3
        return $container->protect(function () use ($container) {
68 3
            static $initialized = false;
69
70 3
            if ($initialized) {
71 3
                return;
72
            }
73
74 3
            $initialized = true;
75
76 3
            if (!isset($container['doctrine.dbal.dbs.options'])) {
77 2
                $container['doctrine.dbal.dbs.options'] = [
78 2
                    'default' => $container['doctrine.dbal.db.options'] ?? [],
79
                ];
80
            }
81
82 3
            $tmp = $container['doctrine.dbal.dbs.options'];
83 3
            foreach ($tmp as $name => &$options) {
84 3
                $options = array_replace_recursive($container['doctrine.dbal.db.default_options'], $options);
85
86 3
                if (!isset($container['doctrine.dbal.dbs.default'])) {
87 3
                    $container['doctrine.dbal.dbs.default'] = $name;
88
                }
89
            }
90
91 3
            $container['doctrine.dbal.dbs.options'] = $tmp;
92 3
        });
93
    }
94
95
    /**
96
     * @param Container $container
97
     *
98
     * @return callable
99
     */
100
    private function getDbsDefinition(Container $container): callable
101
    {
102 3
        return function () use ($container) {
103 3
            $container['doctrine.dbal.dbs.options.initializer']();
104
105 3
            $dbs = new Container();
106 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
107 3
                if ($container['doctrine.dbal.dbs.default'] === $name) {
108
                    // we use shortcuts here in case the default has been overridden
109 3
                    $config = $container['doctrine.dbal.db.config'];
110 3
                    $manager = $container['doctrine.dbal.db.event_manager'];
111
                } else {
112 1
                    $config = $container['doctrine.dbal.dbs.config'][$name];
113 1
                    $manager = $container['doctrine.dbal.dbs.event_manager'][$name];
114
                }
115
116 3
                $dbs[$name] = function () use ($options, $config, $manager) {
117 3
                    return DriverManager::getConnection($options['connection'], $config, $manager);
118 3
                };
119
            }
120
121 3
            return $dbs;
122 3
        };
123
    }
124
125
    /**
126
     * @param Container $container
127
     *
128
     * @return callable
129
     */
130
    private function getDbsConfigDefinition(Container $container): callable
131
    {
132 3
        return function () use ($container) {
133 3
            $container['doctrine.dbal.dbs.options.initializer']();
134
135 3
            $addLogger = $container['logger'] ?? false;
136
137 3
            $configs = new Container();
138 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
139 3
                $configs[$name] = function () use ($addLogger, $container, $name, $options) {
140 3
                    $configOptions = $options['configuration'];
141
142 3
                    $config = new Configuration();
143
144 3
                    if ($addLogger) {
145 2
                        $config->setSQLLogger(new DoctrineDbalLogger($container['logger']));
146
                    }
147
148 3
                    if (null !== $configOptions['result_cache']) {
149 2
                        $cacheFactoryKey = sprintf('doctrine.dbal.db.cache_factory.%s', $configOptions['result_cache']);
150 2
                        $config->setResultCacheImpl($container[$cacheFactoryKey]);
151
                    }
152
153 3
                    $config->setFilterSchemaAssetsExpression($configOptions['filter_schema_assets_expression']);
154 3
                    $config->setAutoCommit($configOptions['auto_commit']);
155
156 3
                    return $config;
157 3
                };
158
            }
159
160 3
            return $configs;
161 3
        };
162
    }
163
164
    /**
165
     * @param Container $container
166
     *
167
     * @return callable
168
     */
169 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...
170
    {
171 3
        return function () use ($container) {
172 3
            $container['doctrine.dbal.dbs.options.initializer']();
173
174 3
            $managers = new Container();
175 3
            foreach ($container['doctrine.dbal.dbs.options'] as $name => $options) {
176 3
                $managers[$name] = function () {
177 3
                    return new EventManager();
178
                    // todo: check for set/add methods to implement
179 3
                };
180
            }
181
182 3
            return $managers;
183 3
        };
184
    }
185
186
    /***
187
     * @param Container $container
188
     * @return callable
189
     */
190
    private function getDbDefinition(Container $container): callable
191
    {
192 3
        return function () use ($container) {
193 2
            $dbs = $container['doctrine.dbal.dbs'];
194
195 2
            return $dbs[$container['doctrine.dbal.dbs.default']];
196 3
        };
197
    }
198
199
    /***
200
     * @param Container $container
201
     * @return callable
202
     */
203
    private function getDbConfigDefinition(Container $container): callable
204
    {
205 3
        return function () use ($container) {
206 3
            $dbs = $container['doctrine.dbal.dbs.config'];
207
208 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
209 3
        };
210
    }
211
212
    /***
213
     * @param Container $container
214
     * @return callable
215
     */
216
    private function getDbEventManagerDefinition(Container $container): callable
217
    {
218 3
        return function () use ($container) {
219 3
            $dbs = $container['doctrine.dbal.dbs.event_manager'];
220
221 3
            return $dbs[$container['doctrine.dbal.dbs.default']];
222 3
        };
223
    }
224
225
    /**
226
     * @param Container $container
227
     *
228
     * @return callable
229
     */
230
    private function getDbArrayCacheFactoryDefinition(Container $container): callable
231
    {
232 3
        return $container->factory(function () use ($container) {
233 1
            return new ArrayCache();
234 3
        });
235
    }
236
237
    /**
238
     * @param Container $container
239
     *
240
     * @return callable
241
     */
242
    private function getDbApcuCacheFactoryDefinition(Container $container): callable
243
    {
244 3
        return $container->factory(function () use ($container) {
245 1
            return new ApcuCache();
246 3
        });
247
    }
248
}
249