Completed
Push — master ( 46d273...229a5a )
by Mathieu
02:04
created

MasterSlavesDriver::wrappedDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Ez\DbLinker\Driver;
4
5
use Ez\DbLinker\Driver\Connection\MasterSlavesConnection;
6
7
trait MasterSlavesDriver
8
{
9
    use WrapperDriver;
10
11
    /**
12
     * Attempts to create a connection with the database.
13
     *
14
     * @param array       $params        All connection parameters passed by the user.
15
     * @param string|null $username      The username to use when connecting.
16
     * @param string|null $password      The password to use when connecting.
17
     * @param array       $driverOptions The driver options to use when connecting.
18
     *
19
     * @return \Doctrine\DBAL\Driver\Connection The database connection.
20
     */
21
    public function connect(Array $params, $username = null, $password = null, Array $driverOptions = []) {
22
        $driverKey = array_key_exists('driverClass', $params['master']) ? 'driverClass' : 'driver';
23
        $driverValue = $params['master'][$driverKey];
24
        $slaves = [];
25
        foreach ($params['slaves'] as $slaveParams) {
26
            $slaveParams[$driverKey] = $driverValue;
27
            $slaves[] = $slaveParams;
28
        }
29
        return new MasterSlavesConnection($params['master'], $slaves);
30
    }
31
}
32