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

ConnectorFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 1
b 0
f 0
dl 0
loc 28
rs 10
ccs 7
cts 7
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 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