Elasticsearch   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 45
ccs 14
cts 18
cp 0.7778
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 7 2
A buildElasticsearchClient() 0 26 5
1
<?php
2
namespace Triadev\Es;
3
4
use Elasticsearch\Client;
5
use Elasticsearch\ClientBuilder;
6
use Triadev\Es\Contract\ElasticsearchContract;
7
8
class Elasticsearch implements ElasticsearchContract
9
{
10
    /** @var Client */
11
    private $client;
12
    
13
    /**
14
     * Get client
15
     *
16
     * @return Client
17
     */
18 1
    public function getClient(): Client
19
    {
20 1
        if (!$this->client) {
21 1
            $this->client = $this->buildElasticsearchClient();
22
        }
23
    
24 1
        return $this->client;
25
    }
26
    
27 1
    private function buildElasticsearchClient() : Client
28
    {
29 1
        $config = config('triadev-elasticsearch');
30
        
31 1
        $clientBuilder = ClientBuilder::create();
32
        
33 1
        $clientBuilder->setHosts(explode('|', array_get($config, 'hosts')));
34 1
        $clientBuilder->setRetries(array_get($config, 'retries'));
35
    
36 1
        if ($logger = array_get($config, 'logger')) {
37
            $clientBuilder->setLogger($logger);
38
        }
39
        
40 1
        if ($connectionPool = array_get($config, 'connection.pool')) {
41
            $clientBuilder->setConnectionPool($connectionPool);
42
        }
43
    
44 1
        if ($connectionSelector = array_get($config, 'connection.selector')) {
45
            $clientBuilder->setSelector($connectionSelector);
46
        }
47
    
48 1
        if ($serializer = array_get($config, 'serializer')) {
49
            $clientBuilder->setSerializer($serializer);
50
        }
51
        
52 1
        return $clientBuilder->build();
53
    }
54
}
55