Completed
Push — master ( fe5fd9...28190b )
by Sam
07:00 queued 04:19
created

KX::getPreference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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