Completed
Push — master ( fe5fd9...28190b )
by Sam
07:00 queued 04:19
created

SOA::toText()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.0109

Importance

Changes 0
Metric Value
cc 8
eloc 17
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 21
ccs 17
cts 18
cp 0.9444
crap 8.0109
rs 8.4444
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 28
    public function setExpire(int $expire): void
89
    {
90 28
        $this->expire = $expire;
91 28
    }
92
93
    /**
94
     * @return int
95
     */
96 6
    public function getExpire(): ?int
97
    {
98 6
        return $this->expire;
99
    }
100
101 28
    public function setMinimum(int $minimum): void
102
    {
103 28
        $this->minimum = $minimum;
104 28
    }
105
106
    /**
107
     * @return int
108
     */
109 6
    public function getMinimum(): ?int
110
    {
111 6
        return $this->minimum;
112
    }
113
114 28
    public function setMname(string $mname): void
115
    {
116 28
        $this->mname = $mname;
117 28
    }
118
119
    /**
120
     * @return string
121
     */
122 5
    public function getMname(): ?string
123
    {
124 5
        return $this->mname;
125
    }
126
127 28
    public function setRefresh(int $refresh): void
128
    {
129 28
        $this->refresh = $refresh;
130 28
    }
131
132
    /**
133
     * @return int
134
     */
135 6
    public function getRefresh(): ?int
136
    {
137 6
        return $this->refresh;
138
    }
139
140 28
    public function setRetry(int $retry): void
141
    {
142 28
        $this->retry = (int) $retry;
143 28
    }
144
145
    /**
146
     * @return int
147
     */
148 6
    public function getRetry(): ?int
149
    {
150 6
        return $this->retry;
151
    }
152
153 28
    public function setRname(string $rname): void
154
    {
155 28
        $this->rname = $rname;
156 28
    }
157
158
    /**
159
     * @return string
160
     */
161 5
    public function getRname(): ?string
162
    {
163 5
        return $this->rname;
164
    }
165
166 28
    public function setSerial(int $serial): void
167
    {
168 28
        $this->serial = $serial;
169 28
    }
170
171
    /**
172
     * @return int
173
     */
174 5
    public function getSerial(): ?int
175
    {
176 5
        return $this->serial;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 8
    public function toText(): string
183
    {
184 8
        if (!isset($this->mname) ||
185 8
            !isset($this->rname) ||
186 8
            !isset($this->serial) ||
187 8
            !isset($this->refresh) ||
188 8
            !isset($this->retry) ||
189 8
            !isset($this->expire) ||
190 8
            !isset($this->minimum)) {
191
            throw new InvalidArgumentException('All parameters of SOA must be set.');
192
        }
193
194 8
        return sprintf(
195 8
            '%s %s %s %s %s %s %s',
196 8
            $this->mname,
197 8
            $this->rname,
198 8
            $this->serial,
199 8
            $this->refresh,
200 8
            $this->retry,
201 8
            $this->expire,
202 8
            $this->minimum
203
        );
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209 1
    public function toWire(): string
210
    {
211 1
        if (!isset($this->mname) ||
212 1
            !isset($this->rname) ||
213 1
            !isset($this->serial) ||
214 1
            !isset($this->refresh) ||
215 1
            !isset($this->retry) ||
216 1
            !isset($this->expire) ||
217 1
            !isset($this->minimum)) {
218
            throw new InvalidArgumentException('All parameters of SOA must be set.');
219
        }
220
221
        return
222 1
            Message::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\Message::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

222
            Message::encodeName(/** @scrutinizer ignore-type */ $this->mname).
Loading history...
223 1
            Message::encodeName($this->rname).
224 1
            pack(
225 1
                'NNNNN',
226 1
                $this->serial,
227 1
                $this->refresh,
228 1
                $this->retry,
229 1
                $this->expire,
230 1
                $this->minimum
231
            );
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237 12
    public function fromText(string $text): void
238
    {
239 12
        $rdata = explode(Tokens::SPACE, $text);
240
241 12
        $this->setMname($rdata[0]);
242 12
        $this->setRname($rdata[1]);
243 12
        $this->setSerial((int) $rdata[2]);
244 12
        $this->setRefresh(TimeFormat::toSeconds($rdata[3]));
245 12
        $this->setRetry(TimeFormat::toSeconds($rdata[4]));
246 12
        $this->setExpire(TimeFormat::toSeconds($rdata[5]));
247 12
        $this->setMinimum(TimeFormat::toSeconds($rdata[6]));
248 12
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
254
    {
255 1
        $this->setMname(Message::decodeName($rdata, $offset));
256 1
        $this->setRname(Message::decodeName($rdata, $offset));
257 1
        $parameters = unpack('Nserial/Nrefresh/Nretry/Nexpire/Nminimum', $rdata, $offset);
258
259 1
        $this->setSerial((int) $parameters['serial']);
260 1
        $this->setRefresh(TimeFormat::toSeconds($parameters['refresh']));
261 1
        $this->setRetry(TimeFormat::toSeconds($parameters['retry']));
262 1
        $this->setExpire(TimeFormat::toSeconds($parameters['expire']));
263 1
        $this->setMinimum(TimeFormat::toSeconds($parameters['minimum']));
264
265 1
        $offset += 20;
266 1
    }
267
}
268