Completed
Branch scrutinizer-changes (af95a9)
by Sam
05:03
created

ResourceRecord::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 5
dl 0
loc 10
ccs 8
cts 8
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;
15
16
use Badcow\DNS\Rdata\RdataInterface;
17
18
class ResourceRecord
19
{
20
    /**
21
     * @var string|null
22
     */
23
    private $class = Classes::INTERNET;
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 \UnexpectedValueException
73
     */
74 32
    public function setClass(?string $class): void
75
    {
76 32
        if (null !== $class && !Classes::isValid($class)) {
77 1
            throw new \UnexpectedValueException(sprintf('No such class as "%s"', $class));
78
        }
79
80 32
        $this->class = $class;
81 32
    }
82
83
    /**
84
     * Set the name for the resource record.
85
     * Eg. "subdomain.example.com.".
86
     *
87
     * @param string $name
88
     */
89 32
    public function setName(string $name): void
90
    {
91 32
        $this->name = $name;
92 32
    }
93
94
    /**
95
     * @param RdataInterface $rdata
96
     */
97 30
    public function setRdata(?RdataInterface $rdata): void
98
    {
99 30
        $this->rdata = $rdata;
100 30
    }
101
102
    /**
103
     * @return string
104
     */
105 27
    public function getClass(): ?string
106
    {
107 27
        return $this->class;
108
    }
109
110
    /**
111
     * @return int|null
112
     */
113
    public function getClassId(): ?int
114
    {
115
        if (!isset($this->class)) {
116
            return null;
117
        }
118
119
        return Classes::getClassId($this->class);
0 ignored issues
show
Bug introduced by
It seems like $this->class can also be of type null; however, parameter $className of Badcow\DNS\Classes::getClassId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
        return Classes::getClassId(/** @scrutinizer ignore-type */ $this->class);
Loading history...
120
    }
121
122
    /**
123
     * Set the time to live.
124
     *
125
     * @param int $ttl
126
     */
127 33
    public function setTtl(?int $ttl): void
128
    {
129 33
        $this->ttl = $ttl;
130 33
    }
131
132
    /**
133
     * @return string
134
     */
135 26
    public function getName(): ?string
136
    {
137 26
        return $this->name;
138
    }
139
140
    /**
141
     * @return string
142
     */
143 23
    public function getType(): ?string
144
    {
145 23
        if (null === $this->rdata) {
146 2
            return null;
147
        }
148
149 22
        return $this->rdata->getType();
150
    }
151
152
    /**
153
     * @return RdataInterface
154
     */
155 21
    public function getRdata(): ?RdataInterface
156
    {
157 21
        return $this->rdata;
158
    }
159
160
    /**
161
     * @return int
162
     */
163 23
    public function getTtl(): ?int
164
    {
165 23
        return $this->ttl;
166
    }
167
168
    /**
169
     * Set a comment for the record.
170
     *
171
     * @param string $comment
172
     */
173 33
    public function setComment(?string $comment): void
174
    {
175 33
        $this->comment = $comment;
176 33
    }
177
178
    /**
179
     * Get the record's comment.
180
     *
181
     * @return string
182
     */
183 10
    public function getComment(): ?string
184
    {
185 10
        return $this->comment;
186
    }
187
}
188