Completed
Push — master ( e3e9e9...e717f3 )
by Christopher
06:02 queued 03:09
created

Elasticsearch::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
    public function getClient(): Client
19
    {
20
        if (!$this->client) {
21
            $this->client = $this->buildElasticsearchClient();
22
        }
23
    
24
        return $this->client;
25
    }
26
    
27
    private function buildElasticsearchClient() : Client
28
    {
29
        $config = config('triadev-elasticsearch');
30
        
31
        $clientBuilder = ClientBuilder::create();
32
        $clientBuilder->setHosts([
33
            [
34
                'host' => array_get($config, 'host'),
35
                'port' => array_get($config, 'port'),
36
                'scheme' => array_get($config, 'scheme'),
37
                'user' => array_get($config, 'user'),
38
                'pass' => array_get($config, 'password')
39
            ]
40
        ]);
41
        
42
        return $clientBuilder->build();
43
    }
44
}
45