Completed
Pull Request — 2.11.x (#2448)
by
unknown
12:14
created

AbstractMasterSlaveConnector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 50
rs 10
ccs 14
cts 14
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A connectWithParams() 0 10 1
A __construct() 0 4 1
A updateFromMasterConnfiguration() 0 7 3
1
<?php
2
3
namespace Doctrine\DBAL\Connections\Connector;
4
5
use Doctrine\DBAL\Driver;
6
use Doctrine\DBAL\Driver\Connection as DriverConnection;
7
8
abstract class AbstractMasterSlaveConnector implements Connector
9
{
10
    /** @var array */
11
    protected $_params;
12
13
    /** @var Driver */
14
    protected $_driver;
15
16
    /**
17
     * Creates HighAvailabilityConnector.
18
     *
19
     * @param array $params
20
     */
21 373
    public function __construct(array $params, Driver $driver)
22
    {
23 373
        $this->_params = $params;
24 373
        $this->_driver = $driver;
25 373
    }
26
27
    /**
28
     * Connects with arbitrary params to connection.
29
     *
30
     * @param array $connectionParams
31
     *
32
     * @return DriverConnection
33
     */
34 369
    protected function connectWithParams($connectionParams)
35
    {
36 369
        $driverOptions = $this->_params['driverOptions'] ?? [];
37
38 369
        $connectionParams = $this->updateFromMasterConnfiguration($connectionParams);
39
40 369
        $user     = $connectionParams['user'] ?? null;
41 369
        $password = $connectionParams['password'] ?? null;
42
43 369
        return $this->_driver->connect($connectionParams, $user, $password, $driverOptions);
44
    }
45
46
    /**
47
     * @param array $config
48
     *
49
     * @return array
50
     */
51 369
    protected function updateFromMasterConnfiguration($config)
52
    {
53 369
        if (! isset($config['charset']) && isset($this->_params['master']['charset'])) {
54 108
            $config['charset'] = $this->_params['master']['charset'];
55
        }
56
57 369
        return $config;
58
    }
59
}
60