Passed
Branch master (8940db)
by Sam
02:38
created

SOA::setRetry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Parser\Tokens;
17
18
/**
19
 * @see https://tools.ietf.org/html/rfc1035#section-3.3.13
20
 */
21
class SOA implements RdataInterface
22
{
23
    use RdataTrait;
24
25
    const TYPE = 'SOA';
26
    const TYPE_CODE = 6;
27
28
    /**
29
     * The <domain-name> of the name server that was the
30
     * original or primary source of data for this zone.
31
     *
32
     * @var string|null
33
     */
34
    private $mname;
35
36
    /**
37
     * A <domain-name> which specifies the mailbox of the
38
     * person responsible for this zone.
39
     *
40
     * @var string|null
41
     */
42
    private $rname;
43
44
    /**
45
     * The unsigned 32 bit version number of the original copy
46
     * of the zone.
47
     *
48
     * @var int|null
49
     */
50
    private $serial;
51
52
    /**
53
     * A 32 bit time interval before the zone should be
54
     * refreshed.
55
     *
56
     * @var int|null
57
     */
58
    private $refresh;
59
60
    /**
61
     * A 32 bit time interval that should elapse before a
62
     * failed refresh should be retried.
63
     *
64
     * @var int|null
65
     */
66
    private $retry;
67
68
    /**
69
     * A 32 bit time value that specifies the upper limit on
70
     * the time interval that can elapse before the zone is no
71
     * longer authoritative.
72
     *
73
     * @var int|null
74
     */
75
    private $expire;
76
77
    /**
78
     * The unsigned 32 bit minimum TTL field that should be
79
     * exported with any RR from this zone.
80
     *
81
     * @var int|null
82
     */
83
    private $minimum;
84
85
    /**
86
     * @param int $expire
87
     */
88 24
    public function setExpire(int $expire): void
89
    {
90 24
        $this->expire = $expire;
91 24
    }
92
93
    /**
94
     * @return int
95
     */
96 5
    public function getExpire(): ?int
97
    {
98 5
        return $this->expire;
99
    }
100
101
    /**
102
     * @param int $minimum
103
     */
104 24
    public function setMinimum(int $minimum): void
105
    {
106 24
        $this->minimum = $minimum;
107 24
    }
108
109
    /**
110
     * @return int
111
     */
112 5
    public function getMinimum(): ?int
113
    {
114 5
        return $this->minimum;
115
    }
116
117
    /**
118
     * @param string $mname
119
     */
120 24
    public function setMname(string $mname): void
121
    {
122 24
        $this->mname = $mname;
123 24
    }
124
125
    /**
126
     * @return string
127
     */
128 5
    public function getMname(): ?string
129
    {
130 5
        return $this->mname;
131
    }
132
133
    /**
134
     * @param int $refresh
135
     */
136 24
    public function setRefresh(int $refresh): void
137
    {
138 24
        $this->refresh = $refresh;
139 24
    }
140
141
    /**
142
     * @return int
143
     */
144 5
    public function getRefresh(): ?int
145
    {
146 5
        return $this->refresh;
147
    }
148
149
    /**
150
     * @param int $retry
151
     */
152 24
    public function setRetry(int $retry): void
153
    {
154 24
        $this->retry = (int) $retry;
155 24
    }
156
157
    /**
158
     * @return int
159
     */
160 5
    public function getRetry(): ?int
161
    {
162 5
        return $this->retry;
163
    }
164
165
    /**
166
     * @param string $rname
167
     */
168 24
    public function setRname(string $rname): void
169
    {
170 24
        $this->rname = $rname;
171 24
    }
172
173
    /**
174
     * @return string
175
     */
176 5
    public function getRname(): ?string
177
    {
178 5
        return $this->rname;
179
    }
180
181
    /**
182
     * @param int $serial
183
     */
184 24
    public function setSerial(int $serial): void
185
    {
186 24
        $this->serial = $serial;
187 24
    }
188
189
    /**
190
     * @return int
191
     */
192 5
    public function getSerial(): ?int
193
    {
194 5
        return $this->serial;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 8
    public function toText(): string
201
    {
202 8
        if (!isset($this->mname) ||
203 8
            !isset($this->rname) ||
204 8
            !isset($this->serial) ||
205 8
            !isset($this->refresh) ||
206 8
            !isset($this->retry) ||
207 8
            !isset($this->expire) ||
208 8
            !isset($this->minimum)) {
209
            throw new \InvalidArgumentException('All parameters of SOA must be set.');
210
        }
211
212 8
        return sprintf(
213 8
            '%s %s %s %s %s %s %s',
214 8
            $this->mname,
215 8
            $this->rname,
216 8
            $this->serial,
217 8
            $this->refresh,
218 8
            $this->retry,
219 8
            $this->expire,
220 8
            $this->minimum
221
        );
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 1
    public function toWire(): string
228
    {
229 1
        if (!isset($this->mname) ||
230 1
            !isset($this->rname) ||
231 1
            !isset($this->serial) ||
232 1
            !isset($this->refresh) ||
233 1
            !isset($this->retry) ||
234 1
            !isset($this->expire) ||
235 1
            !isset($this->minimum)) {
236
            throw new \InvalidArgumentException('All parameters of SOA must be set.');
237
        }
238
239
        return
240 1
            self::encodeName($this->mname).
0 ignored issues
show
Bug introduced by
It seems like $this->mname can also be of type null; however, parameter $name of Badcow\DNS\Rdata\SOA::encodeName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

240
            self::encodeName(/** @scrutinizer ignore-type */ $this->mname).
Loading history...
241 1
            self::encodeName($this->rname).
242 1
            pack(
243 1
                'NNNNN',
244 1
                $this->serial,
245 1
                $this->refresh,
246 1
                $this->retry,
247 1
                $this->expire,
248 1
                $this->minimum
249
            );
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 9
    public static function fromText(string $text): RdataInterface
256
    {
257 9
        $rdata = explode(Tokens::SPACE, $text);
258 9
        $soa = new self();
259 9
        $soa->setMname($rdata[0]);
260 9
        $soa->setRname($rdata[1]);
261 9
        $soa->setSerial((int) $rdata[2]);
262 9
        $soa->setRefresh((int) $rdata[3]);
263 9
        $soa->setRetry((int) $rdata[4]);
264 9
        $soa->setExpire((int) $rdata[5]);
265 9
        $soa->setMinimum((int) $rdata[6]);
266
267 9
        return $soa;
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273 1
    public static function fromWire(string $string): RdataInterface
274
    {
275 1
        $offset = 0;
276 1
        $rdata = array_merge(
277
            [
278 1
                'mname' => self::decodeName($string, $offset),
279 1
                'rname' => self::decodeName($string, $offset),
280
            ],
281 1
            unpack('Nserial/Nrefresh/Nretry/Nexpire/Nminimum', substr($string, $offset))
0 ignored issues
show
Bug introduced by
It seems like unpack('Nserial/Nrefresh...bstr($string, $offset)) can also be of type false; however, parameter $array2 of array_merge() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

281
            /** @scrutinizer ignore-type */ unpack('Nserial/Nrefresh/Nretry/Nexpire/Nminimum', substr($string, $offset))
Loading history...
282
        );
283
284 1
        $soa = new self();
285 1
        $soa->setMname($rdata['mname']);
286 1
        $soa->setRname($rdata['rname']);
287 1
        $soa->setSerial((int) $rdata['serial']);
288 1
        $soa->setRefresh((int) $rdata['refresh']);
289 1
        $soa->setRetry((int) $rdata['retry']);
290 1
        $soa->setExpire((int) $rdata['expire']);
291 1
        $soa->setMinimum((int) $rdata['minimum']);
292
293 1
        return $soa;
294
    }
295
}
296