KX::fromWire()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
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
     * @throws \InvalidArgumentException throws exception if preference or exchanger have not been set
60
     */
61 4
    public function toText(): string
62
    {
63 4
        if (null === $this->preference) {
64 1
            throw new \InvalidArgumentException('No preference has been set on KX object.');
65
        }
66 3
        if (null === $this->exchanger) {
67 1
            throw new \InvalidArgumentException('No exchanger has been set on KX object.');
68
        }
69
70 2
        return $this->preference.' '.$this->exchanger;
71
    }
72
73 1
    public function toWire(): string
74
    {
75 1
        if (null === $this->preference) {
76
            throw new \InvalidArgumentException('No preference has been set on KX object.');
77
        }
78 1
        if (null === $this->exchanger) {
79
            throw new \InvalidArgumentException('No exchanger has been set on KX object.');
80
        }
81
82 1
        return pack('n', $this->preference).Message::encodeName($this->exchanger);
83
    }
84
85 1
    public function fromText(string $text): void
86
    {
87 1
        $rdata = explode(' ', $text);
88 1
        $this->setPreference((int) $rdata[0]);
89 1
        $this->setExchanger($rdata[1]);
90 1
    }
91
92 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
93
    {
94 1
        if (false === $preference = unpack('n', $rdata, $offset)) {
95 1
            throw new DecodeException(static::TYPE, $rdata);
96 1
        }
97 1
        $this->setPreference($preference[1]);
98
        $offset += 2;
99
        $this->setExchanger(Message::decodeName($rdata, $offset));
100
    }
101
}
102