Passed
Push — master ( a6b189...673d08 )
by Christopher
02:32
created

src/Elasticsearch.php (1 issue)

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
        
33
        $clientBuilder->setHosts(explode('|', $config, 'hosts'));
0 ignored issues
show
'hosts' of type string is incompatible with the type integer expected by parameter $limit of explode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        $clientBuilder->setHosts(explode('|', $config, /** @scrutinizer ignore-type */ 'hosts'));
Loading history...
34
        $clientBuilder->setRetries(array_get($config, 'retries'));
35
    
36
        if ($logger = array_get($config, 'logger')) {
37
            $clientBuilder->setLogger($logger);
38
        }
39
        
40
        if ($connectionPool = array_get($config, 'connection.pool')) {
41
            $clientBuilder->setConnectionPool($connectionPool);
42
        }
43
    
44
        if ($connectionSelector = array_get($config, 'connection.selector')) {
45
            $clientBuilder->setSelector($connectionSelector);
46
        }
47
    
48
        if ($serializer = array_get($config, 'serializer')) {
49
            $clientBuilder->setSerializer($serializer);
50
        }
51
        
52
        return $clientBuilder->build();
53
    }
54
}
55