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

ConnectorFactory::create()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
rs 9.6111
ccs 7
cts 7
cp 1
cc 5
nc 6
nop 2
crap 5
1
<?php
2
3
namespace Doctrine\DBAL\Connections;
4
5
use Doctrine\DBAL\Connections\Connector\Connector;
6
use Doctrine\DBAL\Connections\Connector\HighAvailabilityConnector;
7
use Doctrine\DBAL\Connections\Connector\RandomConnector;
8
use Doctrine\DBAL\Driver;
9
use Doctrine\DBAL\Exception\ConnectorException;
10
use function is_a;
11
12
/**
13
 * Creates connector classes based on strategy
14
 */
15
class ConnectorFactory
16
{
17
    /** @var array */
18
    protected static $connectors = [
19
        RandomConnector::class => RandomConnector::STRATEGY,
20
        HighAvailabilityConnector::class => HighAvailabilityConnector::STRATEGY,
21
    ];
22
23
    /**
24
     * @param array $params
25
     *
26
     * @return Connector
27
     *
28
     * @throws ConnectorException If no matching strategy is found
29
     */
30 228
    public static function create(array $params, Driver $driver)
31
    {
32 228
        if (! isset($params['strategy'])) {
33 179
            $params['strategy'] = RandomConnector::STRATEGY;
34
        }
35
36 228
        foreach (self::$connectors as $class => $strategy) {
37 228
            if ($strategy === $params['strategy'] && is_a($class, Connector::class, true)) {
38 227
                return new $class($params, $driver);
39
            }
40
        }
41
42 155
        throw new ConnectorException('No connector for strategy ' . $params['strategy'] . ' found.');
43
    }
44
}
45