Passed
Push — master ( bb44e9...3f3f7b )
by Sergey
02:48
created

Node::getRecordAppender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

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