Total Complexity | 7 |
Total Lines | 57 |
Duplicated Lines | 0 % |
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 | 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 |
||
68 | } |
||
69 | } |
||
70 |