1 | <?php |
||
15 | final class ConnectionFactory |
||
16 | { |
||
17 | /** |
||
18 | * @var Client[] |
||
19 | */ |
||
20 | private $clients = []; |
||
21 | |||
22 | /** |
||
23 | * @param string $database |
||
24 | * @param string $host |
||
25 | * @param int $httpPort |
||
26 | * @param int $udpPort |
||
27 | * @param string $user |
||
28 | * @param string $password |
||
29 | * @param bool $udp |
||
30 | * |
||
31 | * @return \InfluxDB\Database |
||
32 | */ |
||
33 | 2 | public function createConnection(string $database, string $host, int $httpPort, int $udpPort, string $user, string $password, bool $udp = false) |
|
34 | { |
||
35 | 2 | $protocol = $udp ? 'udp' : 'http'; |
|
36 | // Define the client key to retrieve or create the client instance. |
||
37 | 2 | $clientKey = sprintf('%s.%s.%s', $host, $udpPort, $httpPort); |
|
38 | 2 | if (!empty($user)) { |
|
39 | 1 | $clientKey .= '.'.$user; |
|
40 | } |
||
41 | 2 | if (!empty($password)) { |
|
42 | 1 | $clientKey .= '.'.$password; |
|
43 | } |
||
44 | 2 | $clientKey .= '.'.$protocol; |
|
45 | |||
46 | 2 | if (!array_key_exists($clientKey, $this->clients)) { |
|
47 | 2 | $client = $this->createClient($host, $httpPort, $udpPort, $user, $password, $udp); |
|
48 | 2 | $this->clients[$clientKey] = $client; |
|
49 | } else { |
||
50 | 1 | $client = $this->clients[$clientKey]; |
|
51 | } |
||
52 | |||
53 | 2 | return $client->selectDB($database); |
|
54 | } |
||
55 | |||
56 | 2 | private function createClient(string $host, int $httpPort, int $udpPort, string $user, string $password, bool $udp = false) |
|
66 | } |
||
67 |