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

KX::fromWire()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
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
/**
17
 * {@link https://tools.ietf.org/html/rfc2230}.
18
 */
19
class KX implements RdataInterface
20
{
21
    use RdataTrait;
22
23
    const TYPE = 'KX';
24
    const TYPE_CODE = 36;
25
26
    /**
27
     * @var int
28
     */
29
    private $preference;
30
31
    /**
32
     * @var string
33
     */
34
    private $exchanger;
35
36
    /**
37
     * @param string $exchanger
38
     */
39 5
    public function setExchanger(string $exchanger): void
40
    {
41 5
        $this->exchanger = $exchanger;
42 5
    }
43
44
    /**
45
     * @return string
46
     */
47 2
    public function getExchanger(): string
48
    {
49 2
        return $this->exchanger;
50
    }
51
52
    /**
53
     * @param int $preference
54
     */
55 5
    public function setPreference(int $preference): void
56
    {
57 5
        $this->preference = $preference;
58 5
    }
59
60
    /**
61
     * @return int
62
     */
63 2
    public function getPreference(): int
64
    {
65 2
        return $this->preference;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @throws \InvalidArgumentException throws exception if preference or exchanger have not been set
72
     */
73 3
    public function toText(): string
74
    {
75 3
        if (null === $this->preference) {
76 1
            throw new \InvalidArgumentException('No preference has been set on KX object.');
77
        }
78 2
        if (null === $this->exchanger) {
79 1
            throw new \InvalidArgumentException('No exchanger has been set on KX object.');
80
        }
81
82 1
        return $this->preference.' '.$this->exchanger;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 1
    public function toWire(): string
89
    {
90 1
        if (null === $this->preference) {
91
            throw new \InvalidArgumentException('No preference has been set on KX object.');
92
        }
93 1
        if (null === $this->exchanger) {
94
            throw new \InvalidArgumentException('No exchanger has been set on KX object.');
95
        }
96
97 1
        return pack('n', $this->preference).self::encodeName($this->exchanger);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public static function fromText(string $text): RdataInterface
104
    {
105 1
        $rdata = explode(' ', $text);
106 1
        $kx = new self();
107 1
        $kx->setPreference((int) $rdata[0]);
108 1
        $kx->setExchanger($rdata[1]);
109
110 1
        return $kx;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1
    public static function fromWire(string $rdata): RdataInterface
117
    {
118 1
        $offset = 2;
119 1
        $kx = new self();
120 1
        $kx->setPreference(unpack('n', $rdata)[1]);
121 1
        $kx->setexchanger(self::decodeName($rdata, $offset));
122
123 1
        return $kx;
124
    }
125
}
126