Passed
Push — type-registry ( f9a1df...0931f1 )
by Michael
24:09
created

DriverManager   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 405
Duplicated Lines 0 %

Test Coverage

Coverage 94%

Importance

Changes 0
Metric Value
wmc 44
eloc 110
dl 0
loc 405
ccs 94
cts 100
cp 0.94
rs 8.8798
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A parseSqliteDatabaseUrlPath() 0 11 2
A getAvailableDrivers() 0 3 1
B parseDatabaseUrl() 0 41 7
A parseDatabaseUrlQuery() 0 11 2
A parseDatabaseUrlPath() 0 19 4
B _checkParams() 0 18 7
A normalizeDatabaseUrlPath() 0 4 1
A parseDatabaseUrlScheme() 0 27 4
F getConnection() 0 63 14
A parseRegularDatabaseUrlPath() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like DriverManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DriverManager, and based on these observations, apply Extract Interface, too.

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