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

Builder::setClientFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Framework;
4
5
use AmaTeam\ElasticSearch\API\ClientFactoryInterface;
6
use AmaTeam\ElasticSearch\API\DefaultClientFactory;
7
use AmaTeam\ElasticSearch\API\Entity\LoaderInterface;
8
use AmaTeam\ElasticSearch\API\Framework\BuilderInterface;
9
use AmaTeam\ElasticSearch\API\Framework\ConfigurationInterface;
10
use AmaTeam\ElasticSearch\API\FrameworkInterface;
11
use AmaTeam\ElasticSearch\Entity\Annotation\Loader;
12
use AmaTeam\ElasticSearch\Framework;
13
use Psr\Log\LoggerInterface;
14
use Psr\Log\NullLogger;
15
16
class Builder implements BuilderInterface, ConfigurationInterface
17
{
18
    /**
19
     * @var LoaderInterface[]
20
     */
21
    private $loaders = [];
22
    /**
23
     * @var ClientFactoryInterface
24
     */
25
    private $clientFactory;
26
    /**
27
     * @var LoggerInterface
28
     */
29
    private $logger;
30
31
    public function __construct()
32
    {
33
        $this->loaders = [new Loader()];
34
        $this->logger = new NullLogger();
35
        $this->clientFactory = (new DefaultClientFactory())->setLogger($this->logger);
36
    }
37
38
    /**
39
     * @return LoaderInterface[]
40
     */
41
    public function getLoaders(): array
42
    {
43
        return $this->loaders;
44
    }
45
46
    public function registerLoader(LoaderInterface $loader): BuilderInterface
47
    {
48
        if (!in_array($loader, $this->loaders)) {
49
            $this->loaders[] = $loader;
50
        }
51
        return $this;
52
    }
53
54
    public function deregisterLoader(LoaderInterface $loader)
55
    {
56
        $this->loaders = array_filter($this->loaders, function (LoaderInterface $item) use ($loader) {
57
            return $loader !== $item;
58
        });
59
        return $this;
60
    }
61
62
    public function deregisterLoaders(): BuilderInterface
63
    {
64
        $this->loaders = [];
65
        return $this;
66
    }
67
68
    /**
69
     * @return ClientFactoryInterface
70
     */
71
    public function getClientFactory(): ClientFactoryInterface
72
    {
73
        return $this->clientFactory;
74
    }
75
76
    /**
77
     * @param ClientFactoryInterface $clientFactory
78
     * @return BuilderInterface
79
     */
80
    public function setClientFactory(ClientFactoryInterface $clientFactory): BuilderInterface
81
    {
82
        $this->clientFactory = $clientFactory;
83
        return $this;
84
    }
85
86
    /**
87
     * @return LoggerInterface
88
     */
89
    public function getLogger(): LoggerInterface
90
    {
91
        return $this->logger;
92
    }
93
94
    /**
95
     * @param LoggerInterface $logger
96
     * @return BuilderInterface
97
     */
98
    public function setLogger(LoggerInterface $logger): BuilderInterface
99
    {
100
        $this->logger = $logger;
101
        return $this;
102
    }
103
104
    public function build(): FrameworkInterface
105
    {
106
        return new Framework($this);
107
    }
108
}
109