Passed
Push — master ( 6e9648...ae59e9 )
by Sergey
05:36 queued 02:45
created

ArrayDeserializer::deserializeOption()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009
Metric Value
dl 0
loc 14
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 3
eloc 10
nc 3
nop 3
crap 3.009
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date: 19.03.16
5
 * @time: 1:54
6
 */
7
8
namespace LTDBeget\sphinx\configurator\deserializers;
9
10
11
use LTDBeget\sphinx\configurator\Configuration;
12
use LTDBeget\sphinx\configurator\configurationEntities\base\Section;
13
use LTDBeget\sphinx\configurator\exceptions\DeserializeException;
14
use LTDBeget\sphinx\enums\base\eOption;
15
use LTDBeget\sphinx\enums\eSection;
16
use LTDBeget\sphinx\enums\options\eCommonOption;
17
use LTDBeget\sphinx\enums\options\eIndexerOption;
18
use LTDBeget\sphinx\enums\options\eIndexOption;
19
use LTDBeget\sphinx\enums\options\eSearchdOption;
20
use LTDBeget\sphinx\enums\options\eSourceOption;
21
22
/**
23
 * Class ArrayDeserializer
24
 * Serialize correct array to Configuration object
25
 * @package LTDBeget\sphinx\configurator\deserializers
26
 */
27
final class ArrayDeserializer
28
{
29
    /**
30
     * Make Configuration object from array serialized content of sphinx configuration file
31
     * @param array $arrayConfiguration
32
     * @param Configuration $objectConfiguration
33
     * @return Configuration
34
     * @throws \LTDBeget\sphinx\configurator\exceptions\SectionException
35
     * @throws \InvalidArgumentException
36
     * @throws \LogicException
37
     * @throws \BadMethodCallException
38
     * @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException
39
     * @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException
40
     */
41 8
    public static function deserialize(array $arrayConfiguration, Configuration $objectConfiguration) : Configuration
42
    {
43 8
        $serializer                      = new self();
44 8
        $serializer->arrayConfiguration  = $arrayConfiguration;
45 8
        $serializer->objectConfiguration = $objectConfiguration;
46
47 8
        return $serializer->deserializeInternal();
48
    }
49
50
    /**
51
     * @internal
52
     * ArrayDeserializer constructor.
53
     */
54 8
    private function __construct()
55
    {
56 8
    }
57
58
    /**
59
     * @internal
60
     * @return Configuration
61
     * @throws \LTDBeget\sphinx\configurator\exceptions\SectionException
62
     * @throws \LogicException
63
     * @throws \InvalidArgumentException
64
     * @throws \BadMethodCallException
65
     * @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException
66
     * @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException
67
     */
68 8
    private function deserializeInternal() : Configuration
69
    {
70 8
        foreach ($this->arrayConfiguration as $section) {
71 8
            $sectionObject = $this->deserializeSection($section);
72
73 8
            if (!array_key_exists('options', $section)) {
74 2
                continue;
75
            }
76
77 6
            $this->deserializeOptions($section['options'], $sectionObject);
78
        }
79
80 3
        return $this->objectConfiguration;
81
    }
82
83
    /**
84
     * @internal
85
     * @param array $section_data
86
     * @return Section
87
     * @throws \LTDBeget\sphinx\configurator\exceptions\SectionException
88
     * @throws \LogicException
89
     * @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException
90
     * @throws \InvalidArgumentException
91
     * @throws \BadMethodCallException
92
     * @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException
93
     */
94 8
    private function deserializeSection(array $section_data) : Section
95
    {
96 8
        $type        = $section_data['type'] ?? null;
97 8
        $name        = $section_data['name'] ?? null;
98 8
        $inheritance = $section_data['inheritance'] ?? null;
99
100
        switch ($type) {
101 8
            case eSection::INDEXER:
102 4
                $section = $this->objectConfiguration->getIndexer();
103 4
                break;
104 8
            case eSection::SEARCHD:
105 4
                $section = $this->objectConfiguration->getSearchd();
106 4
                break;
107 8
            case eSection::COMMON:
108 4
                $section = $this->objectConfiguration->getCommon();
109 3
                break;
110 8
            case eSection::SOURCE:
111 6
                $section = $this->objectConfiguration->addSource($name, $inheritance);
112 6
                break;
113 6
            case eSection::INDEX:
114 6
                $section = $this->objectConfiguration->addIndex($name, $inheritance);
115 6
                break;
116
            default:
117
                throw new DeserializeException('Unknown section type');
118
        }
119
120 8
        return $section;
121
    }
122
123
    /**
124
     * @internal
125
     * @param array $options
126
     * @param Section $section
127
     * @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException
128
     * @throws \InvalidArgumentException
129
     * @throws \LogicException
130
     */
131 6
    private function deserializeOptions(array $options, Section $section)
132
    {
133 6
        foreach ($options as $option) {
134
135 6
            if (!array_key_exists('name', $option)) {
136
                throw new DeserializeException('Wrong array format. All options must contain name.');
137
            }
138
139 6
            if (!array_key_exists('value', $option)) {
140
                throw new DeserializeException('Wrong array format. All options must contain value.');
141
            }
142
143 6
            $optionName  = $option['name'];
144 6
            $optionValue = $option['value'];
145 6
            $this->deserializeOption($section, $optionName, $optionValue);
146
        }
147 6
    }
148
149
    /**
150
     * @param Section $section
151
     * @param $optionName
152
     * @param $optionValue
153
     * @throws \InvalidArgumentException
154
     * @throws \LogicException
155
     * @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException
156
     */
157 6
    private function deserializeOption(Section $section, $optionName, $optionValue)
158
    {
159 6
        $sectionType = $section->getType();
160 6
        $informer = $section->getConfiguration()->getInformer();
161 6
        $optionName = $this->getOptionName($section, $optionName);
162
163 6
        if($informer->isKnownOption($sectionType, $optionName)) {
164 6
            $section->addOption($optionName, $optionValue);
165 3
        } elseif($informer->isRemovedOption($sectionType, $optionName)) {
166 3
            return;
167
        } else {
168
            throw new DeserializeException("Unknown option name {$optionName} in section {$section->getType()}");
169
        }
170 6
    }
171
172
    /**
173
     * @internal
174
     * @param Section $section
175
     * @param string $name
176
     * @return eOption
177
     * @throws \LogicException
178
     * @throws \InvalidArgumentException
179
     * @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException
180
     */
181 6
    private function getOptionName(Section $section, string $name) : eOption
182
    {
183 6
        switch ($section->getType()) {
184 6
            case eSection::SOURCE:
185 5
                $option = eSourceOption::get($name);
186 5
                break;
187 5
            case eSection::INDEX:
188 5
                $option = eIndexOption::get($name);
189 5
                break;
190 4
            case eSection::INDEXER:
191 4
                $option = eIndexerOption::get($name);
192 4
                break;
193 4
            case eSection::SEARCHD:
194 4
                $option = eSearchdOption::get($name);
195 4
                break;
196 3
            case eSection::COMMON:
197 3
                $option = eCommonOption::get($name);
198 3
                break;
199
            default:
200
                throw new DeserializeException("Unknown section type {$section->getType()}");
201
        }
202
203 6
        return $option;
204
    }
205
206
    /**
207
     * @var array
208
     */
209
    private $arrayConfiguration = [];
210
211
    /**
212
     * @var Configuration
213
     */
214
    private $objectConfiguration;
215
}