Total Complexity | 7 |
Total Lines | 57 |
Duplicated Lines | 0 % |
Coverage | 73.68% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 | 120 | public function __construct(ContainerInterface $container, string $defaultConnectionName, array $connectionNames) |
|
26 | { |
||
27 | 120 | $this->container = $container; |
|
28 | 120 | $this->defaultConnectionName = $defaultConnectionName; |
|
29 | 120 | $this->connectionNames = $connectionNames; |
|
30 | 120 | } |
|
31 | |||
32 | 24 | public function getDefaultConnectionName() : string |
|
33 | { |
||
34 | 24 | return $this->defaultConnectionName; |
|
35 | } |
||
36 | |||
37 | 72 | public function getConnection(?string $name = null) : Connection |
|
38 | { |
||
39 | 72 | $name = $name ?? $this->defaultConnectionName; |
|
40 | |||
41 | 72 | if (! $this->container->has($name)) { |
|
42 | 24 | throw new InvalidArgumentException(sprintf('Connection with name "%s" does not exist.', $name)); |
|
43 | } |
||
44 | |||
45 | 48 | 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 | 24 | public function getConnectionNames() : array |
|
68 | } |
||
69 | } |
||
70 |