Completed
Pull Request — master (#35)
by Mathieu
02:03
created

MasterSlavesDriver::getSchemaManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ez\DbLinker\Driver;
4
5
use \Doctrine\DBAL\DriverManager;
6
use Ez\DbLinker\Driver\Connection\MasterSlavesConnection;
7
use SplObjectStorage;
8
9
trait MasterSlavesDriver
10
{
11
    use WrapperDriver;
12
13
    /**
14
     * Attempts to create a connection with the database.
15
     *
16
     * @param array       $params        All connection parameters passed by the user.
17
     * @param string|null $username      The username to use when connecting.
18
     * @param string|null $password      The password to use when connecting.
19
     * @param array       $driverOptions The driver options to use when connecting.
20
     *
21
     * @return \Doctrine\DBAL\Driver\Connection The database connection.
22
     */
23
    public function connect(Array $params, $username = null, $password = null, Array $driverOptions = []) {
3 ignored issues
show
Unused Code introduced by
The parameter $username is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $password is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $driverOptions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
        $driverKey = array_key_exists('driverClass', $params['master']) ? 'driverClass' : 'driver';
25
        $driverValue = $params['master'][$driverKey];
26
        $master = function () use ($params) {
27
            return DriverManager::getConnection($params['master']);
28
        };
29
        $slaves = new SplObjectStorage;
30
        foreach ($params['slaves'] as $slaveParams) {
31
            $slaveParams[$driverKey] = $driverValue;
32
            $slaves->attach(function () use ($slaveParams) {
33
                return DriverManager::getConnection($slaveParams);
34
            }, $slaveParams['weight']);
35
        }
36
        return new MasterSlavesConnection($master, $slaves);
37
    }
38
}
39