Passed
Push — feature/initial-implementation ( aa3ce7...132c28 )
by Fike
02:05
created

Builder   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
dl 0
loc 213
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A getPreservedMappingParameters() 0 3 1
A shouldNormalize() 0 3 1
A getLoaders() 0 3 1
A setClientFactory() 0 4 1
A deregisterLoader() 0 6 1
A getClientFactory() 0 3 1
A registerLoader() 0 6 2
A setPreservedIndexingOptions() 0 4 1
A __construct() 0 5 1
A setNormalize() 0 4 1
A getPreservedIndexingOptions() 0 3 1
A shouldValidate() 0 3 1
A setValidate() 0 4 1
A setLogger() 0 4 1
A build() 0 15 2
A setPreservedMappingParameters() 0 4 1
A deregisterLoaders() 0 4 1
A shouldPreserveUnknownEntries() 0 3 1
A getLogger() 0 3 1
A setPreserveUnknownEntries() 0 4 1
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\Descriptor\LoaderInterface;
8
use AmaTeam\ElasticSearch\API\Entity\Loader\Context;
9
use AmaTeam\ElasticSearch\API\Framework\BuilderInterface;
10
use AmaTeam\ElasticSearch\API\Framework\ConfigurationInterface;
11
use AmaTeam\ElasticSearch\API\FrameworkInterface;
12
use AmaTeam\ElasticSearch\Entity\Annotation\Loader;
13
use AmaTeam\ElasticSearch\Entity\Descriptor\Manager;
14
use AmaTeam\ElasticSearch\Entity\Loader as EntityLoader;
15
use AmaTeam\ElasticSearch\Entity\Registry;
16
use AmaTeam\ElasticSearch\Framework;
17
use Psr\Log\LoggerInterface;
18
use Psr\Log\NullLogger;
19
20
class Builder implements BuilderInterface, ConfigurationInterface
21
{
22
    /**
23
     * @var LoaderInterface[]
24
     */
25
    private $loaders = [];
26
    /**
27
     * @var ClientFactoryInterface
28
     */
29
    private $clientFactory;
30
    /**
31
     * @var LoggerInterface
32
     */
33
    private $logger;
34
    /**
35
     * @var bool
36
     */
37
    private $preserveUnknownEntries = false;
38
    /**
39
     * @var string[]
40
     */
41
    private $preservedIndexingOptions = [];
42
    /**
43
     * @var string[]
44
     */
45
    private $preservedMappingParameters = [];
46
    /**
47
     * @var bool
48
     */
49
    private $normalize = true;
50
    /**
51
     * @var bool
52
     */
53
    private $validate = true;
54
55
    public function __construct()
56
    {
57
        $this->loaders = [new Loader()];
58
        $this->logger = new NullLogger();
59
        $this->clientFactory = (new DefaultClientFactory())->setLogger($this->logger);
60
    }
61
62
    /**
63
     * @return LoaderInterface[]
64
     */
65
    public function getLoaders(): array
66
    {
67
        return $this->loaders;
68
    }
69
70
    public function registerLoader(LoaderInterface $loader): BuilderInterface
71
    {
72
        if (!in_array($loader, $this->loaders)) {
73
            $this->loaders[] = $loader;
74
        }
75
        return $this;
76
    }
77
78
    public function deregisterLoader(LoaderInterface $loader)
79
    {
80
        $this->loaders = array_filter($this->loaders, function (LoaderInterface $item) use ($loader) {
81
            return $loader !== $item;
82
        });
83
        return $this;
84
    }
85
86
    public function deregisterLoaders(): BuilderInterface
87
    {
88
        $this->loaders = [];
89
        return $this;
90
    }
91
92
    /**
93
     * @return ClientFactoryInterface
94
     */
95
    public function getClientFactory(): ClientFactoryInterface
96
    {
97
        return $this->clientFactory;
98
    }
99
100
    /**
101
     * @param ClientFactoryInterface $clientFactory
102
     * @return BuilderInterface
103
     */
104
    public function setClientFactory(ClientFactoryInterface $clientFactory): BuilderInterface
105
    {
106
        $this->clientFactory = $clientFactory;
107
        return $this;
108
    }
109
110
    /**
111
     * @return LoggerInterface
112
     */
113
    public function getLogger(): LoggerInterface
114
    {
115
        return $this->logger;
116
    }
117
118
    /**
119
     * @param LoggerInterface $logger
120
     * @return BuilderInterface
121
     */
122
    public function setLogger(LoggerInterface $logger): BuilderInterface
123
    {
124
        $this->logger = $logger;
125
        return $this;
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function shouldPreserveUnknownEntries(): bool
132
    {
133
        return $this->preserveUnknownEntries;
134
    }
135
136
    /**
137
     * @param bool $preserveUnknownEntries
138
     * @return BuilderInterface
139
     */
140
    public function setPreserveUnknownEntries(bool $preserveUnknownEntries): BuilderInterface
141
    {
142
        $this->preserveUnknownEntries = $preserveUnknownEntries;
143
        return $this;
144
    }
145
146
    /**
147
     * @return string[]
148
     */
149
    public function getPreservedIndexingOptions(): array
150
    {
151
        return $this->preservedIndexingOptions;
152
    }
153
154
    /**
155
     * @param string[] $options
156
     * @return BuilderInterface
157
     */
158
    public function setPreservedIndexingOptions(string ...$options): BuilderInterface
159
    {
160
        $this->preservedIndexingOptions = $options;
161
        return $this;
162
    }
163
164
    /**
165
     * @return string[]
166
     */
167
    public function getPreservedMappingParameters(): array
168
    {
169
        return $this->preservedMappingParameters;
170
    }
171
172
    /**
173
     * @param string[] $parameters
174
     * @return BuilderInterface
175
     */
176
    public function setPreservedMappingParameters(string ...$parameters): BuilderInterface
177
    {
178
        $this->preservedMappingParameters = $parameters;
179
        return $this;
180
    }
181
182
    /**
183
     * @return bool
184
     */
185
    public function shouldNormalize(): bool
186
    {
187
        return $this->normalize;
188
    }
189
190
    /**
191
     * @param bool $normalize
192
     * @return BuilderInterface
193
     */
194
    public function setNormalize(bool $normalize): BuilderInterface
195
    {
196
        $this->normalize = $normalize;
197
        return $this;
198
    }
199
200
    /**
201
     * @return bool
202
     */
203
    public function shouldValidate(): bool
204
    {
205
        return $this->validate;
206
    }
207
208
    /**
209
     * @param bool $validate
210
     * @return BuilderInterface
211
     */
212
    public function setValidate(bool $validate): BuilderInterface
213
    {
214
        $this->validate = $validate;
215
        return $this;
216
    }
217
218
    public function build(): FrameworkInterface
219
    {
220
        $manager = new Manager();
221
        foreach ($this->loaders as $loader) {
222
            $manager->registerLoader($loader);
223
        }
224
        $loader = new EntityLoader($manager);
225
        $context = (new Context())
226
            ->setPreserveUnknownEntries($this->shouldPreserveUnknownEntries())
227
            ->setPreservedMappingParameters($this->getPreservedMappingParameters())
228
            ->setPreservedIndexingOptions($this->getPreservedIndexingOptions())
229
            ->setNormalize($this->shouldNormalize())
230
            ->setValidate($this->shouldValidate());
231
        $registry = new Registry($loader, $context);
232
        return new Framework($registry, $this);
233
    }
234
}
235