Completed
Pull Request — 2.11.x (#3956)
by David
73:44 queued 11:39
created

ArrayConnectionProvider::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Doctrine\DBAL\Tools\Console\ConnectionProvider;
4
5
use Doctrine\DBAL\Connection;
6
use InvalidArgumentException;
7
use function sprintf;
8
9
class ArrayConnectionProvider implements ConnectionProvider
10
{
11
    /** @var array<string, Connection> */
12
    private $connections;
13
14
    /** @var string */
15
    private $defaultConnectionName;
16
17
    /**
18
     * @param array<string, Connection> $connections
19
     */
20
    public function __construct(array $connections, string $defaultConnectionName)
21
    {
22
        $this->connections           = $connections;
23
        $this->defaultConnectionName = $defaultConnectionName;
24
    }
25
26
    public static function createFromSingleConnection(Connection $connection, string $name = 'default') : self
27
    {
28
        return new self([$name => $connection], $name);
29
    }
30
31
    public function getDefaultConnection() : Connection
32
    {
33
        return $this->getConnection($this->defaultConnectionName);
34
    }
35
36
    public function getConnection(string $name) : Connection
37
    {
38
        if (! isset($this->connections[$name])) {
39
            throw new InvalidArgumentException(sprintf('Connection with name "%s" does not exist.', $name));
40
        }
41
42
        return $this->connections[$name];
43
    }
44
}
45