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

MX   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 101
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getExchange() 0 3 1
A getPreference() 0 3 1
A fromText() 0 5 1
A setExchange() 0 3 1
A toWire() 0 11 3
A setPreference() 0 3 1
A fromWire() 0 5 1
A toText() 0 11 3
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
 * @see https://tools.ietf.org/html/rfc1035#section-3.3.9
20
 */
21
class MX implements RdataInterface
22
{
23
    use RdataTrait;
24
25
    const TYPE = 'MX';
26
    const TYPE_CODE = 15;
27
28
    /**
29
     * @var int|null
30
     */
31
    private $preference;
32
33
    /**
34
     * @var string|null
35
     */
36
    private $exchange;
37
38
    /**
39
     * @param string $exchange
40
     */
41 26
    public function setExchange(string $exchange): void
42
    {
43 26
        $this->exchange = $exchange;
44 26
    }
45
46
    /**
47
     * @return string|null
48
     */
49 4
    public function getExchange(): ?string
50
    {
51 4
        return $this->exchange;
52
    }
53
54
    /**
55
     * @param int $preference
56
     */
57 26
    public function setPreference(int $preference): void
58
    {
59 26
        $this->preference = $preference;
60 26
    }
61
62
    /**
63
     * @return int|null
64
     */
65 3
    public function getPreference(): ?int
66
    {
67 3
        return $this->preference;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @throws \InvalidArgumentException throws exception if preference or exchange have not been set
74
     */
75 8
    public function toText(): string
76
    {
77 8
        if (null === $this->preference) {
78 1
            throw new \InvalidArgumentException('No preference has been set on MX object.');
79
        }
80
81 7
        if (null === $this->exchange) {
82 1
            throw new \InvalidArgumentException('No exchange has been set on MX object.');
83
        }
84
85 6
        return $this->preference.' '.$this->exchange;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function toWire(): string
92
    {
93 2
        if (null === $this->preference) {
94
            throw new \InvalidArgumentException('No preference has been set on MX object.');
95
        }
96
97 2
        if (null === $this->exchange) {
98
            throw new \InvalidArgumentException('No exchange has been set on MX object.');
99
        }
100
101 2
        return pack('n', $this->preference).Message::encodeName($this->exchange);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 8
    public function fromText(string $text): void
108
    {
109 8
        $rdata = explode(' ', $text);
110 8
        $this->setPreference((int) $rdata[0]);
111 8
        $this->setExchange($rdata[1]);
112 8
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 4
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
118
    {
119 4
        $this->setPreference(unpack('n', $rdata, $offset)[1]);
120 4
        $offset += 2;
121 4
        $this->setExchange(Message::decodeName($rdata, $offset));
122 4
    }
123
}
124