Completed
Push — master ( 4c1e92...762548 )
by Alessandro
12:26 queued 09:30
created

ConnectionFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 98
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createConnection() 0 21 3
A createClient() 0 16 2
A getClientForConfiguration() 0 19 2
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