1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Algatux\InfluxDbBundle\Services; |
6
|
|
|
|
7
|
|
|
use InfluxDB\Client; |
8
|
|
|
use InfluxDB\Database; |
9
|
|
|
use InfluxDB\Driver\UDP; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Create connections as `InfluxDB\Database` instances. |
13
|
|
|
* |
14
|
|
|
* This keeps clients on a property to avoid useless instance duplication. |
15
|
|
|
* |
16
|
|
|
* @internal |
17
|
|
|
*/ |
18
|
|
|
final class ConnectionFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Client[] |
22
|
|
|
*/ |
23
|
|
|
private $clients = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $database |
27
|
|
|
* @param string $host |
28
|
|
|
* @param int $httpPort |
29
|
|
|
* @param int $udpPort |
30
|
|
|
* @param string $user |
31
|
|
|
* @param string $password |
32
|
|
|
* @param bool $udp |
33
|
|
|
* |
34
|
|
|
* @return Database |
35
|
|
|
*/ |
36
|
3 |
|
public function createConnection( |
37
|
|
|
string $database, |
38
|
|
|
string $host, |
39
|
|
|
int $httpPort, |
40
|
|
|
int $udpPort, |
41
|
|
|
string $user, |
42
|
|
|
string $password, |
43
|
|
|
bool $udp = false |
44
|
|
|
): Database { |
45
|
3 |
|
$protocol = $udp ? 'udp' : 'http'; |
46
|
|
|
// Define the client key to retrieve or create the client instance. |
47
|
3 |
|
$clientKey = sprintf('%s.%s.%s', $host, $udpPort, $httpPort); |
48
|
3 |
|
if (!empty($user)) { |
49
|
1 |
|
$clientKey .= '.'.$user; |
50
|
|
|
} |
51
|
3 |
|
$clientKey .= '.'.$protocol; |
52
|
|
|
|
53
|
3 |
|
$client = $this->getClientForConfiguration($host, $httpPort, $udpPort, $user, $password, $udp, $clientKey); |
54
|
|
|
|
55
|
3 |
|
return $client->selectDB($database); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $host |
60
|
|
|
* @param int $httpPort |
61
|
|
|
* @param int $udpPort |
62
|
|
|
* @param string $user |
63
|
|
|
* @param string $password |
64
|
|
|
* @param bool $udp |
65
|
|
|
* |
66
|
|
|
* @return Client |
67
|
|
|
*/ |
68
|
3 |
|
private function createClient( |
69
|
|
|
string $host, |
70
|
|
|
int $httpPort, |
71
|
|
|
int $udpPort, |
72
|
|
|
string $user, |
73
|
|
|
string $password, |
74
|
|
|
bool $udp = false |
75
|
|
|
): Client { |
76
|
3 |
|
$client = new Client($host, $httpPort, $user, $password); |
77
|
|
|
|
78
|
3 |
|
if ($udp) { |
79
|
2 |
|
$client->setDriver(new UDP($client->getHost(), $udpPort)); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
return $client; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $host |
87
|
|
|
* @param int $httpPort |
88
|
|
|
* @param int $udpPort |
89
|
|
|
* @param string $user |
90
|
|
|
* @param string $password |
91
|
|
|
* @param bool $udp |
92
|
|
|
* @param string $clientKey |
93
|
|
|
* |
94
|
|
|
* @return Client |
95
|
|
|
*/ |
96
|
3 |
|
private function getClientForConfiguration( |
97
|
|
|
string $host, |
98
|
|
|
int $httpPort, |
99
|
|
|
int $udpPort, |
100
|
|
|
string $user, |
101
|
|
|
string $password, |
102
|
|
|
bool $udp, |
103
|
|
|
$clientKey |
104
|
|
|
): Client { |
105
|
3 |
|
if (!array_key_exists($clientKey, $this->clients)) { |
106
|
3 |
|
$client = $this->createClient($host, $httpPort, $udpPort, $user, $password, $udp); |
107
|
3 |
|
$this->clients[$clientKey] = $client; |
108
|
|
|
|
109
|
3 |
|
return $client; |
110
|
|
|
} |
111
|
1 |
|
$client = $this->clients[$clientKey]; |
112
|
|
|
|
113
|
1 |
|
return $client; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|