Completed
Branch master (f0bd78)
by Sam
04:24 queued 01:02
created

Question::getName()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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;
15
16
use Badcow\DNS\Rdata\RdataTrait;
17
use Badcow\DNS\Rdata\Types;
18
use Badcow\DNS\Rdata\UnsupportedTypeException;
19
use InvalidArgumentException;
20
use UnexpectedValueException;
21
22
class Question
23
{
24
    /**
25
     * @var string
26
     */
27
    private $name;
28
29
    /**
30
     * @var int
31
     */
32
    private $typeCode;
33
34
    /**
35
     * @var int
36
     */
37
    private $classId;
38
39
    /**
40
     * @return string
41
     */
42 3
    public function getName(): string
43
    {
44 3
        return $this->name;
45
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @throws InvalidArgumentException
51
     */
52 11
    public function setName($name): void
53
    {
54 11
        if (!Validator::fullyQualifiedDomainName($name)) {
55 1
            throw new InvalidArgumentException(sprintf('"%s" is not a fully qualified domain name.', $name));
56
        }
57
58 10
        $this->name = $name;
59 10
    }
60
61
    /**
62
     * @return int
63
     */
64 2
    public function getTypeCode(): int
65
    {
66 2
        return $this->typeCode;
67
    }
68
69
    /**
70
     * @return string
71
     *
72
     * @throws UnsupportedTypeException
73
     */
74 1
    public function getType(): string
75
    {
76 1
        return Types::getName($this->typeCode);
77
    }
78
79
    /**
80
     * @param int $typeCode
81
     *
82
     * @throws \DomainException
83
     */
84 11
    public function setTypeCode(int $typeCode): void
85
    {
86 11
        if (!Validator::isUnsignedInteger($typeCode, 16)) {
87 1
            throw new \DomainException(sprintf('TypeCode must be an unsigned 16-bit integer. "%d" given.', $typeCode));
88
        }
89 10
        $this->typeCode = $typeCode;
90 10
    }
91
92
    /**
93
     * @param string $type
94
     *
95
     * @throws UnsupportedTypeException
96
     */
97 2
    public function setType(string $type): void
98
    {
99 2
        $this->setTypeCode(Types::getTypeCode($type));
100 2
    }
101
102
    /**
103
     * @return int
104
     */
105 2
    public function getClassId(): int
106
    {
107 2
        return $this->classId;
108
    }
109
110
    /**
111
     * @return string
112
     */
113 1
    public function getClass(): string
114
    {
115 1
        return Classes::getClassName($this->classId);
116
    }
117
118
    /**
119
     * @param int $classId
120
     *
121
     * @throws \InvalidArgumentException
122
     */
123 11
    public function setClassId(int $classId): void
124
    {
125 11
        if (!Validator::isUnsignedInteger($classId, 16)) {
126 1
            throw new InvalidArgumentException(sprintf('Invalid class: "%s".', $classId));
127
        }
128
129 10
        $this->classId = $classId;
130 10
    }
131
132
    /**
133
     * @param string $class
134
     */
135 2
    public function setClass(string $class): void
136
    {
137 2
        $this->setClassId(Classes::getClassId($class));
138 2
    }
139
140
    /**
141
     * @return string
142
     */
143 4
    public function toWire(): string
144
    {
145 4
        return RdataTrait::encodeName($this->name).pack('nn', $this->typeCode, $this->classId);
146
    }
147
148
    /**
149
     * @param string $encoded
150
     * @param int    $offset
151
     *
152
     * @return Question
153
     *
154
     * @throws UnexpectedValueException
155
     * @throws UnsupportedTypeException
156
     */
157 8
    public static function fromWire(string $encoded, int &$offset = 0): Question
158
    {
159 8
        $question = new self();
160 8
        $question->setName(RdataTrait::decodeName($encoded, $offset));
161 8
        $integers = unpack('ntype/nclass', $encoded, $offset);
162 8
        $question->setTypeCode($integers['type']);
163 8
        $question->setClassId($integers['class']);
164 8
        $offset += 4;
165
166 8
        return $question;
167
    }
168
}
169