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

MX   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

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

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