Passed
Push — master ( 3f3f7b...ddf05f )
by Sergey
02:41
created

Zone::iterateNodes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
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\OriginValidator;
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
        $records = [];
138 1
        foreach ($this->iterateRecords() as $record) {
139 1
            $records[] = $record->toArray();
140
        }
141
142 1
        return $records;
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function getNodeNames() : array
149
    {
150
        return array_keys($this->nodes);
151
    }
152
153
    /**
154
     * @param $name
155
     * @return Node
156
     */
157 1
    public function getNode(string $name) : Node
158
    {
159 1
        $name = mb_strtolower(trim($name));
160
161 1
        if (!$this->isNodeExist($name)) {
162 1
            $this->nodes[$name] = new Node($this, $name);
163
        }
164
165 1
        return $this->nodes[$name];
166
    }
167
168
    /**
169
     * Check is node by given name already exists
170
     *
171
     * @param $name
172
     * @return bool
173
     */
174 1
    public function isNodeExist($name) : bool
175
    {
176 1
        return array_key_exists(mb_strtolower(trim($name)), $this->nodes);
177
    }
178
179
    /**
180
     * @param eRecordType|NULL $type
181
     */
182
    public function removeRecords(eRecordType $type = NULL)
183
    {
184
        foreach ($this->iterateNodes() as $node) {
185
            foreach ($node->iterateRecords($type) as $record) {
186
                $record->remove();
187
            }
188
        }
189
    }
190
191
    /**
192
     * @return bool
193
     */
194
    public function isEmptyZone() : bool
195
    {
196
        $result = true;
197
        foreach ($this->iterateNodes() as $node) {
198
            if(! $node->isEmptyNode()) {
199
                $result = false;
200
            }
201
        }
202
        return $result;
203
    }
204
205
    /**
206
     * Full validate zone via build in validators
207
     *
208
     * @return bool
209
     */
210 1
    public function validate() : bool
211
    {
212 1
        $errorsStore = $this->getErrorsStore();
213 1
        $errorsStore->clear();
214
215 1
        foreach ($this->iterateNodes() as $node) {
216
            /** @noinspection PhpInternalEntityUsedInspection */
217 1
            $node->validate();
218
        }
219
220 1
        if (!OriginValidator::validate($this->getOrigin())) {
221 1
            $errorsStore->add(ValidationError::makeZoneError($this, eErrorCode::WRONG_ORIGIN()));
222
        }
223
224 1
        return !$errorsStore->isHasErrors();
225
    }
226
227
    /**
228
     * @return ErrorsStore
229
     */
230 1
    public function getErrorsStore()
231
    {
232 1
        return $this->errorsStore;
233
    }
234
235
    /**
236
     * @return String
237
     */
238 1
    public function getOrigin() : string
239
    {
240 1
        return $this->origin;
241
    }
242
}