Completed
Branch Message (499c8f)
by Sam
03:03
created

Question::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
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 2
    public function getName(): string
43
    {
44 2
        return $this->name;
45
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @throws InvalidArgumentException
51
     */
52 8
    public function setName($name): void
53
    {
54 8
        if (!Validator::fullyQualifiedDomainName($name)) {
55
            throw new InvalidArgumentException(sprintf('"%s" is not a fully qualified domain name.', $name));
56
        }
57
58 8
        $this->name = $name;
59 8
    }
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
    public function getType(): string
75
    {
76
        return Types::getName($this->typeCode);
77
    }
78
79
    /**
80
     * @param string|int $typeCode
81
     *
82
     * @throws UnsupportedTypeException
83
     */
84 8
    public function setTypeCode($typeCode): void
85
    {
86 8
        if (is_string($typeCode)) {
87
            $this->typeCode = Types::getTypeCode($typeCode);
88 8
        } elseif (is_int($typeCode)) {
0 ignored issues
show
introduced by
The condition is_int($typeCode) is always true.
Loading history...
89 8
            $this->typeCode = $typeCode;
90
        } else {
91
            throw new UnsupportedTypeException(sprintf('Library does not support type "%s".', $typeCode));
92
        }
93 8
    }
94
95
    /**
96
     * @param string $type
97
     *
98
     * @throws UnsupportedTypeException
99
     */
100 1
    public function setType(string $type): void
101
    {
102 1
        $this->setTypeCode(Types::getTypeCode($type));
103 1
    }
104
105
    /**
106
     * @return int
107
     */
108 2
    public function getClassId(): int
109
    {
110 2
        return $this->classId;
111
    }
112
113
    /**
114
     * @param string|int $classId
115
     *
116
     * @throws \InvalidArgumentException
117
     */
118 8
    public function setClass($classId): void
119
    {
120 8
        if (is_string($classId)) {
121 1
            $this->classId = Classes::getClassId($classId);
122 7
        } elseif (Validator::isUnsignedInteger($classId, 16)) {
123 7
            $this->classId = $classId;
124
        } else {
125
            throw new InvalidArgumentException(sprintf('Invalid class: "%s".', $classId));
126
        }
127 8
    }
128
129
    /**
130
     * @return string
131
     */
132 3
    public function toWire(): string
133
    {
134 3
        return RdataTrait::encodeName($this->name).pack('nn', $this->typeCode, $this->classId);
135
    }
136
137
    /**
138
     * @param string $encoded
139
     * @param int    $offset
140
     *
141
     * @return Question
142
     *
143
     * @throws UnexpectedValueException
144
     * @throws UnsupportedTypeException
145
     */
146 7
    public static function fromWire(string $encoded, int &$offset = 0): Question
147
    {
148 7
        $question = new self();
149 7
        $question->setName(RdataTrait::decodeName($encoded, $offset));
150 7
        $integers = unpack('ntype/nclass', $encoded, $offset);
151 7
        $question->setTypeCode($integers['type']);
152 7
        $question->setClass($integers['class']);
153 7
        $offset += 4;
154
155 7
        return $question;
156
    }
157
}
158