Completed
Branch Message (24050e)
by Sam
03:08
created

SRV   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 46
dl 0
loc 176
ccs 51
cts 51
cp 1
rs 10
c 2
b 1
f 0
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A toWire() 0 3 1
A fromText() 0 10 1
A getWeight() 0 3 1
A setPort() 0 7 3
A setWeight() 0 7 3
A setPriority() 0 7 3
A getPort() 0 3 1
A getPriority() 0 3 1
A setTarget() 0 3 1
A toText() 0 7 1
A fromWire() 0 11 1
A getTarget() 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\Parser\Tokens;
17
18
/**
19
 * Class SrvRdata.
20
 *
21
 * SRV is defined in RFC 2782
22
 *
23
 * @see https://tools.ietf.org/html/rfc2782
24
 *
25
 * @author Samuel Williams <[email protected]>
26
 */
27
class SRV implements RdataInterface
28
{
29
    use RdataTrait;
30
31
    const TYPE = 'SRV';
32
    const TYPE_CODE = 33;
33
    const HIGHEST_PORT = 65535;
34
    const MAX_PRIORITY = 65535;
35
    const MAX_WEIGHT = 65535;
36
37
    /**
38
     * The priority of this target host. A client MUST attempt to
39
     * contact the target host with the lowest-numbered priority it can
40
     * reach; target hosts with the same priority SHOULD be tried in an
41
     * order defined by the weight field. The range is 0-65535. This
42
     * is a 16 bit unsigned integer.
43
     *
44
     * @var int|null
45
     */
46
    private $priority;
47
48
    /**
49
     * A server selection mechanism.  The weight field specifies a
50
     * relative weight for entries with the same priority. The range
51
     * is 0-65535. This is a 16 bit unsigned integer.
52
     *
53
     * @var int|null
54
     */
55
    private $weight;
56
57
    /**
58
     * The port on this target host of this service. The range is
59
     * 0-65535. This is a 16 bit unsigned integer.
60
     *
61
     * @var int|null
62
     */
63
    private $port;
64
65
    /**
66
     * The domain name of the target host.  There MUST be one or more
67
     * address records for this name, the name MUST NOT be an alias (in
68
     * the sense of RFC 1034 or RFC 2181).  Implementors are urged, but
69
     * not required, to return the address record(s) in the Additional
70
     * Data section.  Unless and until permitted by future standards
71
     * action, name compression is not to be used for this field.
72
     *
73
     * A Target of "." means that the service is decidedly not
74
     * available at this domain.
75
     *
76
     * @var string
77
     */
78
    private $target;
79
80
    /**
81
     * @return int
82
     */
83 1
    public function getPriority(): ?int
84
    {
85 1
        return $this->priority;
86
    }
87
88
    /**
89
     * @param int $priority
90
     *
91
     * @throws \InvalidArgumentException
92
     */
93 15
    public function setPriority(int $priority): void
94
    {
95 15
        if ($priority < 0 || $priority > static::MAX_PRIORITY) {
96 1
            throw new \InvalidArgumentException('Priority must be an unsigned integer on the range [0-65535]');
97
        }
98
99 14
        $this->priority = $priority;
100 14
    }
101
102
    /**
103
     * @return int
104
     */
105 1
    public function getWeight(): ?int
106
    {
107 1
        return $this->weight;
108
    }
109
110
    /**
111
     * @param int $weight
112
     *
113
     * @throws \InvalidArgumentException
114
     */
115 15
    public function setWeight(int $weight): void
116
    {
117 15
        if ($weight < 0 || $weight > static::MAX_WEIGHT) {
118 1
            throw new \InvalidArgumentException('Weight must be an unsigned integer on the range [0-65535]');
119
        }
120
121 14
        $this->weight = $weight;
122 14
    }
123
124
    /**
125
     * @return int
126
     */
127 1
    public function getPort(): ?int
128
    {
129 1
        return $this->port;
130
    }
131
132
    /**
133
     * @param int $port
134
     *
135
     * @throws \InvalidArgumentException
136
     */
137 15
    public function setPort(int $port): void
138
    {
139 15
        if ($port < 0 || $port > static::HIGHEST_PORT) {
140 1
            throw new \InvalidArgumentException('Port must be an unsigned integer on the range [0-65535]');
141
        }
142
143 14
        $this->port = $port;
144 14
    }
145
146
    /**
147
     * @return string
148
     */
149 1
    public function getTarget(): string
150
    {
151 1
        return $this->target;
152
    }
153
154
    /**
155
     * @param string $target
156
     */
157 14
    public function setTarget(string $target): void
158
    {
159 14
        $this->target = $target;
160 14
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 4
    public function toText(): string
166
    {
167 4
        return sprintf('%s %s %s %s',
168 4
            $this->priority,
169 4
            $this->weight,
170 4
            $this->port,
171 4
            $this->target
172
        );
173
    }
174
175 1
    public function toWire(): string
176
    {
177 1
        return pack('nnn', $this->priority, $this->weight, $this->port).self::encodeName($this->target);
178
    }
179
180 2
    public static function fromText(string $text): RdataInterface
181
    {
182 2
        $rdata = explode(Tokens::SPACE, $text);
183 2
        $srv = new SRV();
184 2
        $srv->setPriority((int) $rdata[0]);
185 2
        $srv->setWeight((int) $rdata[1]);
186 2
        $srv->setPort((int) $rdata[2]);
187 2
        $srv->setTarget($rdata[3]);
188
189 2
        return $srv;
190
    }
191
192 1
    public static function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): RdataInterface
193
    {
194 1
        $integers = unpack('npriority/nweight/nport', $rdata, $offset);
195 1
        $offset += 6;
196 1
        $srv = new self();
197 1
        $srv->setTarget(self::decodeName($rdata, $offset));
198 1
        $srv->setPriority($integers['priority']);
199 1
        $srv->setWeight($integers['weight']);
200 1
        $srv->setPort($integers['port']);
201
202 1
        return $srv;
203
    }
204
}
205