Passed
Push — master ( 3c1566...cfcfb4 )
by Sergey
02:41
created

Zone::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date  : 4/12/16
5
 * @time  : 1:00 PM
6
 */
7
8
namespace LTDBeget\dns\configurator;
9
10
use LTDBeget\dns\configurator\deserializer\ArrayDeserializer;
11
use LTDBeget\dns\configurator\deserializer\PlainDeserializer;
12
use LTDBeget\dns\configurator\errors\ErrorsStore;
13
use LTDBeget\dns\configurator\errors\ValidationError;
14
use LTDBeget\dns\configurator\traits\RecordsIterateTrait;
15
use LTDBeget\dns\configurator\validators\HostnameValidator;
16
use LTDBeget\dns\configurator\zoneEntities\Node;
17
use LTDBeget\dns\configurator\zoneEntities\record\base\Record;
18
use LTDBeget\dns\enums\eErrorCode;
19
use LTDBeget\dns\enums\eRecordType;
20
21
/**
22
 * Class Zone
23
 *
24
 * @package LTDBeget\dns\configurator
25
 */
26
class Zone
27
{
28
    use RecordsIterateTrait;
29
30
    /**
31
     * @var String
32
     */
33
    private $origin;
34
    /**
35
     * @var Node[] $nodes
36
     */
37
    private $nodes = [];
38
    /**
39
     * @var ErrorsStore
40
     */
41
    private $errorsStore;
42
43
    /**
44
     * @param $origin
45
     */
46 1
    public function __construct(string $origin)
47
    {
48 1
        $this->origin      = mb_strtolower(trim($origin));
49 1
        $this->errorsStore = new ErrorsStore();
50 1
    }
51
52
    /**
53
     * @param string $origin
54
     * @param string $plainData
55
     * @return Zone
56
     */
57 1
    public static function fromString(string $origin, string $plainData) : Zone
58
    {
59 1
        return PlainDeserializer::deserialize(new static($origin), $plainData);
60
    }
61
62
    /**
63
     * @param string $origin
64
     * @param array  $arrayData
65
     * @return Zone
66
     */
67
    public static function fromArray(string $origin, array $arrayData) : Zone
68
    {
69
        return ArrayDeserializer::deserialize(new static($origin), $arrayData);
70
    }
71
72
    /**
73
     * @return string
74
     */
75 1
    public function __toString() : string
76
    {
77 1
        $this->sort();
78 1
        $records = [];
79 1
        foreach ($this->iterateRecords() as $record) {
80 1
            $records[] = (string) $record;
81
        }
82
83 1
        return implode("\n", $records);
84
    }
85
86
    /**
87
     * Sort zone by node names and record types
88
     */
89 1
    public function sort()
90
    {
91 1
        $nodes = $this->nodes;
92
        /** @var Node[] $_nodes */
93 1
        $_nodes = [];
94 1
        foreach (['@', 'www', '*'] as $node_name) {
95 1
            if (isset($nodes[$node_name])) {
96 1
                $_nodes[$node_name] = $nodes[$node_name];
97 1
                unset($nodes[$node_name]);
98
            }
99
        }
100
101 1
        ksort($nodes);
102 1
        $this->nodes = array_merge($_nodes, $nodes);
103
104 1
        foreach ($this->iterateNodes() as $node) {
105 1
            $node->sort();
106
        }
107 1
    }
108
109
    /**
110
     * @return Node[]
111
     */
112 1
    public function iterateNodes()
113
    {
114 1
        foreach ($this->nodes as $node) {
115 1
            yield $node;
116
        }
117 1
    }
118
119
    /**
120
     * @param eRecordType|null $type
121
     * @return Record[]
122
     */
123 1
    public function iterateRecords(eRecordType $type = NULL)
124
    {
125 1
        foreach ($this->iterateNodes() as $node) {
126 1
            foreach ($node->iterateRecords($type) as $record) {
127 1
                yield $record;
128
            }
129
        }
130 1
    }
131
132
    /**
133
     * @return array
134
     */
135 1
    public function toArray() : array
136
    {
137 1
        $this->sort();
138 1
        $records = [];
139 1
        foreach ($this->iterateRecords() as $record) {
140 1
            $records[] = $record->toArray();
141
        }
142
143 1
        return $records;
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function getNodeNames() : array
150
    {
151
        return array_keys($this->nodes);
152
    }
153
154
    /**
155
     * @param $name
156
     * @return Node
157
     */
158 1
    public function getNode(string $name) : Node
159
    {
160 1
        $name = mb_strtolower(trim($name));
161
162 1
        if (!$this->isNodeExist($name)) {
163 1
            $this->nodes[$name] = new Node($this, $name);
164
        }
165
166 1
        return $this->nodes[$name];
167
    }
168
169
    /**
170
     * Check is node by given name already exists
171
     *
172
     * @param $name
173
     * @return bool
174
     */
175 1
    public function isNodeExist($name) : bool
176
    {
177 1
        return array_key_exists(mb_strtolower(trim($name)), $this->nodes);
178
    }
179
180
    /**
181
     * @param eRecordType|NULL $type
182
     */
183
    public function removeRecords(eRecordType $type = NULL)
184
    {
185
        foreach ($this->iterateNodes() as $node) {
186
            foreach ($node->iterateRecords($type) as $record) {
187
                $record->remove();
188
            }
189
        }
190
    }
191
192
    /**
193
     * @return bool
194
     */
195
    public function isEmptyZone() : bool
196
    {
197
        $result = true;
198
        foreach ($this->iterateNodes() as $node) {
199
            if(! $node->isEmptyNode()) {
200
                $result = false;
201
            }
202
        }
203
        return $result;
204
    }
205
206
    /**
207
     * Full validate zone via build in validators
208
     *
209
     * @return bool
210
     */
211 1
    public function validate() : bool
212
    {
213 1
        $errorsStore = $this->getErrorsStore();
214 1
        $errorsStore->clear();
215
216 1
        foreach ($this->iterateNodes() as $node) {
217
            /** @noinspection PhpInternalEntityUsedInspection */
218 1
            $node->validate();
219
        }
220
221 1
        if (!HostnameValidator::validate($this->getOrigin())) {
222
            $errorsStore->add(ValidationError::makeZoneError($this, eErrorCode::WRONG_ORIGIN()));
223
        }
224
225 1
        return !$errorsStore->isHasErrors();
226
    }
227
228
    /**
229
     * @return ErrorsStore
230
     */
231 1
    public function getErrorsStore()
232
    {
233 1
        return $this->errorsStore;
234
    }
235
236
    /**
237
     * @return String
238
     */
239 1
    public function getOrigin() : string
240
    {
241 1
        return $this->origin;
242
    }
243
244
    /**
245
     * @return Zone
246
     */
247
    public function clear() : Zone
248
    {
249
        $this
250
            ->dropNodes()
251
            ->getErrorsStore()
252
            ->clear();
253
254
        return $this;
255
    }
256
257
    /**
258
     * @return Zone
259
     */
260
    private function dropNodes() : Zone
261
    {
262
        $this->nodes = [];
263
264
        return $this;
265
    }
266
}