Passed
Push — master ( 41a727...ec7c33 )
by Sergey
02:52
created

Configuration::isAllowedSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date: 3/2/16
5
 * @time: 4:33 PM
6
 */
7
8
namespace LTDBeget\sphinx\configurator;
9
10
11
use LTDBeget\sphinx\configurator\configurationEntities\sections\Common;
12
use LTDBeget\sphinx\configurator\configurationEntities\sections\Index;
13
use LTDBeget\sphinx\configurator\configurationEntities\sections\Indexer;
14
use LTDBeget\sphinx\configurator\configurationEntities\sections\Searchd;
15
use LTDBeget\sphinx\configurator\configurationEntities\sections\Source;
16
use LTDBeget\sphinx\configurator\deserializers\ArrayDeserializer;
17
use LTDBeget\sphinx\configurator\deserializers\JsonDeserializer;
18
use LTDBeget\sphinx\configurator\deserializers\PlainDeserializer;
19
use LTDBeget\sphinx\configurator\exceptions\NotFoundException;
20
use LTDBeget\sphinx\configurator\serializers\ArraySerializer;
21
use LTDBeget\sphinx\configurator\serializers\JsonSerializer;
22
use LTDBeget\sphinx\configurator\serializers\PlainSerializer;
23
use LTDBeget\sphinx\enums\eSection;
24
use LTDBeget\sphinx\enums\eVersion;
25
use LTDBeget\sphinx\informer\Informer;
26
27
/**
28
 * Class Configuration
29
 * @package LTDBeget\sphinx\configurator
30
 */
31
class Configuration
32
{
33
    /**
34
     * @param string $plainData
35
     * @param eVersion $version
36
     * @return Configuration
37
     */
38 4
    public static function fromString(string $plainData, eVersion $version) : Configuration
39
    {
40 4
        return PlainDeserializer::deserialize($plainData, new self($version));
41
    }
42
43
    /**
44
     * @param array $plainData
45
     * @param eVersion $version
46
     * @return Configuration
47
     */
48 1
    public static function fromArray(array $plainData, eVersion $version) : Configuration
49
    {
50 1
        return ArrayDeserializer::deserialize($plainData, new self($version));
51
    }
52
53
    /**
54
     * @param string $plainData
55
     * @param eVersion $version
56
     * @return Configuration
57
     */
58 1
    public static function fromJson(string $plainData, eVersion $version) : Configuration
59
    {
60 1
        return JsonDeserializer::deserialize($plainData, new self($version));
61
    }
62
63
    /**
64
     * @return string
65
     */
66 2
    public function __toString() : string
67
    {
68 2
        return PlainSerializer::serialize($this);
69
    }
70
71
    /**
72
     * @return array
73
     */
74 1
    public function toArray() : array
75
    {
76 1
        return ArraySerializer::serialize($this);
77
    }
78
79
    /**
80
     * @return string
81
     */
82 1
    public function toJson() : string
83
    {
84 1
        return JsonSerializer::serialize($this);
85
    }
86
87
    /**
88
     * @return Informer
89
     */
90 4
    public function getInformer() : Informer
91
    {
92 4
        return $this->informer;
93
    }
94
95
    /**
96
     * @return eVersion
97
     */
98
    public function getVersion() : eVersion
99
    {
100
        return $this->version;
101
    }
102
103
    /**
104
     * @param string $name
105
     * @param string|null $inheritanceName
106
     * @return Source
107
     * @throws NotFoundException
108
     */
109 4
    public function addSource(string $name, string $inheritanceName = null) : Source
110
    {
111 4
        $source          = new Source($this, $name, $inheritanceName);
112 4
        $this->sources[] = $source;
113
114 4
        return $source;
115
    }
116
117
    /**
118
     * @return Source[]
119
     */
120 3
    public function iterateSource()
121
    {
122 3
        foreach ($this->sources as $source) {
123 3
            yield $source;
124
        }
125 3
    }
126
127
    /**
128
     * @param string $name
129
     * @param string|null $inheritanceName
130
     * @return Index
131
     */
132 4
    public function addIndex(string $name, string $inheritanceName = null) : Index
133
    {
134 4
        $indexDefinition = new Index($this, $name, $inheritanceName);
135 4
        $this->indexes[] = $indexDefinition;
136
137 4
        return $indexDefinition;
138
    }
139
140
    /**
141
     * @return Index[]
142
     */
143 3
    public function iterateIndex()
144
    {
145 3
        foreach ($this->indexes as $index) {
146 3
            yield $index;
147
        }
148 3
    }
149
150
    /**
151
     * @param eSection $section
152
     * @return bool
153
     */
154 4
    public function isAllowedSection(eSection $section) : bool
155
    {
156 4
        return $this->informer->isSectionExist($section);
157
    }
158
159
    /**
160
     * @return Indexer
161
     */
162 4
    public function getIndexer() : Indexer
163
    {
164 4
        if (!$this->isHasIndexer()) {
165 4
            $this->initIndexer();
166
        }
167
168 4
        return $this->indexer;
169
    }
170
171
    /**
172
     * @return Searchd
173
     */
174 4
    public function getSearchd() : Searchd
175
    {
176 4
        if (!$this->isHasSearchd()) {
177 4
            $this->initSearchd();
178
        }
179
180 4
        return $this->searchd;
181
    }
182
183
    /**
184
     * @return Common
185
     */
186 4
    public function getCommon() : Common
187
    {
188 4
        $section = eSection::COMMON();
189 4
        if(! $this->isAllowedSection($section)) {
190 1
            throw new \LogicException("Sphinx of version {$this->version} does't have section {$section}");
191
        }
192
193 3
        if (!$this->isHasCommon()) {
194 3
            $this->initCommon();
195
        }
196
197 3
        return $this->common;
198
    }
199
200
    /**
201
     * @return bool
202
     */
203 4
    public function isHasIndexer() : bool
204
    {
205 4
        return !is_null($this->indexer);
206
    }
207
208
    /**
209
     * @return bool
210
     */
211 4
    public function isHasSearchd() : bool
212
    {
213 4
        return !is_null($this->searchd);
214
    }
215
216
    /**
217
     * @return bool
218
     */
219 3
    public function isHasCommon() : bool
220
    {
221 3
        return !is_null($this->common);
222
    }
223
224
    /**
225
     * Configuration constructor.
226
     * @param eVersion $version
227
     */
228 4
    protected function __construct(eVersion $version)
229
    {
230 4
        $this->version  = $version;
231 4
        $this->informer = Informer::get($this->version);
232 4
    }
233
234
    /**
235
     * @return Configuration
236
     */
237 4
    private function initIndexer() : self
238
    {
239 4
        $this->indexer = new Indexer($this);
240
241 4
        return $this;
242
    }
243
244
    /**
245
     * @return Configuration
246
     */
247 4
    private function initSearchd() : self
248
    {
249 4
        $this->searchd = new Searchd($this);
250
251 4
        return $this;
252
    }
253
254
    /**
255
     * @return Configuration
256
     */
257 3
    private function initCommon() : self
258
    {
259 3
        $this->common = new Common($this);
260
261 3
        return $this;
262
    }
263
264
    /**
265
     * @var eVersion
266
     */
267
    private $version;
268
269
    /**
270
     * @var Informer
271
     */
272
    private $informer;
273
274
    /**
275
     * @var Source[]
276
     */
277
    private $sources = [];
278
279
    /**
280
     * @var Index[]
281
     */
282
    private $indexes = [];
283
284
    /**
285
     * @var Indexer
286
     */
287
    private $indexer = null;
288
289
    /**
290
     * @var Searchd
291
     */
292
    private $searchd = null;
293
294
    /**
295
     * @var Common
296
     */
297
    private $common = null;
298
}