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

NSEC3PARAM::getFlags()   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 0
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
use Badcow\DNS\Validator;
18
19
/**
20
 * {@link https://tools.ietf.org/html/rfc5155#section-4}.
21
 */
22
class NSEC3PARAM implements RdataInterface
23
{
24
    use RdataTrait;
25
26
    const TYPE = 'NSEC3PARAM';
27
    const TYPE_CODE = 51;
28
29
    /**
30
     * @var int
31
     */
32
    private $hashAlgorithm;
33
34
    /**
35
     * @var int
36
     */
37
    private $flags = 0;
38
39
    /**
40
     * @var int
41
     */
42
    private $iterations;
43
44
    /**
45
     * @var string Binary encoded string
46
     */
47
    private $salt;
48
49
    /**
50
     * @return int
51
     */
52 1
    public function getHashAlgorithm(): int
53
    {
54 1
        return $this->hashAlgorithm;
55
    }
56
57
    /**
58
     * @param int $hashAlgorithm
59
     *
60
     * @throws \InvalidArgumentException
61
     */
62 5
    public function setHashAlgorithm(int $hashAlgorithm): void
63
    {
64 5
        if (!Validator::isUnsignedInteger($hashAlgorithm, 8)) {
65
            throw new \InvalidArgumentException('Hash algorithm must be 8-bit integer.');
66
        }
67 5
        $this->hashAlgorithm = $hashAlgorithm;
68 5
    }
69
70
    /**
71
     * @return int
72
     */
73 1
    public function getFlags(): int
74
    {
75 1
        return $this->flags;
76
    }
77
78
    /**
79
     * @param int $flags
80
     *
81
     * @throws \InvalidArgumentException
82
     */
83 5
    public function setFlags(int $flags): void
84
    {
85 5
        if (!Validator::isUnsignedInteger($flags, 8)) {
86
            throw new \InvalidArgumentException('Flags must be an 8-bit unsigned integer.');
87
        }
88 5
        $this->flags = $flags;
89 5
    }
90
91
    /**
92
     * @return int
93
     */
94 1
    public function getIterations(): int
95
    {
96 1
        return $this->iterations;
97
    }
98
99
    /**
100
     * @param int $iterations
101
     */
102 5
    public function setIterations(int $iterations): void
103
    {
104 5
        if (!Validator::isUnsignedInteger($iterations, 16)) {
105
            throw new \InvalidArgumentException('Hash algorithm must be 16-bit integer.');
106
        }
107 5
        $this->iterations = $iterations;
108 5
    }
109
110
    /**
111
     * @return string Base16 string
112
     */
113 1
    public function getSalt(): string
114
    {
115 1
        return bin2hex($this->salt);
116
    }
117
118
    /**
119
     * @param string $salt Hexadecimal string
120
     */
121 5
    public function setSalt(string $salt): void
122
    {
123 5
        if (false === $bin = @hex2bin($salt)) {
124
            throw new \InvalidArgumentException('Salt must be a hexadecimal string.');
125
        }
126 5
        $this->salt = $bin;
127 5
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1
    public function toText(): string
133
    {
134 1
        return sprintf('%d %d %d %s', $this->hashAlgorithm, $this->flags, $this->iterations, bin2hex($this->salt));
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 1
    public function toWire(): string
141
    {
142 1
        return pack('CCnC', $this->hashAlgorithm, $this->flags, $this->iterations, strlen($this->salt)).$this->salt;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     *
148
     * @return NSEC3PARAM
149
     */
150 1
    public static function fromText(string $text): RdataInterface
151
    {
152 1
        $rdata = explode(Tokens::SPACE, $text);
153 1
        $nsec3param = new self();
154 1
        $nsec3param->setHashAlgorithm((int) array_shift($rdata));
155 1
        $nsec3param->setFlags((int) array_shift($rdata));
156 1
        $nsec3param->setIterations((int) array_shift($rdata));
157 1
        $nsec3param->setSalt((string) array_shift($rdata));
158
159 1
        return $nsec3param;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     *
165
     * @return NSEC3PARAM
166
     */
167 1
    public static function fromWire(string $rdata): RdataInterface
168
    {
169 1
        $integers = unpack('C<algorithm>/C<flags>/n<iterations>/C<saltLen>', $rdata);
170 1
        $nsec3param = new self();
171 1
        $nsec3param->setHashAlgorithm($integers['<algorithm>']);
172 1
        $nsec3param->setFlags($integers['<flags>']);
173 1
        $nsec3param->setIterations($integers['<iterations>']);
174
175 1
        $saltBin = substr($rdata, 5, $integers['<saltLen>']);
176 1
        $nsec3param->setSalt(bin2hex($saltBin));
177
178 1
        return $nsec3param;
179
    }
180
}
181