Passed
Push — master ( 1b54b9...ff455f )
by Sam
10:10 queued 11s
created

KX::setPreference()   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 1
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
    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
    /**
39
     * @param string $exchanger
40
     */
41 6
    public function setExchanger(string $exchanger): void
42
    {
43 6
        $this->exchanger = $exchanger;
44 6
    }
45
46
    /**
47
     * @return string
48
     */
49 2
    public function getExchanger(): string
50
    {
51 2
        return $this->exchanger;
52
    }
53
54
    /**
55
     * @param int $preference
56
     */
57 6
    public function setPreference(int $preference): void
58
    {
59 6
        $this->preference = $preference;
60 6
    }
61
62
    /**
63
     * @return int
64
     */
65 2
    public function getPreference(): int
66
    {
67 2
        return $this->preference;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @throws \InvalidArgumentException throws exception if preference or exchanger have not been set
74
     */
75 4
    public function toText(): string
76
    {
77 4
        if (null === $this->preference) {
78 1
            throw new \InvalidArgumentException('No preference has been set on KX object.');
79
        }
80 3
        if (null === $this->exchanger) {
81 1
            throw new \InvalidArgumentException('No exchanger has been set on KX object.');
82
        }
83
84 2
        return $this->preference.' '.$this->exchanger;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function toWire(): string
91
    {
92 1
        if (null === $this->preference) {
93
            throw new \InvalidArgumentException('No preference has been set on KX object.');
94
        }
95 1
        if (null === $this->exchanger) {
96
            throw new \InvalidArgumentException('No exchanger has been set on KX object.');
97
        }
98
99 1
        return pack('n', $this->preference).Message::encodeName($this->exchanger);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function fromText(string $text): void
106
    {
107 1
        $rdata = explode(' ', $text);
108 1
        $this->setPreference((int) $rdata[0]);
109 1
        $this->setExchanger($rdata[1]);
110 1
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
116
    {
117 1
        $this->setPreference(unpack('n', $rdata, $offset)[1]);
118 1
        $offset += 2;
119 1
        $this->setExchanger(Message::decodeName($rdata, $offset));
120 1
    }
121
}
122