Passed
Push — master ( ef3c0e...cf8c5e )
by Jonathan-Paul
08:47
created

getArgConnectionAuthority()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * MongoDB service provider for the Silex micro-framework.
4
 *
5
 * @see http://php.net/manual/set.mongodb.php
6
 */
7
8
namespace LiquidBox\Silex\Provider;
9
10
use MongoDB;
11
use Pimple\Container;
12
use Pimple\ServiceProviderInterface;
13
14
/**
15
 * MongoDb Provider.
16
 *
17
 * @author Jonathan-Paul Marois <[email protected]>
18
 */
19
class MongoDbServiceProvider implements ServiceProviderInterface
20
{
21
    /**
22
     * @var array Buffer.
23
     */
24
    private $args = array();
25
26
    /**
27
     * @var array
28
     */
29
    private $defaultArgs = array(
30
        'uri_options' => array(),
31
        'driver_options' => array(),
32
    );
33
34
    /**
35
     * @var int|string
36
     */
37
    private $defaultConnection = 0;
38
39
    /**
40
     * @var string
41
     */
42
    private $id = 'mongodb';
43
44
    /**
45
     * @var string
46
     */
47
    private $instances = 'mongodb.clients';
48
49
    /**
50
     * @var bool
51
     */
52
    private $isLoaded = false;
53
54
    /**
55
     * @var array
56
     */
57
    private $parameters = array();
58
59
    /**
60
     * @param string $id
61
     * @param string $instances
62
     */
63 6
    public function __construct($id = null, $instances = null)
64
    {
65 6
        if (strlen($id)) {
66 1
            $this->id = $id;
67 1
        }
68 6
        if (strlen($instances)) {
69 1
            $this->instances = $instances;
70 1
        }
71 6
    }
72
73
    /**
74
     * @return string
75
     */
76 6
    private function buildConnectionString(array $connection)
77
    {
78 6
        if (isset($connection[0]) && count($connection) == 1) {
79 1
            return '//' . $this->sanitizeUri($connection[0]);
80
        }
81
82 5
        $uri = '//' . $this->getArgConnectionAuthority($connection);
83
84 5
        if (!empty($connection['db'])) {
85 2
            $uri .= '/' . $connection['db'];
86
87 2
            if (!empty($connection['options'])) {
88
                $uri .= '?' . http_build_query($connection['options']);
89
            }
90 5
        } elseif (!empty($connection['options'])) {
91 1
            $uri .= '/?' . http_build_query($connection['options']);
92 1
        }
93
94 5
        return $uri;
95
    }
96
97
    /**
98
     * @param array|string $connection
99
     *
100
     * @return string
101
     */
102 6
    private function buildUri($connection)
103
    {
104 6
        return 'mongodb:' . $this->buildConnectionString(is_array($connection) ? $connection : array($connection));
105
    }
106
107
    /**
108
     * @param \Pimple\Container $app
109
     * @param string            $name
110
     *
111
     * @return string
112
     */
113 6
    private function getArg(Container $app, $name)
114
    {
115 6
        return isset($this->args[$name]) ? $this->args[$name] : $this->getDefaultArg($app, $name);
116
    }
117
118 5
    private function getArgConnectionAuthority(array $connection)
119
    {
120 5
        $authority = '';
121
122 5
        if (!empty($connection['user'])) {
123 2
            $authority .= $connection['user'] . ':' . rawurlencode($connection['pwd']) . '@';
124 2
        }
125
126 5
        $authority .= $this->getArgConnectionHost($connection);
127
128 5
        return $authority;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 5
    private function getArgConnectionHost(array $connection = array())
135
    {
136 5
        if (empty($connection['host'])) {
137 5
            $connection['host'] = '127.0.0.1';
138 5
        }
139
140 5
        return empty($connection['port']) ? $connection['host'] : $connection['host'] . ':' . $connection['port'];
141
    }
142
143
    private function getArgConnectionHosts(array $connection)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
144
    {
145
        if (empty($connection['hosts'])) {
146
            return getArgConnectionHost($connection);
147
        }
148
149
        return implode(',', array_map(function ($connection) {
150
            return is_array($connection) ? getArgConnectionHost($connection) : $host;
0 ignored issues
show
Bug introduced by
The variable $host does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
151
        }, $connection['hosts']));
152
    }
153
154
    /**
155
     * @return string
156
     */
157 6
    private function getArgUri()
158
    {
159 6
        if (!empty($this->args['uri'])) {
160 1
            return $this->args['uri'];
161 5
        } elseif (!empty($this->args['connection'])) {
162 3
            return $this->args['connection'];
163
        }
164 2
    }
165
166
    /**
167
     * @param \Pimple\Container $app
168
     * @param string            $name
169
     *
170
     * @return string|array
171
     */
172 6
    private function getDefaultArg(Container $app, $name)
173
    {
174 6
        if (!isset($this->defaultArgs[$name])) {
175
            $this->setDefaultArg($app, $name);
176
        }
177
178 6
        return $this->defaultArgs[$name];
179
    }
180
181 6
    private function loadParameters(Container $app)
182
    {
183 6
        if ($this->isLoaded) {
184 6
            return;
185
        }
186
187 6
        $this->isLoaded = true;
188
189 6
        if (empty($app['mongodb.uri']) || !is_array($app['mongodb.uri'])) {
190 5
            $this->loadSingletonParameters($app);
191 5
        } else {
192 1
            $this->parameters = $app['mongodb.uri'];
193 1
            $this->defaultConnection = array_keys($this->parameters)[0];
194
        }
195 6
    }
196
197 5
    private function loadSingletonParameters(Container $app)
198
    {
199 5
        $this->parameters[0] = array();
200
201 5
        if (!empty($app['mongodb.uri'])) {
202 1
            $this->parameters[0]['uri'] = $app['mongodb.uri'];
203 5
        } elseif (!empty($app['mongodb.connection'])) {
204 2
            $this->parameters[0]['connection'] = $app['mongodb.connection'];
205 2
        }
206
207 5
        if (!empty($app['mongodb.uri_options'])) {
208
            $this->parameters[0]['uri_options'] = $app['mongodb.uri_options'];
209
        }
210 5
        if (!empty($app['mongodb.driver_options'])) {
211
            $this->parameters[0]['driver_options'] = $app['mongodb.driver_options'];
212
        }
213 5
    }
214
215
    /**
216
     * @param string $uri
217
     *
218
     * @return string
219
     */
220 1
    private function sanitizeUri($uri)
221
    {
222 1
        if ('mongodb:' == substr($uri, 0, 8)) {
223 1
            $uri = substr($uri, 8);
224 1
        }
225
226 1
        return ('//' == substr($uri, 0, 2)) ? substr($uri, 2) : $uri;
227
    }
228
229
    /**
230
     * @param \Pimple\Container $app
231
     * @param string            $name
232
     */
233
    private function setDefaultArg(Container $app, $name)
234
    {
235
        $this->defaultArgs[$name] = empty($app['mongodb.' . $name]) ?
236
            array('uri_options' => array(), 'driver_options' => array())[$name] :
237
            $app['mongodb.' . $name];
238
    }
239
240 6
    public function register(Container $app)
241
    {
242
        $app[$this->id] = function () use ($app) {
243 6
            $this->loadParameters($app);
244
245 6
            return $app[$this->instances][$this->defaultConnection];
246
        };
247
        $app[$this->instances] = function () use ($app) {
248 6
            $this->loadParameters($app);
249
250 6
            $instances = new Container();
251 6
            foreach ($this->parameters as $client => $this->args) {
252
                $instances[$client] = function () use ($app) {
253 6
                    return $app['mongodb.client'](
254 6
                        $this->getArgUri(),
255 6
                        $this->getArg($app, 'uri_options'),
256 6
                        $this->getArg($app, 'driver_options')
257 6
                    );
258
                };
259 6
            }
260
261 6
            return $instances;
262
        };
263
264 6
        $app['mongodb.client'] = $app->protect(
265 6
            function ($uri = null, array $uriOptions = array(), array $driverOptions = array()) {
266 6
                return new MongoDB\Client($this->buildUri($uri), $uriOptions, $driverOptions);
267
            }
268 6
        );
269 6
    }
270
}
271