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