Passed
Branch master (8940db)
by Sam
02:38
created

URI::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
 * {@link https://tools.ietf.org/html/rfc7553}.
20
 */
21
class URI implements RdataInterface
22
{
23
    use RdataTrait;
24
25
    const TYPE = 'URI';
26
    const TYPE_CODE = 256;
27
    const MAX_PRIORITY = 65535;
28
    const MAX_WEIGHT = 65535;
29
30
    /**
31
     * This field holds the priority of the target URI in this RR.  Its
32
     * range is 0-65535.  A client MUST attempt to contact the URI with the
33
     * lowest-numbered priority it can reach; URIs with the same priority
34
     * SHOULD be selected according to probabilities defined by the weight
35
     * field.
36
     *
37
     * @var int
38
     */
39
    private $priority;
40
41
    /**
42
     * This field holds the server selection mechanism.  The weight field
43
     * specifies a relative weight for entries with the same priority.
44
     * Larger weights SHOULD be given a proportionately higher probability
45
     * of being selected.  The range of this number is 0-65535.
46
     *
47
     * @var int
48
     */
49
    private $weight;
50
51
    /**
52
     * @var string
53
     */
54
    private $target;
55
56
    /**
57
     * @return int
58
     */
59 2
    public function getPriority(): ?int
60
    {
61 2
        return $this->priority;
62
    }
63
64
    /**
65
     * @param int $priority
66
     *
67
     * @throws \InvalidArgumentException
68
     */
69 9
    public function setPriority(int $priority): void
70
    {
71 9
        if ($priority < 0 || $priority > static::MAX_PRIORITY) {
72 2
            throw new \InvalidArgumentException('Priority must be an unsigned integer on the range [0-65535]');
73
        }
74
75 7
        $this->priority = $priority;
76 7
    }
77
78
    /**
79
     * @return int
80
     */
81 2
    public function getWeight(): ?int
82
    {
83 2
        return $this->weight;
84
    }
85
86
    /**
87
     * @param int $weight
88
     *
89
     * @throws \InvalidArgumentException
90
     */
91 7
    public function setWeight(int $weight): void
92
    {
93 7
        if ($weight < 0 || $weight > static::MAX_WEIGHT) {
94 2
            throw new \InvalidArgumentException('Weight must be an unsigned integer on the range [0-65535]');
95
        }
96
97 5
        $this->weight = $weight;
98 5
    }
99
100
    /**
101
     * @return string
102
     */
103 2
    public function getTarget(): ?string
104
    {
105 2
        return $this->target;
106
    }
107
108
    /**
109
     * @param string $target
110
     */
111 5
    public function setTarget(string $target): void
112
    {
113 5
        if (false === filter_var($target, FILTER_VALIDATE_URL)) {
114 1
            throw new \InvalidArgumentException(sprintf('The target "%s" is not a valid URI.', $target));
115
        }
116
117 4
        $this->target = $target;
118 4
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 1
    public function toText(): string
124
    {
125 1
        return sprintf('%d %d "%s"',
126 1
            $this->priority,
127 1
            $this->weight,
128 1
            $this->target
129
        );
130
    }
131
132 1
    public function toWire(): string
133
    {
134 1
        return pack('nn', $this->priority, $this->weight).$this->target;
135
    }
136
137 2
    public static function fromText(string $text): RdataInterface
138
    {
139 2
        $rdata = explode(Tokens::SPACE, $text);
140 2
        $uri = new self();
141 2
        $uri->setPriority((int) array_shift($rdata));
142 2
        $uri->setWeight((int) array_shift($rdata));
143 2
        $target = implode(' ', $rdata);
144 2
        $uri->setTarget(trim($target, '"'));
145
146 2
        return $uri;
147
    }
148
149 1
    public static function fromWire(string $rdata): RdataInterface
150
    {
151 1
        $integers = unpack('npriority/nweight', $rdata);
152
153 1
        $uri = new self();
154 1
        $uri->setTarget(substr($rdata, 4));
155 1
        $uri->setPriority($integers['priority']);
156 1
        $uri->setWeight($integers['weight']);
157
158 1
        return $uri;
159
    }
160
}
161