Passed
Pull Request — master (#3892)
by David
65:26
created

Psr11ConnectionRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL;
6
7
use InvalidArgumentException;
8
use Psr\Container\ContainerInterface;
9
use function sprintf;
10
11
class Psr11ConnectionRegistry implements ConnectionRegistry
12
{
13
    /** @var ContainerInterface */
14
    private $container;
15
16
    /** @var string */
17
    private $defaultConnectionName;
18
19
    /** @var string[] */
20
    private $connectionNames;
21
22
    /**
23
     * @param string[] $connectionNames
24
     */
25
    public function __construct(ContainerInterface $container, string $defaultConnectionName, array $connectionNames)
26
    {
27
        $this->container             = $container;
28
        $this->defaultConnectionName = $defaultConnectionName;
29
        $this->connectionNames       = $connectionNames;
30
    }
31
32
    public function getDefaultConnectionName() : string
33
    {
34
        return $this->defaultConnectionName;
35
    }
36
37
    public function getConnection(?string $name = null) : Connection
38
    {
39
        $name = $name ?? $this->defaultConnectionName;
40
41
        if (! $this->container->has($name)) {
42
            throw new InvalidArgumentException(sprintf('Connection with name "%s" does not exist.', $name));
43
        }
44
45
        return $this->container->get($name);
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function getConnections() : array
52
    {
53
        $connections = [];
54
55
        foreach ($this->connectionNames as $connectionName) {
56
            $connections[$connectionName] = $this->container->get($connectionName);
57
        }
58
59
        return $connections;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function getConnectionNames() : array
66
    {
67
        return $this->connectionNames;
68
    }
69
}
70