Passed
Push — master ( cf3275...8940db )
by Sam
49s queued 11s
created

AFSDB::toText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
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\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