Failed Conditions
Pull Request — master (#3892)
by David
61:44
created

Psr11ConnectionRegistry::getConnections()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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