Test Failed
Push — master ( 2e661b...41a727 )
by Sergey
02:30
created

Section::createOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
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
     * Section constructor.
44
     * @param Configuration $configuration
45
     */
46
    public function __construct(Configuration $configuration)
47
    {
48
        $this->configuration = $configuration;
49
    }
50
51
    public function className() : string
52
    {
53
        return get_called_class();
54
    }
55
56
    /**
57
     * @return eSection
58
     */
59
    final public function getType() : eSection
60
    {
61
        if (is_null($this->type)) {
62
            $this->initType();
63
        }
64
65
        return $this->type;
66
    }
67
68
    /**
69
     * @return Configuration
70
     */
71
    final public function getConfiguration() : Configuration
72
    {
73
        return $this->configuration;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function __toString() : string
80
    {
81
        return $this->getType();
82
    }
83
84
    /**
85
     * @return Option[]
86
     */
87
    final public function iterateOptions()
88
    {
89
        foreach ($this->options as $option) {
90
            if (is_array($option)) {
91
                foreach ($option as $multiOption) {
92
                    yield $multiOption;
93
                }
94
            } else {
95
                yield $option;
96
            }
97
98
        }
99
    }
100
101
    /**
102
     * @param eOption $name
103
     * @param string $value
104
     * @return Option
105
     * @throws WrongContextException
106
     */
107
    final protected function addOptionInternal(eOption $name, string $value) : Option
108
    {
109
        $option = $this->createOption($name, $value);
110
        $option_name = (string) $option->getName();
111
112
        if ($option->isMultiValue()) {
113
            $this->options[$option_name] = $this->options[$option_name] ?? [];
114
            $this->options[$option_name][] = $option;
115
        } else {
116
            $this->options[$option_name] = $option;
117
        }
118
119
        return $option;
120
    }
121
122
    /**
123
     * @return Informer
124
     */
125
    protected function getInformer() : Informer
126
    {
127
        return $this
128
            ->getConfiguration()
129
            ->getInformer();
130
    }
131
132
    /**
133
     * @internal
134
     * @return string
135
     */
136
    private function shortClassName() : string
137
    {
138
        return (new ReflectionClass($this->className()))->getShortName();
139
    }
140
141
    /**
142
     * @internal
143
     * @param eOption $name
144
     * @param string $value
145
     * @return Option
146
     * @throws WrongContextException
147
     */
148
    final private function createOption(eOption $name, string $value)
149
    {
150
        $informer = $this->getInformer();
151
        if (!$informer->isKnownOption($this->getType(), $name)) {
152
            $version = $this->getConfiguration()->getVersion();
153
            throw new WrongContextException(
154
                "For sphinx v. {$version} option {$name} in {$this->getType()} isn't available"
155
            );
156
        }
157
        $isMultiValue = $informer->getOptionInfo($this->getType(), $name)->isIsMultiValue();
158
159
        return new Option($this, $name, $value, $isMultiValue);
160
    }
161
162
    /**
163
     * @internal
164
     * @throws LogicException
165
     */
166
    private function initType()
167
    {
168
        $this->type = eSection::get(strtolower($this->shortClassName()));
169
    }
170
}