Question::setClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\DecodeException;
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 3
39
    public function getName(): string
40 3
    {
41
        return $this->name;
42
    }
43
44
    /**
45
     * @param string $name
46
     *
47
     * @throws InvalidArgumentException
48 11
     */
49
    public function setName($name): void
50 11
    {
51 1
        if (!Validator::fullyQualifiedDomainName($name)) {
52
            throw new InvalidArgumentException(sprintf('"%s" is not a fully qualified domain name.', $name));
53
        }
54 10
55 10
        $this->name = $name;
56
    }
57 2
58
    public function getTypeCode(): int
59 2
    {
60
        return $this->typeCode;
61
    }
62
63
    /**
64
     * @throws UnsupportedTypeException
65 1
     */
66
    public function getType(): string
67 1
    {
68
        return Types::getName($this->typeCode);
69
    }
70
71
    /**
72
     * @throws \DomainException
73 11
     */
74
    public function setTypeCode(int $typeCode): void
75 11
    {
76 1
        if (!Validator::isUnsignedInteger($typeCode, 16)) {
77
            throw new \DomainException(sprintf('TypeCode must be an unsigned 16-bit integer. "%d" given.', $typeCode));
78 10
        }
79 10
        $this->typeCode = $typeCode;
80
    }
81
82
    /**
83
     * @throws UnsupportedTypeException
84 2
     */
85
    public function setType(string $type): void
86 2
    {
87 2
        $this->setTypeCode(Types::getTypeCode($type));
88
    }
89 2
90
    public function getClassId(): int
91 2
    {
92
        return $this->classId;
93
    }
94 1
95
    public function getClass(): string
96 1
    {
97
        return Classes::getClassName($this->classId);
98
    }
99
100
    /**
101
     * @throws \InvalidArgumentException
102 11
     */
103
    public function setClassId(int $classId): void
104 11
    {
105 1
        if (!Validator::isUnsignedInteger($classId, 16)) {
106
            throw new InvalidArgumentException(sprintf('Invalid class: "%s".', $classId));
107
        }
108 10
109 10
        $this->classId = $classId;
110
    }
111 2
112
    public function setClass(string $class): void
113 2
    {
114 2
        $this->setClassId(Classes::getClassId($class));
115
    }
116 4
117
    public function toWire(): string
118 4
    {
119
        return Message::encodeName($this->name).pack('nn', $this->typeCode, $this->classId);
120
    }
121
122
    /**
123
     * @throws UnexpectedValueException
124
     * @throws UnsupportedTypeException
125 8
     */
126
    public static function fromWire(string $encoded, int &$offset = 0): Question
127 8
    {
128 8
        $question = new self();
129 8
        $question->setName(Message::decodeName($encoded, $offset));
130 8 View Code Duplication
        if (false === $integers = unpack('ntype/nclass', $encoded, $offset)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131 8
            throw new \UnexpectedValueException(sprintf('Malformed DNS query encountered. "%s"', DecodeException::binaryToHex($encoded)));
132 8
        }
133
        $question->setTypeCode($integers['type']);
134 8
        $question->setClassId($integers['class']);
135
        $offset += 4;
136
137
        return $question;
138
    }
139
}
140