ElasticsearchClients   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 13
dl 0
loc 68
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 3 1
A add() 0 18 1
A all() 0 3 1
1
<?php
2
namespace Triadev\EsMigration\Business\Repository;
3
4
use Elasticsearch\Client;
5
use Elasticsearch\ClientBuilder;
6
7
class ElasticsearchClients
8
{
9
    /**
10
     * @var array
11
     */
12
    private $elasticsearchClients = [];
13
    
14
    /** @var ClientBuilder */
15
    private $clientBuilder;
16
    
17
    /**
18
     * ElasticsearchClients constructor.
19
     */
20 63
    public function __construct()
21
    {
22 63
        $this->clientBuilder = ClientBuilder::create();
23 63
    }
24
    
25
    /**
26
     * Add
27
     *
28
     * @param string $esClientKey
29
     * @param string $host
30
     * @param int $port
31
     * @param string $scheme
32
     * @param null|string $user
33
     * @param null|string $password
34
     * @param int $retries
35
     */
36 63
    public function add(
37
        string $esClientKey,
38
        string $host,
39
        int $port,
40
        string $scheme,
41
        ?string $user = null,
42
        ?string $password = null,
43
        int $retries = 1
44
    ) {
45 63
        $this->elasticsearchClients[$esClientKey] = $this->clientBuilder->setHosts([
46
            [
47 63
                'host' => $host,
48 63
                'port' => $port,
49 63
                'scheme' => $scheme,
50 63
                'user' => $user,
51 63
                'pass' => $password
52
            ]
53 63
        ])->setRetries($retries)->build();
54 63
    }
55
    
56
    /**
57
     * Get
58
     *
59
     * @param string $esClientKey
60
     * @return Client|null
61
     */
62 47
    public function get(string $esClientKey) : ?Client
63
    {
64 47
        return array_get($this->elasticsearchClients, $esClientKey);
65
    }
66
    
67
    /**
68
     * All
69
     *
70
     * @return Client[]
71
     */
72 6
    public function all() : array
73
    {
74 6
        return $this->elasticsearchClients;
75
    }
76
}
77