CAA::getFlag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
use Badcow\DNS\Validator;
18
19
/**
20
 * Class CaaRdata.
21
 *
22
 * CAA is defined in RFC 6844
23
 *
24
 * @see https://tools.ietf.org/html/rfc6844
25
 *
26
 * @author Samuel Williams <[email protected]>
27
 */
28
class CAA implements RdataInterface
29
{
30 1
    use RdataTrait;
31
32
    const TYPE = 'CAA';
33
    const TYPE_CODE = 257;
34
35
    const TAG_ISSUE = 'issue';
36
    const TAG_ISSUEWILD = 'issuewild';
37
    const TAG_IODEF = 'iodef';
38
    const ALLOWED_TAGS = [self::TAG_ISSUE, self::TAG_ISSUEWILD, self::TAG_IODEF];
39
40
    /**
41
     * It is currently used to represent the critical flag.
42
     *
43
     * @var int
44
     */
45
    private $flag;
46
47
    /**
48
     * An ASCII string that represents the identifier of the property represented by the record.
49
     * The RFC currently defines 3 available tags:
50
     *  - issue: explicitly authorizes a single certificate authority to issue a certificate (any type) for the hostname.
51
     *  - issuewild: explicitly authorizes a single certificate authority to issue a wildcard certificate (and only wildcard) for the hostname.
52
     *  - iodef: specifies a URL to which a certificate authority may report policy violations.
53
     *
54
     * @var string
55
     */
56
    private $tag;
57
58
    /**
59
     * @var string
60
     */
61
    private $value;
62
63
    /**
64
     * @return int
65
     */
66 3
    public function getFlag(): ?int
67
    {
68 3
        return $this->flag;
69
    }
70
71
    /**
72
     * @throws \InvalidArgumentException
73
     */
74 6
    public function setFlag(int $flag): void
75
    {
76 6
        if (!Validator::isUnsignedInteger($flag, 8)) {
77 1
            throw new \InvalidArgumentException('Flag must be an unsigned 8-bit integer.');
78
        }
79
80 5
        $this->flag = $flag;
81 5
    }
82
83
    /**
84
     * @return string
85
     */
86 3
    public function getTag(): ?string
87
    {
88 3
        return $this->tag;
89
    }
90
91
    /**
92
     * @throws \InvalidArgumentException
93
     */
94 7
    public function setTag(string $tag): void
95
    {
96 7
        $tag = strtolower($tag);
97 7
        if (!in_array($tag, static::ALLOWED_TAGS)) {
98 1
            throw new \InvalidArgumentException('Tag can be one of this type "issue", "issuewild", or "iodef".');
99
        }
100
101 6
        $this->tag = $tag;
102 6
    }
103
104 2
    public function getValue(): ?string
105
    {
106 2
        return $this->value;
107
    }
108
109 5
    public function setValue(string $value): void
110
    {
111 5
        $this->value = $value;
112 5
    }
113
114 2
    public function toText(): string
115
    {
116 2
        if (!isset($this->tag) || !isset($this->flag) || !isset($this->value)) {
117 1
            throw new \InvalidArgumentException('All CAA parameters must be set.');
118
        }
119
120 1
        return sprintf('%d %s "%s"',
121 1
            $this->flag,
122 1
            $this->tag ?? '',
123 1
            $this->value ?? ''
124
        );
125
    }
126
127 2
    public function toWire(): string
128
    {
129 2
        if (!isset($this->tag) || !isset($this->flag) || !isset($this->value)) {
130 1
            throw new \InvalidArgumentException('All CAA parameters must be set.');
131
        }
132
133 1
        return chr($this->flag).
134 1
            chr(strlen($this->tag)).
135 1
            $this->tag.
136 1
            $this->value;
137
    }
138
139 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
140
    {
141 1
        $this->setFlag(ord($rdata[$offset]));
142 1
        ++$offset;
143
144 1
        $tagLen = ord($rdata[$offset]);
145 1
        ++$offset;
146
147 1
        $this->setTag(substr($rdata, $offset, $tagLen));
148 1
        $offset += $tagLen;
149
150 1
        $valueLen = ($rdLength ?? strlen($rdata)) - 2 - $tagLen;
151 1
        $this->setValue(substr($rdata, $offset, $valueLen));
152
153 1
        $offset = $offset += $valueLen;
154 1
    }
155
156 2
    public function fromText(string $text): void
157
    {
158 2
        $rdata = explode(Tokens::SPACE, $text);
159 2
        $this->setFlag((int) array_shift($rdata));
160 2
        $this->setTag((string) array_shift($rdata));
161 2
        $rdata = implode('', $rdata);
162 2
        $this->setValue(trim($rdata, '"'));
163 2
    }
164
}
165