Passed
Push — feature/initial-implementation ( 88960a...869acf )
by Fike
02:03
created

DefaultClientFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 3 1
A __construct() 0 4 1
A setLogger() 0 4 1
A setHosts() 0 4 1
1
<?php
2
3
namespace AmaTeam\ElasticSearch\API;
4
5
use Elasticsearch\Client;
6
use Elasticsearch\ClientBuilder;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
10
class DefaultClientFactory implements ClientFactoryInterface
11
{
12
    /**
13
     * @var string[]
14
     */
15
    private $hosts;
16
    /**
17
     * @var LoggerInterface
18
     */
19
    private $logger;
20
21
    /**
22
     * @param string[] $hosts
23
     */
24
    public function __construct($hosts = ['localhost:9200'])
25
    {
26
        $this->hosts = $hosts;
27
        $this->logger = new NullLogger();
28
    }
29
30
    public function getClient(): Client
31
    {
32
        return ClientBuilder::create()->setHosts($this->hosts)->build();
33
    }
34
35
    /**
36
     * @param string[] $hosts
37
     * @return DefaultClientFactory
38
     */
39
    public function setHosts(string ...$hosts): DefaultClientFactory
40
    {
41
        $this->hosts = $hosts;
42
        return $this;
43
    }
44
45
    /**
46
     * @param LoggerInterface $logger
47
     * @return DefaultClientFactory
48
     */
49
    public function setLogger(LoggerInterface $logger): DefaultClientFactory
50
    {
51
        $this->logger = $logger;
52
        return $this;
53
    }
54
}
55