Total Complexity | 5 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
13 | final class ConnectionLocatorFactory |
||
14 | { |
||
15 | private function __construct() |
||
16 | { |
||
17 | } |
||
18 | |||
19 | public static function newInstance(string $dsn, string $user, string $password, string $slave): ConnectionLocator |
||
20 | { |
||
21 | $writes = ['master' => new Connection($dsn, $user, $password)]; |
||
22 | $i = 1; |
||
23 | $slaves = explode(',', $slave); |
||
24 | $reads = []; |
||
25 | foreach ($slaves as $host) { |
||
26 | $slaveDsn = self::changeHost($dsn, $host); |
||
27 | $name = 'slave' . (string) $i++; |
||
28 | $reads[$name] = new Connection($slaveDsn, $user, $password); |
||
29 | } |
||
30 | |||
31 | return new ConnectionLocator(null, $reads, $writes); |
||
32 | } |
||
33 | |||
34 | private static function changeHost(string $dsn, string $host): string |
||
44 | } |
||
45 | } |
||
46 |