SOA::getSerial()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Rdata;
15
16
use Badcow\DNS\Message;
17
use Badcow\DNS\Parser\TimeFormat;
18
use Badcow\DNS\Parser\Tokens;
19
use InvalidArgumentException;
20
21
/**
22
 * @see https://tools.ietf.org/html/rfc1035#section-3.3.13
23
 */
24
class SOA implements RdataInterface
25
{
26 1
    use RdataTrait;
27
28
    const TYPE = 'SOA';
29
    const TYPE_CODE = 6;
30
31
    /**
32
     * The <domain-name> of the name server that was the
33
     * original or primary source of data for this zone.
34
     *
35
     * @var string|null
36
     */
37
    private $mname;
38
39
    /**
40
     * A <domain-name> which specifies the mailbox of the
41
     * person responsible for this zone.
42
     *
43
     * @var string|null
44
     */
45
    private $rname;
46
47
    /**
48
     * The unsigned 32 bit version number of the original copy
49
     * of the zone.
50
     *
51
     * @var int|null
52
     */
53
    private $serial;
54
55
    /**
56
     * A 32 bit time interval before the zone should be
57
     * refreshed.
58
     *
59
     * @var int|null
60
     */
61
    private $refresh;
62
63
    /**
64
     * A 32 bit time interval that should elapse before a
65
     * failed refresh should be retried.
66
     *
67
     * @var int|null
68
     */
69
    private $retry;
70
71
    /**
72
     * A 32 bit time value that specifies the upper limit on
73
     * the time interval that can elapse before the zone is no
74
     * longer authoritative.
75
     *
76
     * @var int|null
77
     */
78
    private $expire;
79
80
    /**
81
     * The unsigned 32 bit minimum TTL field that should be
82
     * exported with any RR from this zone.
83
     *
84
     * @var int|null
85
     */
86
    private $minimum;
87
88 31
    public function setExpire(int $expire): void
89
    {
90 31
        $this->expire = $expire;
91 31
    }
92
93
    /**
94
     * @return int
95
     */
96 6
    public function getExpire(): ?int
97
    {
98 6
        return $this->expire;
99
    }
100
101 31
    public function setMinimum(int $minimum): void
102
    {
103 31
        $this->minimum = $minimum;
104 31
    }
105
106
    /**
107
     * @return int
108
     */
109 6
    public function getMinimum(): ?int
110
    {
111 6
        return $this->minimum;
112
    }
113
114 31
    public function setMname(string $mname): void
115
    {
116 31
        $this->mname = $mname;
117 31
    }
118
119
    /**
120
     * @return string
121
     */
122 5
    public function getMname(): ?string
123
    {
124 5
        return $this->mname;
125
    }
126
127 31
    public function setRefresh(int $refresh): void
128
    {
129 31
        $this->refresh = $refresh;
130 31
    }
131
132
    /**
133
     * @return int
134
     */
135 6
    public function getRefresh(): ?int
136
    {
137 6
        return $this->refresh;
138
    }
139
140 31
    public function setRetry(int $retry): void
141
    {
142 31
        $this->retry = (int) $retry;
143 31
    }
144
145
    /**
146
     * @return int
147
     */
148 6
    public function getRetry(): ?int
149
    {
150 6
        return $this->retry;
151
    }
152
153 31
    public function setRname(string $rname): void
154
    {
155 31
        $this->rname = $rname;
156 31
    }
157
158
    /**
159
     * @return string
160
     */
161 5
    public function getRname(): ?string
162
    {
163 5
        return $this->rname;
164
    }
165
166 31
    public function setSerial(int $serial): void
167
    {
168 31
        $this->serial = $serial;
169 31
    }
170
171
    /**
172
     * @return int
173
     */
174 5
    public function getSerial(): ?int
175
    {
176 5
        return $this->serial;
177
    }
178
179 11
    public function toText(): string
180
    {
181 11
        if (!isset($this->mname) ||
182 11
            !isset($this->rname) ||
183 11
            !isset($this->serial) ||
184 11
            !isset($this->refresh) ||
185 11
            !isset($this->retry) ||
186 11
            !isset($this->expire) ||
187 11
            !isset($this->minimum)) {
188
            throw new InvalidArgumentException('All parameters of SOA must be set.');
189
        }
190
191 11
        return sprintf(
192 11
            '%s %s %s %s %s %s %s',
193 11
            $this->mname,
194 11
            $this->rname,
195 11
            $this->serial,
196 11
            $this->refresh,
197 11
            $this->retry,
198 11
            $this->expire,
199 11
            $this->minimum
200
        );
201
    }
202
203 1
    public function toWire(): string
204
    {
205 1
        if (!isset($this->mname) ||
206 1
            !isset($this->rname) ||
207 1
            !isset($this->serial) ||
208 1
            !isset($this->refresh) ||
209 1
            !isset($this->retry) ||
210 1
            !isset($this->expire) ||
211 1
            !isset($this->minimum)) {
212
            throw new InvalidArgumentException('All parameters of SOA must be set.');
213
        }
214
215
        return
216 1
            Message::encodeName($this->mname).
217 1
            Message::encodeName($this->rname).
218 1
            pack(
219 1
                'NNNNN',
220 1
                $this->serial,
221 1
                $this->refresh,
222 1
                $this->retry,
223 1
                $this->expire,
224 1
                $this->minimum
225
            );
226
    }
227
228 15
    public function fromText(string $text): void
229
    {
230 15
        $rdata = explode(Tokens::SPACE, $text);
231
232 15
        $this->setMname($rdata[0]);
233 15
        $this->setRname($rdata[1]);
234 15
        $this->setSerial((int) $rdata[2]);
235 15
        $this->setRefresh(TimeFormat::toSeconds($rdata[3]));
236 15
        $this->setRetry(TimeFormat::toSeconds($rdata[4]));
237 15
        $this->setExpire(TimeFormat::toSeconds($rdata[5]));
238 15
        $this->setMinimum(TimeFormat::toSeconds($rdata[6]));
239 15
    }
240
241 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
242
    {
243 1
        $this->setMname(Message::decodeName($rdata, $offset));
244 1
        $this->setRname(Message::decodeName($rdata, $offset));
245 1
        if (false === $parameters = unpack('Nserial/Nrefresh/Nretry/Nexpire/Nminimum', $rdata, $offset)) {
246
            throw new DecodeException(static::TYPE, $rdata);
247 1
        }
248 1
249 1
        $this->setSerial((int) $parameters['serial']);
250 1
        $this->setRefresh(TimeFormat::toSeconds($parameters['refresh']));
251 1
        $this->setRetry(TimeFormat::toSeconds($parameters['retry']));
252
        $this->setExpire(TimeFormat::toSeconds($parameters['expire']));
253 1
        $this->setMinimum(TimeFormat::toSeconds($parameters['minimum']));
254 1
255
        $offset += 20;
256
    }
257
}
258