Completed
Branch Message (d15af3)
by Sam
05:12
created

Question::toWire()   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 $type;
33
34
    /**
35
     * @var int
36
     */
37
    private $class;
38
39
    /**
40
     * @return string
41
     */
42
    public function getName(): string
43
    {
44
        return $this->name;
45
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @throws InvalidArgumentException
51
     */
52 4
    public function setName($name): void
53
    {
54 4
        if (!Validator::fullyQualifiedDomainName($name)) {
55
            throw new InvalidArgumentException(sprintf('"%s" is not a fully qualified domain name.', $name));
56
        }
57
58 4
        $this->name = $name;
59 4
    }
60
61
    /**
62
     * @return int
63
     */
64
    public function getType(): int
65
    {
66
        return $this->type;
67
    }
68
69
    /**
70
     * @param string|int $type
71
     *
72
     * @throws UnsupportedTypeException
73
     */
74 4
    public function setType($type): void
75
    {
76 4
        if (is_string($type)) {
77
            $this->type = Types::getTypeCode($type);
78 4
        } elseif (is_int($type)) {
0 ignored issues
show
introduced by
The condition is_int($type) is always true.
Loading history...
79 4
            $this->type = $type;
80
        } else {
81
            throw new UnsupportedTypeException(sprintf('Library does not support type "%s".', $type));
82
        }
83 4
    }
84
85
    /**
86
     * @return int
87
     */
88
    public function getClass(): int
89
    {
90
        return $this->class;
91
    }
92
93
    /**
94
     * @param string|int $class
95
     */
96 4
    public function setClass($class): void
97
    {
98 4
        if (is_string($class)) {
99
            $this->class = Classes::getClassId($class);
100 4
        } elseif (Validator::isUnsignedInteger($class, 16)) {
101 4
            $this->class = $class;
102
        } else {
103
            throw new InvalidArgumentException(sprintf('Invalid class: "%s".', $class));
104
        }
105 4
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function toWire(): string
111
    {
112
        return RdataTrait::encodeName($this->name).pack('nn', $this->type, $this->class);
113
    }
114
115
    /**
116
     * @param string $encoded
117
     * @param int    $offset
118
     *
119
     * @return Question
120
     *
121
     * @throws UnexpectedValueException
122
     * @throws UnsupportedTypeException
123
     */
124 4
    public static function fromWire(string $encoded, int &$offset = 0): Question
125
    {
126 4
        $question = new self();
127 4
        $question->setName(RdataTrait::decodeName($encoded, $offset));
128 4
        $integers = unpack('ntype/nclass', $encoded, $offset);
129 4
        $question->setType($integers['type']);
130 4
        $question->setClass($integers['class']);
131 4
        $offset += 4;
132
133 4
        return $question;
134
    }
135
}
136