Completed
Push — master ( 548e97...364c70 )
by Sam
02:10
created

ResourceRecord::setTtl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of Badcow DNS Library.
5
 *
6
 * (c) Samuel Williams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Badcow\DNS;
13
14
use Badcow\DNS\Rdata\RdataInterface;
15
16
class ResourceRecord implements ResourceRecordInterface
17
{
18
    const COMMENT_DELIMINATOR = '; ';
19
20
    const MULTILINE_BEGIN = '(';
21
22
    const MULTILINE_END = ')';
23
24
    /**
25
     * @var string
26
     */
27
    private $class;
28
29
    /**
30
     * @var RdataInterface
31
     */
32
    private $rdata;
33
34
    /**
35
     * @var int
36
     */
37
    private $ttl;
38
39
    /**
40
     * @var string
41
     */
42
    private $name;
43
44
    /**
45
     * @var string
46
     */
47
    private $comment;
48
49
    /**
50
     * @param string         $name
51
     * @param RdataInterface $rdata
52
     * @param string         $ttl
53
     * @param string         $class
54
     * @param string         $comment
55
     */
56 30
    public function __construct($name = null, RdataInterface $rdata = null, $ttl = null, $class = null, $comment = null)
57
    {
58 30
        if (null !== $name) {
59 18
            $this->setName($name);
60 18
        }
61
62 30
        if (null !== $class) {
63 15
            $this->setClass($class);
64 15
        }
65
66 30
        if (null !== $ttl) {
67 18
            $this->setTtl($ttl);
68 18
        }
69
70 30
        $this->rdata = $rdata;
71 30
        $this->comment = $comment;
72 30
    }
73
74
    /**
75
     * @param string $class
76
     *
77
     * @throws ResourceRecordException
78
     */
79 27
    public function setClass($class)
80
    {
81 27
        if (!Classes::isValid($class)) {
82 3
            throw new ResourceRecordException(sprintf('No such class as "%s"', $class));
83
        }
84
85 27
        $this->class = $class;
86 27
    }
87
88
    /**
89
     * @param string $name
90
     *
91
     * @throws DNSException
92
     */
93 27
    public function setName($name)
94
    {
95 27
        if (!Validator::rrName($name)) {
96 3
            throw new DNSException(sprintf('"%s" is not a valid resource record name.', $name));
97
        }
98
99 24
        $this->name = (string) $name;
100 24
    }
101
102
    /**
103
     * @param RdataInterface $rdata
104
     */
105 24
    public function setRdata(RdataInterface $rdata)
106
    {
107 24
        $this->rdata = $rdata;
108 24
    }
109
110
    /**
111
     * @return string
112
     */
113 21
    public function getClass()
114
    {
115 21
        return $this->class;
116
    }
117
118
    /**
119
     * @param int $ttl
120
     */
121 21
    public function setTtl($ttl)
122
    {
123 21
        $this->ttl = (int) $ttl;
124 21
    }
125
126
    /**
127
     * @return string
128
     */
129 12
    public function getName()
130
    {
131 12
        return $this->name;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 12
    public function getType()
138
    {
139 12
        if (null === $this->rdata) {
140 3
            return;
141
        }
142
143 12
        return $this->rdata->getType();
144
    }
145
146
    /**
147
     * @return RdataInterface
148
     */
149 24
    public function getRdata()
150
    {
151 24
        return $this->rdata;
152
    }
153
154
    /**
155
     * @return int
156
     */
157 9
    public function getTtl()
158
    {
159 9
        return $this->ttl;
160
    }
161
162
    /**
163
     * Set a comment for the record.
164
     *
165
     * @param string $comment
166
     */
167 21
    public function setComment($comment)
168
    {
169 21
        $comment = preg_replace('/(?:\n|\r)/', '', $comment);
170 21
        $this->comment = $comment;
171 21
    }
172
173
    /**
174
     * Get the record's comment.
175
     *
176
     * @return string
177
     */
178 9
    public function getComment()
179
    {
180 9
        return $this->comment;
181
    }
182
}
183