Passed
Branch master (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 %

Code Coverage

Tests 5
CRAP Score 2.2109
Metric Value
dl 0
loc 13
ccs 5
cts 8
cp 0.625
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2.2109
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 1
    public function __construct(Configuration $configuration)
47
    {
48 1
        $this->configuration = $configuration;
49 1
    }
50
51 1
    public function className() : string
52
    {
53 1
        return get_called_class();
54
    }
55
56
    /**
57
     * @return eSection
58
     */
59 1
    final public function getType() : eSection
60
    {
61 1
        if (is_null($this->type)) {
62 1
            $this->initType();
63
        }
64
65 1
        return $this->type;
66
    }
67
68
    /**
69
     * @return Configuration
70
     */
71 1
    final public function getConfiguration() : Configuration
72
    {
73 1
        return $this->configuration;
74
    }
75
76
    /**
77
     * @return string
78
     */
79 1
    public function __toString() : string
80
    {
81 1
        return $this->getType();
82
    }
83
84
    /**
85
     * @return Option[]
86
     */
87 1
    final public function iterateOptions()
88
    {
89 1
        foreach ($this->options as $option) {
90 1
            if (is_array($option)) {
91 1
                foreach ($option as $multiOption) {
92 1
                    yield $multiOption;
93
                }
94
            } else {
95 1
                yield $option;
96
            }
97
98
        }
99 1
    }
100
101
    /**
102
     * @param eOption $name
103
     * @param string $value
104
     * @return Option
105
     * @throws WrongContextException
106
     */
107 1
    final protected function addOptionInternal(eOption $name, string $value) : Option
108
    {
109 1
        $option = $this->createOption($name, $value);
110 1
        $option_name = (string) $option->getName();
111
112 1
        if ($option->isMultiValue()) {
113 1
            $this->options[$option_name] = $this->options[$option_name] ?? [];
114 1
            $this->options[$option_name][] = $option;
115
        } else {
116 1
            $this->options[$option_name] = $option;
117
        }
118
119 1
        return $option;
120
    }
121
122
    /**
123
     * @return Informer
124
     */
125 1
    protected function getInformer() : Informer
126
    {
127
        return $this
128 1
            ->getConfiguration()
129 1
            ->getInformer();
130
    }
131
132
    /**
133
     * @internal
134
     * @return string
135
     */
136 1
    private function shortClassName() : string
137
    {
138 1
        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 1
    final private function createOption(eOption $name, string $value)
149
    {
150 1
        $informer = $this->getInformer();
151 1
        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 1
        $isMultiValue = $informer->getOptionInfo($this->getType(), $name)->isIsMultiValue();
158
159 1
        return new Option($this, $name, $value, $isMultiValue);
160
    }
161
162
    /**
163
     * @internal
164
     * @throws LogicException
165
     */
166 1
    private function initType()
167
    {
168 1
        $this->type = eSection::get(strtolower($this->shortClassName()));
169
    }
170
}