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

AFSDB   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 99
ccs 30
cts 30
cp 1
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubType() 0 3 1
A toText() 0 11 3
A setSubType() 0 3 1
A getHostname() 0 3 1
A fromWire() 0 8 1
A toWire() 0 3 1
A setHostname() 0 3 1
A fromText() 0 8 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\Rdata;
15
16
class AFSDB implements RdataInterface
17
{
18
    use RdataTrait;
19
20
    const TYPE = 'AFSDB';
21
    const TYPE_CODE = 18;
22
23
    /**
24
     * @var int
25
     */
26
    private $subType;
27
28
    /**
29
     * @var string
30
     */
31
    private $hostname;
32
33
    /**
34
     * @return int
35
     */
36 1
    public function getSubType(): int
37
    {
38 1
        return $this->subType;
39
    }
40
41
    /**
42
     * @param int $subType
43
     */
44 4
    public function setSubType(int $subType): void
45
    {
46 4
        $this->subType = $subType;
47 4
    }
48
49
    /**
50
     * @return string
51
     */
52 1
    public function getHostname(): string
53
    {
54 1
        return $this->hostname;
55
    }
56
57
    /**
58
     * @param string $hostname
59
     */
60 4
    public function setHostname(string $hostname): void
61
    {
62 4
        $this->hostname = $hostname;
63 4
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @throws \InvalidArgumentException
69
     */
70 3
    public function toText(): string
71
    {
72 3
        if (!isset($this->subType)) {
73 1
            throw new \InvalidArgumentException('No sub-type has been set on AFSDB object.');
74
        }
75
76 2
        if (!isset($this->hostname)) {
77 1
            throw new \InvalidArgumentException('No hostname has been set on AFSDB object.');
78
        }
79
80 1
        return sprintf('%d %s', $this->subType, $this->hostname);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 1
    public function toWire(): string
87
    {
88 1
        return pack('n', $this->subType).self::encodeName($this->hostname);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public static function fromText(string $text): RdataInterface
95
    {
96 1
        $rdata = explode(' ', $text);
97 1
        $afsdb = new self();
98 1
        $afsdb->setSubType((int) $rdata[0]);
99 1
        $afsdb->setHostname($rdata[1]);
100
101 1
        return $afsdb;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public static function fromWire(string $rdata): RdataInterface
108
    {
109 1
        $offset = 2;
110 1
        $afsdb = new self();
111 1
        $afsdb->setSubType(unpack('n', $rdata)[1]);
112 1
        $afsdb->setHostname(self::decodeName($rdata, $offset));
113
114 1
        return $afsdb;
115
    }
116
}
117