Test Setup Failed
Pull Request — master (#31)
by
unknown
06:45
created

CaaRecord::getFlags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LTDBeget\dns\configurator\zoneEntities\record;
4
5
use LTDBeget\dns\configurator\errors\ValidationError;
6
use LTDBeget\dns\configurator\validators\Int16Validator;
7
use LTDBeget\dns\configurator\zoneEntities\Node;
8
use LTDBeget\dns\configurator\zoneEntities\record\base\Record;
9
use LTDBeget\dns\enums\eErrorCode;
10
use LTDBeget\dns\enums\eRecordType;
11
12
/**
13
 * Class CaaRecord
14
 *
15
 * @package LTDBeget\dns\configurator\zoneEntities\record
16
 */
17
class CaaRecord extends Record
18
{
19
    /** @var string explicity authorizes a single certificate authority to issue a certificate (any type) for the hostname */
20
    const TAG_ISSUE = 'issue';
21
    /** @var string explicity authorizes a single certificate authority to issue a wildcard certificate (and only wildcard) for the hostname */
22
    const TAG_ISSUEWILD = 'issuewild';
23
    /** @var string specifies a URL to which a certificate authority may report policy violations */
24
    const TAG_IODEF = 'iodef';
25
    /** @var string  проверка по FQDN  rfc6844 5.2 */
26
    const PATTERN_FQDN = '/^([a-z0-9\-]([a-z0-9\-]{0,61}[a-z0-9])?\.)*([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?\.)+[a-z0-9-]{2,30}$/i';
27
28
    /**
29
     * @var Int
30
     */
31
    protected $flags;
32
    /**
33
     * @var String
34
     */
35
    protected $tag;
36
    /**
37
     * @var String
38
     */
39
    protected $value;
40
41
    /**
42
     * CaaRecord constructor.
43
     *
44
     * @param Node   $node
45
     * @param        $ttl
46
     * @param int    $flags
47
     * @param string $tag
48
     * @param string $value
49
     */
50
    public function __construct(Node $node, $ttl, int $flags, string $tag, string $value)
51
    {
52
        $this->flags = $flags;
53
        $this->tag   = $tag;
54
        $this->value = $this->getFromQuotes($value);
55
        parent::__construct($node, eRecordType::CAA(), $ttl);
56
    }
57
58
    /**
59
     * @param $value
60
     *
61
     * @return string
62
     */
63
    private function getFromQuotes(string $value)
64
    {
65
        return str_replace(["\n", '"'], "", $value);
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function __toString(): string
72
    {
73
        return $this->getMainRecordPart() . " {$this->getFlags()} {$this->getTag()} \"{$this->getValue()}\"";
74
    }
75
76
    /**
77
     * @return Int
78
     */
79
    public function getFlags(): Int
80
    {
81
        return $this->flags;
82
    }
83
84
    /**
85
     * @param Int $flags
86
     *
87
     * @return CaaRecord
88
     */
89
    public function setFlags(Int $flags): CaaRecord
90
    {
91
        return $this->setAttribute('flags', $flags);
92
    }
93
94
    /**
95
     * @return String
96
     */
97
    public function getTag(): String
98
    {
99
        return $this->tag;
100
    }
101
102
    /**
103
     * @param String $tag
104
     *
105
     * @return CaaRecord
106
     */
107
    public function setTag(String $tag): CaaRecord
108
    {
109
        return $this->setAttribute('tag', $tag);
110
    }
111
112
    /**
113
     * @return String
114
     */
115
    public function getValue(): String
116
    {
117
        return $this->value;
118
    }
119
120
    /**
121
     * @param String $value
122
     *
123
     * @return CaaRecord
124
     */
125
    public function setValue(String $value): CaaRecord
126
    {
127
        return $this->setAttribute('value', $value);
128
    }
129
130
    /**
131
     * @internal
132
     * @return bool
133
     */
134
    public function validate(): bool
135
    {
136
        $errorStorage = $this->getNode()->getZone()->getErrorsStore();
137
138
        if (!in_array($this->tag, [self::TAG_ISSUE, self::TAG_ISSUEWILD, self::TAG_IODEF])) {
139
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_CAA_TAG(), 'tag'));
140
        }
141
142
        if ($this->flags < 0 || $this->flags > 255) {
143
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_CAA_FLAGS(), 'flag'));
144
        }
145
146
        if (!Int16Validator::validate($this->getFlags())) {
147
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_INT16(), 'flags'));
148
        }
149
150
        if (in_array($this->getTag(), [self::TAG_ISSUEWILD, self::TAG_ISSUE]) &&
151
            $this->getValue() !== ';'
152
        ) {
153
            $parts = explode(";", $this->getValue());
154
155
            if (!preg_match(self::PATTERN_FQDN, $parts[0])) {
156
                $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_CAA_VALUE(), 'value'));
157
            }
158
        }
159
160
        if ($this->getTag() == self::TAG_IODEF) {
161
            if (!filter_var($this->getValue(), FILTER_VALIDATE_EMAIL) && !filter_var($this->getValue(), FILTER_VALIDATE_URL)) {
162
                $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_CAA_VALUE(), 'value'));
163
            }
164
        }
165
166
        /** @noinspection PhpInternalEntityUsedInspection */
167
        return parent::validate();
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    protected function recordDataToArray(): array
174
    {
175
        return [
176
            'FLAGS' => $this->getFlags(),
177
            'TAG'   => $this->getTag(),
178
            'VALUE' => $this->getValue()
179
        ];
180
    }
181
}
182