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

Section::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date: 3/18/16
5
 * @time: 5:11 PM
6
 */
7
8
namespace LTDBeget\sphinx\configurator\configurationEntities\base;
9
10
11
use LTDBeget\sphinx\configurator\Configuration;
12
use LTDBeget\sphinx\configurator\configurationEntities\Option;
13
use LTDBeget\sphinx\configurator\exceptions\LogicException;
14
use LTDBeget\sphinx\configurator\exceptions\WrongContextException;
15
use LTDBeget\sphinx\enums\base\eOption;
16
use LTDBeget\sphinx\enums\eSection;
17
use LTDBeget\sphinx\informer\Informer;
18
use ReflectionClass;
19
20
/**
21
 * Class Section
22
 * @package LTDBeget\sphinx\configurator\configurationEntities\base
23
 * @method Option addOption(eOption $name, string $value)
24
 */
25
abstract class Section
26
{
27
    /**
28
     * @var Configuration
29
     */
30
    private $configuration;
31
32
    /**
33
     * @var eSection
34
     */
35
    private $type = null;
36
37
    /**
38
     * @var Option[]
39
     */
40
    private $options = [];
41
42
    /**
43
     * @var boolean
44
     */
45
    private $isDeleted = false;
46
47
    /**
48
     * Section constructor.
49
     * @param Configuration $configuration
50
     */
51 4
    public function __construct(Configuration $configuration)
52
    {
53 4
        $this->configuration = $configuration;
54 4
    }
55
56 4
    public function className() : string
57
    {
58 4
        return get_called_class();
59
    }
60
61
    /**
62
     * @return eSection
63
     */
64 4
    final public function getType() : eSection
65
    {
66 4
        if (is_null($this->type)) {
67 4
            $this->initType();
68
        }
69
70 4
        return $this->type;
71
    }
72
73
    /**
74
     * @return Configuration
75
     */
76 4
    final public function getConfiguration() : Configuration
77
    {
78 4
        return $this->configuration;
79
    }
80
81
    /**
82
     * @return string
83
     */
84 2
    public function __toString() : string
85
    {
86 2
        return $this->getType();
87
    }
88
89
    /**
90
     * @return Option[]
91
     */
92 3
    final public function iterateOptions()
93
    {
94 3
        foreach ($this->options as $option) {
95 3
            if (is_array($option)) {
96 2
                foreach ($option as $multiOption) {
97
                    /**
98
                     * @var Option $multiOption
99
                     */
100 2
                    if(! $multiOption->isDeleted()) {
101 2
                        yield $multiOption;
102
                    }
103
104
                }
105
            } else {
106 3
                if(! $option->isDeleted()) {
107 3
                    yield $option;
108
                }
109
            }
110
111
        }
112 3
    }
113
114
    /**
115
     * mark section as deleted
116
     */
117 1
    final public function delete()
118
    {
119 1
        $this->isDeleted = true;
120 1
    }
121
122
    /**
123
     * is option marked as deleted
124
     * @return bool
125
     */
126 2
    final public function isDeleted()
127
    {
128 2
        return $this->isDeleted;
129
    }
130
131
    /**
132
     * @param eOption $name
133
     * @param string $value
134
     * @return Option
135
     * @throws WrongContextException
136
     */
137 4
    final protected function addOptionInternal(eOption $name, string $value) : Option
138
    {
139 4
        $option = $this->createOption($name, $value);
140 4
        $option_name = (string) $option->getName();
141
142 4
        if ($option->isMultiValue()) {
143 4
            $this->options[$option_name] = $this->options[$option_name] ?? [];
144 4
            $this->options[$option_name][] = $option;
145
        } else {
146 4
            $this->options[$option_name] = $option;
147
        }
148
149 4
        return $option;
150
    }
151
152
    /**
153
     * @return Informer
154
     */
155 4
    protected function getInformer() : Informer
156
    {
157
        return $this
158 4
            ->getConfiguration()
159 4
            ->getInformer();
160
    }
161
162
    /**
163
     * @internal
164
     * @return string
165
     */
166 4
    private function shortClassName() : string
167
    {
168 4
        return (new ReflectionClass($this->className()))->getShortName();
169
    }
170
171
    /**
172
     * @internal
173
     * @param eOption $name
174
     * @param string $value
175
     * @return Option
176
     * @throws WrongContextException
177
     */
178 4
    final private function createOption(eOption $name, string $value)
179
    {
180 4
        $informer = $this->getInformer();
181 4
        if (!$informer->isKnownOption($this->getType(), $name)) {
182
            $version = $this->getConfiguration()->getVersion();
183
            throw new WrongContextException(
184
                "For sphinx v. {$version} option {$name} in {$this->getType()} isn't available"
185
            );
186
        }
187 4
        $isMultiValue = $informer->getOptionInfo($this->getType(), $name)->isIsMultiValue();
188
189 4
        return new Option($this, $name, $value, $isMultiValue);
190
    }
191
192
    /**
193
     * @internal
194
     * @throws LogicException
195
     */
196 4
    private function initType()
197
    {
198 4
        $this->type = eSection::get(strtolower($this->shortClassName()));
199
    }
200
}