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

ResourceRecord::setClassId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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;
15
16
use Badcow\DNS\Rdata\RdataInterface;
17
18
class ResourceRecord
19
{
20
    /**
21
     * @var int|null
22
     */
23
    private $classId = 1;
24
25
    /**
26
     * @var RdataInterface|null
27
     */
28
    private $rdata;
29
30
    /**
31
     * @var int|null
32
     */
33
    private $ttl;
34
35
    /**
36
     * @var string
37
     */
38
    private $name;
39
40
    /**
41
     * @var string|null
42
     */
43
    private $comment;
44
45
    /**
46
     * @param string         $name
47
     * @param RdataInterface $rdata
48
     * @param int            $ttl
49
     * @param string         $class
50
     * @param string         $comment
51
     *
52
     * @return ResourceRecord
53
     */
54 15
    public static function create(string $name, RdataInterface $rdata, int $ttl = null, string $class = Classes::INTERNET, string $comment = null): ResourceRecord
55
    {
56 15
        $rr = new self();
57 15
        $rr->setName($name);
58 15
        $rr->setRdata($rdata);
59 15
        $rr->setTtl($ttl);
60 15
        $rr->setClass($class);
61 15
        $rr->setComment($comment);
62
63 15
        return $rr;
64
    }
65
66
    /**
67
     * Set the class for the resource record
68
     * Usually one of IN, HS, or CH.
69
     *
70
     * @param string $class
71
     *
72
     * @throws \InvalidArgumentException
73
     */
74 33
    public function setClass(?string $class): void
75
    {
76 33
        if (null !== $class && !Classes::isValid($class)) {
77 1
            throw new \InvalidArgumentException(sprintf('No such class as "%s"', $class));
78
        }
79
80 33
        if (null === $class) {
81 1
            $this->classId = null;
82
83 1
            return;
84
        }
85
86 33
        $this->classId = Classes::getClassId($class);
87 33
    }
88
89
    /**
90
     * Set the name for the resource record.
91
     * Eg. "subdomain.example.com.".
92
     *
93
     * @param string $name
94
     */
95 34
    public function setName(string $name): void
96
    {
97 34
        $this->name = $name;
98 34
    }
99
100
    /**
101
     * @param RdataInterface $rdata
102
     */
103 31
    public function setRdata(?RdataInterface $rdata): void
104
    {
105 31
        $this->rdata = $rdata;
106 31
    }
107
108
    /**
109
     * @return string
110
     */
111 28
    public function getClass(): ?string
112
    {
113 28
        if (null === $this->classId) {
114 1
            return null;
115
        }
116
117 27
        return Classes::getClassName($this->classId);
118
    }
119
120
    public function setClassId(int $classId): void
121
    {
122
        $this->classId = $classId;
123
    }
124
125
    /**
126
     * @return int|null
127
     */
128
    public function getClassId(): ?int
129
    {
130
        return $this->classId;
131
    }
132
133
    /**
134
     * Set the time to live.
135
     *
136
     * @param int $ttl
137
     */
138 34
    public function setTtl(?int $ttl): void
139
    {
140 34
        $this->ttl = $ttl;
141 34
    }
142
143
    /**
144
     * @return string
145
     */
146 27
    public function getName(): ?string
147
    {
148 27
        return $this->name;
149
    }
150
151
    /**
152
     * @return string
153
     */
154 23
    public function getType(): ?string
155
    {
156 23
        if (null === $this->rdata) {
157 2
            return null;
158
        }
159
160 22
        return $this->rdata->getType();
161
    }
162
163
    /**
164
     * @return RdataInterface
165
     */
166 22
    public function getRdata(): ?RdataInterface
167
    {
168 22
        return $this->rdata;
169
    }
170
171
    /**
172
     * @return int
173
     */
174 24
    public function getTtl(): ?int
175
    {
176 24
        return $this->ttl;
177
    }
178
179
    /**
180
     * Set a comment for the record.
181
     *
182
     * @param string $comment
183
     */
184 34
    public function setComment(?string $comment): void
185
    {
186 34
        $this->comment = $comment;
187 34
    }
188
189
    /**
190
     * Get the record's comment.
191
     *
192
     * @return string
193
     */
194 10
    public function getComment(): ?string
195
    {
196 10
        return $this->comment;
197
    }
198
}
199