Passed
Pull Request — master (#44)
by
unknown
02:03
created

Configuration::setIndexer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
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
use LTDBeget\sphinx\configurator\configurationEntities\sections\Common;
11
use LTDBeget\sphinx\configurator\configurationEntities\sections\Index;
12
use LTDBeget\sphinx\configurator\configurationEntities\sections\Indexer;
13
use LTDBeget\sphinx\configurator\configurationEntities\sections\Searchd;
14
use LTDBeget\sphinx\configurator\configurationEntities\sections\Source;
15
use LTDBeget\sphinx\configurator\exceptions\ConfigurationException;
16
use LTDBeget\sphinx\enums\eSection;
17
use LTDBeget\sphinx\enums\eVersion;
18
use LTDBeget\sphinx\informer\Informer;
19
20
/**
21
 * Class Configuration
22
 *
23
 * @package LTDBeget\sphinx\configurator
24
 */
25
class Configuration
26
{
27
    /**
28
     * @var eVersion
29
     */
30
    private $version;
31
    /**
32
     * @var Informer
33
     */
34
    private $informer;
35
    /**
36
     * @var Source[]
37
     */
38
    private $sources = [];
39
    /**
40
     * @var Index[]
41
     */
42
    private $indexes = [];
43
    /**
44
     * @var Indexer
45
     */
46
    private $indexer;
47
    /**
48
     * @var Searchd
49
     */
50
    private $searchd;
51
    /**
52
     * @var Common
53
     */
54
    private $common;
55
56
    /**
57
     * Configuration constructor.
58
     *
59
     * @param eVersion $version
60
     *
61
     * @throws \LTDBeget\sphinx\informer\exceptions\DocumentationSourceException
62
     * @throws \Symfony\Component\Yaml\Exception\ParseException
63
     */
64 16
    public function __construct(eVersion $version)
65
    {
66
        // todo: version is not needed, Informer should be passed
67 16
        $this->version = $version;
68 16
        $this->informer = Informer::get($this->version);
69 16
    }
70
71 4
    public function getSearchd()
72
    {
73 4
        if (!$this->hasSearchd()) {
74
            return null;
75
        }
76
77 4
        return $this->searchd;
78
    }
79
80 5
    public function setSearchd(Searchd $searchd)
81
    {
82
        // todo: clone?
83 5
        $this->searchd = $searchd;
84 5
    }
85
86 5
    public function hasSearchd(): bool
87
    {
88 5
        return null !== $this->searchd && !$this->searchd->isDeleted();
89
    }
90
91 11
    public function addSource(Source $source)
92
    {
93
        // todo: check source name uniqueness
94
        // todo: clone?
95 11
        $this->sources[] = $source;
96 11
    }
97
98
    /**
99
     * @return Source[]
100
     */
101 12
    public function iterateSources()
102
    {
103
        // todo: replace with conditional iterator
104 12
        foreach ($this->sources as $source) {
105 10
            if (!$source->isDeleted()) {
106 10
                yield $source;
107
            }
108
        }
109 12
    }
110
111 9
    public function addIndex(Index $index)
112
    {
113
        // todo: check index name uniqueness
114
        // todo: clone?
115 9
        $this->indexes[] = $index;
116 9
    }
117
118
    /**
119
     * @return Index[]
120
     */
121 9
    public function iterateIndexes()
122
    {
123
        // todo: replace with conditional iterator
124 9
        foreach ($this->indexes as $index) {
125 7
            if (!$index->isDeleted()) {
126 7
                yield $index;
127
            }
128
        }
129 9
    }
130
131 4
    public function getIndexer()
132
    {
133 4
        if (!$this->hasIndexer()) {
134
            return null;
135
        }
136
137 4
        return $this->indexer;
138
    }
139
140 5
    public function setIndexer(Indexer $indexer)
141
    {
142
        // todo: clone?
143 5
        $this->indexer = $indexer;
144 5
    }
145
146 5
    public function hasIndexer(): bool
147
    {
148 5
        return null !== $this->indexer && !$this->indexer->isDeleted();
149
    }
150
151 3
    public function getCommon()
152
    {
153 3
        if (!$this->hasCommon()) {
154
            return null;
155
        }
156
157 3
        return $this->common;
158
    }
159
160
    /**
161
     * @param \LTDBeget\sphinx\configurator\configurationEntities\sections\Common $common
162
     *
163
     * @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException
164
     */
165 4
    public function setCommon(Common $common)
166
    {
167 4
        $section = eSection::COMMON();
168 4
        if (!$this->isAllowedSection($section)) {
169
            $version = $this->getVersion();
170
            throw new ConfigurationException("Sphinx of version {$version} does't have section {$section}");
171
        }
172
173
        // todo: clone?
174 4
        $this->common = $common;
175 4
    }
176
177 4
    public function hasCommon(): bool
178
    {
179 4
        return null !== $this->common && !$this->common->isDeleted();
180
    }
181
182 5
    public function isAllowedSection(eSection $section): bool
183
    {
184 5
        return $this->informer->isSectionExist($section);
185
    }
186
187 1
    public function getVersion(): eVersion
188
    {
189 1
        return $this->version;
190
    }
191
192 10
    public function getInformer(): Informer
193
    {
194 10
        return $this->informer;
195
    }
196
}
197