Failed Conditions
Pull Request — 2.10 (#3803)
by Sergei
62:38
created

DriverManager::getConnection()   C

Complexity

Conditions 11
Paths 192

Size

Total Lines 53
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 53
ccs 24
cts 24
cp 1
rs 6.55
c 0
b 0
f 0
cc 11
nc 192
nop 3
crap 11

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\DBAL;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySQLDriver;
7
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
8
use Doctrine\DBAL\Driver\Mysqli\Driver as MySQLiDriver;
9
use Doctrine\DBAL\Driver\OCI8\Driver as OCI8Driver;
10
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver;
11
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOCIDriver;
12
use Doctrine\DBAL\Driver\PDOPgSql\Driver as PDOPgSQLDriver;
13
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSQLiteDriver;
14
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSrvDriver;
15
use Doctrine\DBAL\Driver\SQLAnywhere\Driver as SQLAnywhereDriver;
16
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
17
use function array_keys;
18
use function array_map;
19
use function array_merge;
20
use function assert;
21
use function class_implements;
22
use function in_array;
23
use function is_string;
24
use function is_subclass_of;
25
use function parse_str;
26
use function parse_url;
27
use function preg_replace;
28
use function str_replace;
29
use function strpos;
30
use function substr;
31
32
/**
33
 * Factory for creating Doctrine\DBAL\Connection instances.
34
 */
35
final class DriverManager
36
{
37
    /**
38
     * List of supported drivers and their mappings to the driver classes.
39
     *
40
     * To add your own driver use the 'driverClass' parameter to
41
     * {@link DriverManager::getConnection()}.
42
     *
43
     * @var string[]
44
     */
45
    private static $_driverMap = [
46
        'pdo_mysql'          => PDOMySQLDriver::class,
47
        'pdo_sqlite'         => PDOSQLiteDriver::class,
48
        'pdo_pgsql'          => PDOPgSQLDriver::class,
49
        'pdo_oci'            => PDOOCIDriver::class,
50
        'oci8'               => OCI8Driver::class,
51
        'ibm_db2'            => DB2Driver::class,
52
        'pdo_sqlsrv'         => PDOSQLSrvDriver::class,
53
        'mysqli'             => MySQLiDriver::class,
54
        'drizzle_pdo_mysql'  => DrizzlePDOMySQLDriver::class,
55
        'sqlanywhere'        => SQLAnywhereDriver::class,
56
        'sqlsrv'             => SQLSrvDriver::class,
57
    ];
58
59
    /**
60
     * List of URL schemes from a database URL and their mappings to driver.
61
     *
62
     * @var string[]
63
     */
64
    private static $driverSchemeAliases = [
65
        'db2'        => 'ibm_db2',
66
        'mssql'      => 'pdo_sqlsrv',
67
        'mysql'      => 'pdo_mysql',
68
        'mysql2'     => 'pdo_mysql', // Amazon RDS, for some weird reason
69
        'postgres'   => 'pdo_pgsql',
70
        'postgresql' => 'pdo_pgsql',
71
        'pgsql'      => 'pdo_pgsql',
72
        'sqlite'     => 'pdo_sqlite',
73
        'sqlite3'    => 'pdo_sqlite',
74
    ];
75
76
    /**
77
     * Private constructor. This class cannot be instantiated.
78
     */
79
    private function __construct()
80
    {
81
    }
82
83
    /**
84
     * Creates a connection object based on the specified parameters.
85
     * This method returns a Doctrine\DBAL\Connection which wraps the underlying
86
     * driver connection.
87
     *
88
     * $params must contain at least one of the following.
89
     *
90
     * Either 'driver' with one of the following values:
91
     *
92
     *     pdo_mysql
93
     *     pdo_sqlite
94
     *     pdo_pgsql
95
     *     pdo_oci (unstable)
96
     *     pdo_sqlsrv
97
     *     pdo_sqlsrv
98
     *     mysqli
99
     *     sqlanywhere
100
     *     sqlsrv
101
     *     ibm_db2 (unstable)
102
     *     drizzle_pdo_mysql
103
     *
104
     * OR 'driverClass' that contains the full class name (with namespace) of the
105
     * driver class to instantiate.
106
     *
107
     * Other (optional) parameters:
108
     *
109
     * <b>user (string)</b>:
110
     * The username to use when connecting.
111
     *
112
     * <b>password (string)</b>:
113
     * The password to use when connecting.
114
     *
115
     * <b>driverOptions (array)</b>:
116
     * Any additional driver-specific options for the driver. These are just passed
117
     * through to the driver.
118
     *
119
     * <b>pdo</b>:
120
     * You can pass an existing PDO instance through this parameter. The PDO
121
     * instance will be wrapped in a Doctrine\DBAL\Connection.
122
     *
123
     * <b>wrapperClass</b>:
124
     * You may specify a custom wrapper class through the 'wrapperClass'
125
     * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
126
     *
127
     * <b>driverClass</b>:
128
     * The driver class to use.
129
     *
130
     * @param mixed[]            $params       The parameters.
131
     * @param Configuration|null $config       The configuration to use.
132
     * @param EventManager|null  $eventManager The event manager to use.
133
     *
134
     * @throws DBALException
135
     */
136
    public static function getConnection(
137 3391
        array $params,
138
        ?Configuration $config = null,
139
        ?EventManager $eventManager = null
140
    ) : Connection {
141
        // create default config and event manager, if not set
142
        if (! $config) {
143 3391
            $config = new Configuration();
144 3377
        }
145
        if (! $eventManager) {
146 3391
            $eventManager = new EventManager();
147 3377
        }
148
149
        $params = self::parseDatabaseUrl($params);
150 3391
151
        // URL support for MasterSlaveConnection
152
        if (isset($params['master'])) {
153 3385
            $params['master'] = self::parseDatabaseUrl($params['master']);
154 1767
        }
155
156
        if (isset($params['slaves'])) {
157 3385
            foreach ($params['slaves'] as $key => $slaveParams) {
158 1767
                $params['slaves'][$key] = self::parseDatabaseUrl($slaveParams);
159 1767
            }
160
        }
161
162
        // URL support for PoolingShardConnection
163
        if (isset($params['global'])) {
164 3385
            $params['global'] = self::parseDatabaseUrl($params['global']);
165 1770
        }
166
167
        if (isset($params['shards'])) {
168 3385
            foreach ($params['shards'] as $key => $shardParams) {
169 1770
                $params['shards'][$key] = self::parseDatabaseUrl($shardParams);
170 1770
            }
171
        }
172
173
        self::_checkParams($params);
174
175 3385
        $className = $params['driverClass'] ?? self::$_driverMap[$params['driver']];
176 2017
177
        $driver = new $className();
178
179 3383
        $wrapperClass = Connection::class;
180 2002
        if (isset($params['wrapperClass'])) {
181 2002
            if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) {
182
                throw DBALException::invalidWrapperClass($params['wrapperClass']);
183 3373
            }
184
185
            $wrapperClass = $params['wrapperClass'];
186 3377
        }
187
188 3377
        return new $wrapperClass($params, $driver, $config, $eventManager);
189
    }
190 3377
191 3377
    /**
192 1913
     * Returns the list of supported drivers.
193 1842
     *
194
     * @return string[]
195
     */
196 1911
    public static function getAvailableDrivers() : array
197
    {
198
        return array_keys(self::$_driverMap);
199 3375
    }
200
201
    /**
202
     * Checks the list of parameters.
203
     *
204
     * @param mixed[] $params The list of parameters.
205
     *
206
     * @throws DBALException
207
     */
208
    private static function _checkParams(array $params) : void
209
    {
210
        // check existence of mandatory parameters
211
212
        // driver
213
        if (! isset($params['driver']) && ! isset($params['driverClass'])) {
214
            throw DBALException::driverRequired();
215
        }
216
217
        // check validity of parameters
218
219 3373
        // driver
220
        if (isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
221
            throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
222
        }
223
224 3373
        if (isset($params['driverClass']) && ! in_array(Driver::class, class_implements($params['driverClass'], true))) {
225 1942
            throw DBALException::invalidDriverClass($params['driverClass']);
226
        }
227
    }
228
229
    /**
230
     * Normalizes the given connection URL path.
231 3371
     *
232 1917
     * @return string The normalized connection URL path
233
     */
234
    private static function normalizeDatabaseUrlPath(string $urlPath) : string
235 3369
    {
236 1817
        // Trim leading slash from URL path.
237
        return substr($urlPath, 1);
238 3367
    }
239
240
    /**
241
     * Extracts parts from a database URL, if present, and returns an
242
     * updated list of parameters.
243
     *
244
     * @param mixed[] $params The list of parameters.
245 1819
     *
246
     * @return mixed[] A modified list of parameters with info from a database
247
     *                 URL extracted into indidivual parameter parts.
248 1819
     *
249
     * @throws DBALException
250
     */
251
    private static function parseDatabaseUrl(array $params) : array
252
    {
253
        if (! isset($params['url'])) {
254
            return $params;
255
        }
256
257
        // (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
258
        $url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $params['url']);
259
        assert(is_string($url));
260
261
        $url = parse_url($url);
262 3391
263
        if ($url === false) {
264 3391
            throw new DBALException('Malformed parameter "url".');
265 3335
        }
266
267
        $url = array_map('rawurldecode', $url);
268
269 1825
        $params = self::parseDatabaseUrlScheme($url, $params);
270 1825
271
        if (isset($url['host'])) {
272 1825
            $params['host'] = $url['host'];
273
        }
274 1825
        if (isset($url['port'])) {
275
            $params['port'] = $url['port'];
276
        }
277
        if (isset($url['user'])) {
278 1825
            $params['user'] = $url['user'];
279
        }
280
        if (isset($url['pass'])) {
281
            $params['password'] = $url['pass'];
282 1825
        }
283
284 1825
        $params = self::parseDatabaseUrlPath($url, $params);
285
        $params = self::parseDatabaseUrlQuery($url, $params);
286 1819
287 1819
        return $params;
288
    }
289 1819
290 1771
    /**
291
     * Parses the given connection URL and resolves the given connection parameters.
292 1819
     *
293 1807
     * Assumes that the connection URL scheme is already parsed and resolved into the given connection parameters
294
     * via {@link parseDatabaseUrlScheme}.
295 1819
     *
296 1807
     * @see parseDatabaseUrlScheme
297
     *
298
     * @param mixed[] $url    The URL parts to evaluate.
299 1819
     * @param mixed[] $params The connection parameters to resolve.
300 1819
     *
301
     * @return mixed[] The resolved connection parameters.
302 1819
     */
303
    private static function parseDatabaseUrlPath(array $url, array $params) : array
304
    {
305
        if (! isset($url['path'])) {
306
            return $params;
307
        }
308
309
        $url['path'] = self::normalizeDatabaseUrlPath($url['path']);
310
311
        // If we do not have a known DBAL driver, we do not know any connection URL path semantics to evaluate
312
        // and therefore treat the path as regular DBAL connection URL path.
313
        if (! isset($params['driver'])) {
314
            return self::parseRegularDatabaseUrlPath($url, $params);
315
        }
316
317
        if (strpos($params['driver'], 'sqlite') !== false) {
318 1819
            return self::parseSqliteDatabaseUrlPath($url, $params);
319
        }
320 1819
321
        return self::parseRegularDatabaseUrlPath($url, $params);
322
    }
323
324 1819
    /**
325
     * Parses the query part of the given connection URL and resolves the given connection parameters.
326
     *
327
     * @param mixed[] $url    The connection URL parts to evaluate.
328 1819
     * @param mixed[] $params The connection parameters to resolve.
329 1417
     *
330
     * @return mixed[] The resolved connection parameters.
331
     */
332 1817
    private static function parseDatabaseUrlQuery(array $url, array $params) : array
333 1677
    {
334
        if (! isset($url['query'])) {
335
            return $params;
336 1805
        }
337
338
        $query = [];
339
340
        parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode
341
342
        return array_merge($params, $query); // parse_str wipes existing array elements
343
    }
344
345
    /**
346
     * Parses the given regular connection URL and resolves the given connection parameters.
347 1819
     *
348
     * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
349 1819
     *
350 1817
     * @see normalizeDatabaseUrlPath
351
     *
352
     * @param mixed[] $url    The regular connection URL parts to evaluate.
353 1592
     * @param mixed[] $params The connection parameters to resolve.
354
     *
355 1592
     * @return mixed[] The resolved connection parameters.
356
     */
357 1592
    private static function parseRegularDatabaseUrlPath(array $url, array $params) : array
358
    {
359
        $params['dbname'] = $url['path'];
360
361
        return $params;
362
    }
363
364
    /**
365
     * Parses the given SQLite connection URL and resolves the given connection parameters.
366
     *
367
     * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
368
     *
369
     * @see normalizeDatabaseUrlPath
370
     *
371
     * @param mixed[] $url    The SQLite connection URL parts to evaluate.
372 1807
     * @param mixed[] $params The connection parameters to resolve.
373
     *
374 1807
     * @return mixed[] The resolved connection parameters.
375
     */
376 1807
    private static function parseSqliteDatabaseUrlPath(array $url, array $params) : array
377
    {
378
        if ($url['path'] === ':memory:') {
379
            $params['memory'] = true;
380
381
            return $params;
382
        }
383
384
        $params['path'] = $url['path']; // pdo_sqlite driver uses 'path' instead of 'dbname' key
385
386
        return $params;
387
    }
388
389
    /**
390
     * Parses the scheme part from given connection URL and resolves the given connection parameters.
391 1677
     *
392
     * @param mixed[] $url    The connection URL parts to evaluate.
393 1677
     * @param mixed[] $params The connection parameters to resolve.
394 1644
     *
395
     * @return mixed[] The resolved connection parameters.
396 1644
     *
397
     * @throws DBALException If parsing failed or resolution is not possible.
398
     */
399 1673
    private static function parseDatabaseUrlScheme(array $url, array $params) : array
400
    {
401 1673
        if (isset($url['scheme'])) {
402
            // The requested driver from the URL scheme takes precedence
403
            // over the default custom driver from the connection parameters (if any).
404
            unset($params['driverClass']);
405
406
            // URL schemes must not contain underscores, but dashes are ok
407
            $driver = str_replace('-', '_', $url['scheme']);
408
            assert(is_string($driver));
409
410
            // The requested driver from the URL scheme takes precedence over the
411
            // default driver from the connection parameters. If the driver is
412
            // an alias (e.g. "postgres"), map it to the actual name ("pdo-pgsql").
413
            // Otherwise, let checkParams decide later if the driver exists.
414 1825
            $params['driver'] = self::$driverSchemeAliases[$driver] ?? $driver;
415
416 1825
            return $params;
417
        }
418
419 1811
        // If a schemeless connection URL is given, we require a default driver or default custom driver
420
        // as connection parameter.
421
        if (! isset($params['driverClass']) && ! isset($params['driver'])) {
422 1811
            throw DBALException::driverRequired($params['url']);
423 1811
        }
424
425
        return $params;
426
    }
427
}
428