Passed
Push — master ( ddf05f...8c3fc3 )
by Sergey
02:46
created

Node::validate()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.4822
Metric Value
dl 0
loc 29
ccs 11
cts 14
cp 0.7856
rs 6.7272
cc 7
eloc 14
nc 48
nop 0
crap 7.4822
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date  : 4/12/16
5
 * @time  : 1:00 PM
6
 */
7
8
namespace LTDBeget\dns\configurator\zoneEntities;
9
10
use LTDBeget\dns\configurator\errors\ValidationError;
11
use LTDBeget\dns\configurator\traits\RecordsIterateTrait;
12
use LTDBeget\dns\configurator\validators\CnameNumberCheck;
13
use LTDBeget\dns\configurator\validators\ConflictTypesValidator;
14
use LTDBeget\dns\configurator\validators\NodeNameValidator;
15
use LTDBeget\dns\configurator\validators\SoaNumberCheck;
16
use LTDBeget\dns\configurator\Zone;
17
use LTDBeget\dns\configurator\zoneEntities\record\base\Record;
18
use LTDBeget\dns\enums\eErrorCode;
19
use LTDBeget\dns\enums\eRecordNotification;
20
use LTDBeget\dns\enums\eRecordType;
21
22
/**
23
 * Class Node
24
 *
25
 * @package LTDBeget\dns\configurator\zoneEntities
26
 */
27
class Node
28
{
29
    use RecordsIterateTrait;
30
31
    /**
32
     * @var Zone
33
     */
34
    private $zone;
35
    /**
36
     * @var string
37
     */
38
    private $name;
39
    /**
40
     * @var RecordAppender
41
     */
42
    private $recordAppender;
43
    /**
44
     * @var RecordsStore
45
     */
46
    private $recordsStore;
47
    /**
48
     * @var RecordsStore
49
     */
50
    private $removedRecordsStore;
51
52
    /**
53
     * @param Zone $zone
54
     * @param      $name
55
     */
56 1
    public function __construct(Zone $zone, string $name)
57
    {
58 1
        $this->zone                = $zone;
59 1
        $this->name                = mb_strtolower(trim($name));
60 1
        $this->recordAppender      = new RecordAppender($this);
61 1
        $this->recordsStore        = new RecordsStore();
62 1
        $this->removedRecordsStore = new RecordsStore();
63 1
    }
64
65
    /**
66
     * @return RecordAppender
67
     */
68 1
    public function getRecordAppender() : RecordAppender
69
    {
70 1
        return $this->recordAppender;
71
    }
72
73
    /**
74
     * @return Record[]
75
     */
76
    public function iterateRemoved()
77
    {
78
        foreach ($this->getRemovedRecordsStore()->iterate() as $record) {
79
            yield $record;
80
        }
81
    }
82
83
    /**
84
     * @return RecordsStore
85
     */
86
    protected function getRemovedRecordsStore()
87
    {
88
        return $this->removedRecordsStore;
89
    }
90
91
    /**
92
     * @param eRecordType|null $type
93
     * @return Record[]
94
     */
95 1
    public function iterateRecords(eRecordType $type = NULL)
96
    {
97 1
        foreach ($this->getRecordsStore()->iterate($type) as $record) {
98 1
            yield $record;
99
        }
100 1
    }
101
102
    /**
103
     * @param eRecordType|NULL $type
104
     */
105
    public function removeRecords(eRecordType $type = NULL)
106
    {
107
        foreach ($this->iterateRecords($type) as $record) {
108
            $record->remove();
109
        }
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function isEmptyNode() : bool
116
    {
117
        return $this->getRecordsStore()->count() === 0;
118
    }
119
120
    /**
121
     * @return RecordsStore
122
     */
123 1
    protected function getRecordsStore() : RecordsStore
124
    {
125 1
        return $this->recordsStore;
126
    }
127
128
    /**
129
     * @internal
130
     * @param Record              $record
131
     * @param eRecordNotification $notification
132
     * @return Node
133
     */
134 1
    public function notify(Record $record, eRecordNotification $notification) : Node
135
    {
136
        switch ($notification) {
137 1
            case eRecordNotification::ADD:
138 1
                $this->recordsStore->append($record);
139 1
                break;
140
            case eRecordNotification::REMOVE:
141
                $this->recordsStore->remove($record);
142
                $this->removedRecordsStore->append($record);
143
                break;
144
            case eRecordNotification::CHANGE:
145
                $this->getRecordsStore()->change($record);
146
                break;
147
        }
148
149 1
        return $this;
150
    }
151
152 1
    public function sort()
153
    {
154 1
        $this->getRecordsStore()->sort();
155 1
    }
156
157
    /**
158
     * @internal
159
     * Full validate zone via build in validators
160
     * @return bool
161
     */
162 1
    public function validate() : bool
163
    {
164 1
        $errorsStore = $this->getZone()->getErrorsStore();
165
166 1
        if (!ConflictTypesValidator::validate($this)) {
167
            $errorsStore->add(ValidationError::makeNodeError($this, eErrorCode::CONFLICT_RECORD_TYPES_ERROR()));
168
        }
169
170 1
        if (!CnameNumberCheck::validate($this)) {
171
            $errorsStore->add(ValidationError::makeNodeError($this, eErrorCode::MULTIPLE_CNAME_ERROR()));
172
        }
173
174 1
        if (!NodeNameValidator::validate($this->getName())) {
175
            $errorsStore->add(ValidationError::makeNodeError($this, eErrorCode::WRONG_NODE_NAME()));
176
        }
177
178 1
        if ($this->getName() === "@") {
179 1
            if(! SoaNumberCheck::validate($this)) {
180 1
                $errorsStore->add(ValidationError::makeNodeError($this, eErrorCode::SOA_ERROR()));
181
            }
182
        }
183
184 1
        foreach ($this->iterateRecords() as $record) {
185
            /** @noinspection PhpInternalEntityUsedInspection */
186 1
            $record->validate();
187
        }
188
189 1
        return !$errorsStore->isHasErrors();
190
    }
191
192
    /**
193
     * @return Zone
194
     */
195 1
    public function getZone() : Zone
196
    {
197 1
        return $this->zone;
198
    }
199
200
    /**
201
     * @return string
202
     */
203 1
    public function getName() : string
204
    {
205 1
        return $this->name;
206
    }
207
}