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

getMongoDbsOptionsInitializerDefinition()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 29

Duplication

Lines 29
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 29
loc 29
ccs 15
cts 15
cp 1
rs 8.8337
c 0
b 0
f 0
cc 6
nc 1
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ServiceProvider\ServiceProvider;
6
7
use Chubbyphp\ServiceProvider\Logger\DoctrineMongoLogger;
8
use Doctrine\Common\EventManager;
9
use Doctrine\MongoDB\Configuration;
10
use Doctrine\MongoDB\Connection;
11
use Pimple\Container;
12
use Pimple\ServiceProviderInterface;
13
14
final class DoctrineMongoServiceProvider implements ServiceProviderInterface
15
{
16
    /**
17
     * @param Container $container
18
     */
19 3
    public function register(Container $container)
20
    {
21 3
        $container['doctrine.mongo.db.default_options'] = $this->getMongoDbDefaultOptions();
22 3
        $container['doctrine.mongo.dbs.options.initializer'] = $this->getMongoDbsOptionsInitializerDefinition($container);
23 3
        $container['doctrine.mongo.dbs'] = $this->getMongoDbsDefinition($container);
24 3
        $container['doctrine.mongo.dbs.config'] = $this->getMongoDbsConfigDefinition($container);
25 3
        $container['doctrine.mongo.dbs.event_manager'] = $this->getMongoDbsEventManagerDefinition($container);
26 3
        $container['doctrine.mongo.db'] = $this->getMongoDbDefinition($container);
27 3
        $container['doctrine.mongo.db.config'] = $this->getMongoDbConfigDefinition($container);
28 3
        $container['doctrine.mongo.db.event_manager'] = $this->getMongoDbEventManagerDefinition($container);
29 3
        $container['doctrine.mongo.db.logger.batch_insert_threshold'] = 10;
30 3
        $container['doctrine.mongo.db.logger.prefix'] = 'MongoDB query: ';
31 3
    }
32
33
    /**
34
     * @return array
35
     */
36 3
    private function getMongoDbDefaultOptions(): array
37
    {
38
        return [
39 3
            'server' => 'mongodb://localhost:27017',
40
            'options' => [],
41
            /* @link http://www.php.net/manual/en/mongoclient.construct.php */
42
        ];
43
    }
44
45
    /**
46
     * @param Container $container
47
     *
48
     * @return callable
49
     */
50 View Code Duplication
    private function getMongoDbsOptionsInitializerDefinition(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...
51
    {
52 3
        return $container->protect(function () use ($container) {
53 3
            static $initialized = false;
54
55 3
            if ($initialized) {
56 3
                return;
57
            }
58
59 3
            $initialized = true;
60
61 3
            if (!isset($container['doctrine.mongo.dbs.options'])) {
62 2
                $container['doctrine.mongo.dbs.options'] = [
63 2
                    'default' => isset($container['doctrine.mongo.db.options']) ? $container['doctrine.mongo.db.options'] : [],
64
                ];
65
            }
66
67 3
            $tmp = $container['doctrine.mongo.dbs.options'];
68 3
            foreach ($tmp as $name => &$options) {
69 3
                $options = array_replace_recursive($container['doctrine.mongo.db.default_options'], $options);
70
71 3
                if (!isset($container['doctrine.mongo.dbs.default'])) {
72 3
                    $container['doctrine.mongo.dbs.default'] = $name;
73
                }
74
            }
75
76 3
            $container['doctrine.mongo.dbs.options'] = $tmp;
77 3
        });
78
    }
79
80
    /**
81
     * @param Container $container
82
     *
83
     * @return callable
84
     */
85
    private function getMongoDbsDefinition(Container $container): callable
86
    {
87 3
        return function () use ($container) {
88 3
            $container['doctrine.mongo.dbs.options.initializer']();
89
90 3
            $mongodbs = new Container();
91 3
            foreach ($container['doctrine.mongo.dbs.options'] as $name => $options) {
92 3
                if ($container['doctrine.mongo.dbs.default'] === $name) {
93
                    // we use shortcuts here in case the default has been overridden
94 3
                    $config = $container['doctrine.mongo.db.config'];
95 3
                    $manager = $container['doctrine.mongo.db.event_manager'];
96
                } else {
97 1
                    $config = $container['doctrine.mongo.dbs.config'][$name];
98 1
                    $manager = $container['doctrine.mongo.dbs.event_manager'][$name];
99
                }
100
101 3
                $mongodbs[$name] = function () use ($options, $config, $manager) {
102 3
                    return new Connection($options['server'], $options['options'], $config, $manager);
103 3
                };
104
            }
105
106 3
            return $mongodbs;
107 3
        };
108
    }
109
110
    /**
111
     * @param Container $container
112
     *
113
     * @return callable
114
     */
115
    private function getMongoDbsConfigDefinition(Container $container): callable
116
    {
117 3
        return function () use ($container) {
118 3
            $container['doctrine.mongo.dbs.options.initializer']();
119
120 3
            $addLogger = $container['logger'] ?? false;
121
122 3
            $configs = new Container();
123 3
            foreach ($container['doctrine.mongo.dbs.options'] as $name => $options) {
124 3
                $configs[$name] = function () use ($addLogger, $container) {
125 3
                    $config = new Configuration();
126 3
                    if ($addLogger) {
127 2
                        $logger = new DoctrineMongoLogger(
128 2
                            $container['logger'],
129 2
                            $container['doctrine.mongo.db.logger.batch_insert_threshold'],
130 2
                            $container['doctrine.mongo.db.logger.prefix']
131
                        );
132 2
                        $config->setLoggerCallable([$logger, 'logQuery']);
133
                    }
134
135 3
                    return $config;
136 3
                };
137
            }
138
139 3
            return $configs;
140 3
        };
141
    }
142
143
    /**
144
     * @param Container $container
145
     *
146
     * @return callable
147
     */
148 View Code Duplication
    private function getMongoDbsEventManagerDefinition(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...
149
    {
150 3
        return function () use ($container) {
151 3
            $container['doctrine.mongo.dbs.options.initializer']();
152
153 3
            $managers = new Container();
154 3
            foreach ($container['doctrine.mongo.dbs.options'] as $name => $options) {
155 3
                $managers[$name] = function () {
156 3
                    return new EventManager();
157 3
                };
158
            }
159
160 3
            return $managers;
161 3
        };
162
    }
163
164
    /***
165
     * @param Container $container
166
     * @return callable
167
     */
168
    private function getMongoDbDefinition(Container $container): callable
169
    {
170 3
        return function () use ($container) {
171 2
            $dbs = $container['doctrine.mongo.dbs'];
172
173 2
            return $dbs[$container['doctrine.mongo.dbs.default']];
174 3
        };
175
    }
176
177
    /***
178
     * @param Container $container
179
     * @return callable
180
     */
181
    private function getMongoDbConfigDefinition(Container $container): callable
182
    {
183 3
        return function () use ($container) {
184 3
            $dbs = $container['doctrine.mongo.dbs.config'];
185
186 3
            return $dbs[$container['doctrine.mongo.dbs.default']];
187 3
        };
188
    }
189
190
    /***
191
     * @param Container $container
192
     * @return callable
193
     */
194
    private function getMongoDbEventManagerDefinition(Container $container): callable
195
    {
196 3
        return function () use ($container) {
197 3
            $dbs = $container['doctrine.mongo.dbs.event_manager'];
198
199 3
            return $dbs[$container['doctrine.mongo.dbs.default']];
200 3
        };
201
    }
202
}
203