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

RandomConnector::connectTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
ccs 6
cts 6
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Connections\Connector;
4
5
use Doctrine\DBAL\Driver\Connection as DriverConnection;
6
use function array_rand;
7
8
/**
9
 * Random connector is connecting to a random slave connection
10
 */
11
class RandomConnector extends AbstractMasterSlaveConnector implements Connector
12
{
13
    public const STRATEGY = 'random';
14
15
    /**
16
     * Connects to a specific connection.
17
     *
18
     * @param string $connectionName
19
     *
20
     * @return DriverConnection
21
     */
22 181
    public function connectTo($connectionName)
23
    {
24 181
        $params = $this->_params;
25
26 181
        $config = $params['master'];
27
28 181
        if ($connectionName !== 'master') {
29 181
            $config = $params['slaves'][array_rand($params['slaves'])];
30
        }
31
32 181
        return $this->connectWithParams($config);
33
    }
34
}
35