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

KX   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 26
c 3
b 0
f 0
dl 0
loc 87
ccs 30
cts 32
cp 0.9375
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getExchanger() 0 3 1
A toText() 0 10 3
A fromWire() 0 5 1
A setExchanger() 0 3 1
A setPreference() 0 3 1
A fromText() 0 5 1
A toWire() 0 10 3
A getPreference() 0 3 1
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