1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\DoctrineDbServiceProvider\Registry; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ConnectionRegistry; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
|
11
|
|
|
final class DoctrineDbalConnectionRegistry implements ConnectionRegistry |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Container |
15
|
|
|
*/ |
16
|
|
|
private $container; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Container |
20
|
|
|
*/ |
21
|
|
|
private $connections; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $defaultConnectionName; |
27
|
|
|
|
28
|
5 |
|
public function __construct(Container $container) |
29
|
|
|
{ |
30
|
5 |
|
$this->container = $container; |
31
|
5 |
|
} |
32
|
|
|
|
33
|
3 |
|
public function getDefaultConnectionName(): string |
34
|
|
|
{ |
35
|
3 |
|
$this->loadConnections(); |
36
|
|
|
|
37
|
3 |
|
return $this->defaultConnectionName; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string|null $name |
42
|
|
|
* |
43
|
|
|
* @throws \InvalidArgumentException |
44
|
|
|
* |
45
|
|
|
* @return Connection |
46
|
|
|
*/ |
47
|
2 |
View Code Duplication |
public function getConnection($name = null): Connection |
|
|
|
|
48
|
|
|
{ |
49
|
2 |
|
$this->loadConnections(); |
50
|
|
|
|
51
|
2 |
|
$name = $name ?? $this->getDefaultConnectionName(); |
52
|
|
|
|
53
|
2 |
|
if (!isset($this->connections[$name])) { |
54
|
1 |
|
throw new \InvalidArgumentException(sprintf('Missing connection with name "%s".', $name)); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return $this->connections[$name]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array<string, Connection> |
|
|
|
|
62
|
|
|
*/ |
63
|
1 |
View Code Duplication |
public function getConnections(): array |
|
|
|
|
64
|
|
|
{ |
65
|
1 |
|
$this->loadConnections(); |
66
|
|
|
|
67
|
1 |
|
$connections = []; |
68
|
|
|
/** @var string $name */ |
69
|
1 |
|
foreach ($this->connections->keys() as $name) { |
70
|
|
|
/** @var Connection $connection */ |
71
|
1 |
|
$connection = $this->connections[$name]; |
72
|
1 |
|
$connections[$name] = $connection; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return $connections; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array<string> |
80
|
|
|
*/ |
81
|
1 |
|
public function getConnectionNames(): array |
82
|
|
|
{ |
83
|
1 |
|
$this->loadConnections(); |
84
|
|
|
|
85
|
1 |
|
return $this->connections->keys(); |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
View Code Duplication |
private function loadConnections(): void |
|
|
|
|
89
|
|
|
{ |
90
|
5 |
|
if (null === $this->connections) { |
91
|
5 |
|
$this->connections = $this->container['doctrine.dbal.dbs']; |
92
|
5 |
|
$this->defaultConnectionName = $this->container['doctrine.dbal.dbs.default']; |
93
|
|
|
} |
94
|
5 |
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.