Passed
Push — master ( 31cac0...d9f189 )
by Sergey
47s
created

Section::createOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

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