Test Failed
Pull Request — master (#26)
by
unknown
07:47 queued 05:30
created

CaaRecord   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 8
dl 0
loc 167
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getFromQuotes() 0 4 1
A __toString() 0 4 1
A getFlags() 0 4 1
A setFlags() 0 4 1
A getTag() 0 4 1
A setTag() 0 4 1
A getValue() 0 4 1
A setValue() 0 4 1
B validate() 0 37 11
A recordDataToArray() 0 8 1
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 */
26
    const PATTERN_FQDN = '/^([a-zа-яё0-9\_]([a-zа-яё0-9\-]{0,61}[a-zа-яё0-9])?\.)*([a-zа-яё0-9]([a-zа-яё0-9\-]{0,61}[a-zа-яё0-9])?\.)+[a-zа-яё0-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
        \Yii::error($this->getTag(), 'check');
139
140
        if (!in_array($this->tag, [self::TAG_ISSUE, self::TAG_ISSUEWILD, self::TAG_IODEF])) {
141
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_CAA_TAG(), 'tag'));
142
        }
143
144
        if ($this->flags < 0 || $this->flags > 128) {
145
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_CAA_FLAG(), 'flag'));
0 ignored issues
show
Bug introduced by
The method WRONG_CAA_FLAG() does not exist on LTDBeget\dns\enums\eErrorCode. Did you maybe mean WRONG_CAA_FLAGS()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
146
        }
147
148
        if (!Int16Validator::validate($this->getFlags())) {
149
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_INT16(), 'flags'));
150
        }
151
152
        if (in_array($this->getTag(), [self::TAG_ISSUEWILD, self::TAG_ISSUE]) &&
153
            $this->getValue() !== ';'
154
        ) {
155
            $parts = explode(";", $this->getValue());
156
157
            if (!preg_match(self::PATTERN_FQDN, $parts[0])) {
158
                $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_CAA_VALUE(), 'value'));
159
            }
160
        }
161
162
        if ($this->getTag() == self::TAG_IODEF) {
163
            if (!filter_var($this->getValue(), FILTER_VALIDATE_EMAIL) && !filter_var($this->getValue(), FILTER_VALIDATE_URL)) {
164
                $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::WRONG_CAA_VALUE(), 'value'));
165
            }
166
        }
167
168
        /** @noinspection PhpInternalEntityUsedInspection */
169
        return parent::validate();
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    protected function recordDataToArray(): array
176
    {
177
        return [
178
            'FLAGS' => $this->getFlags(),
179
            'TAG'   => $this->getTag(),
180
            'VALUE' => $this->getValue()
181
        ];
182
    }
183
}