Passed
Branch master (8940db)
by Sam
02:38
created

CSYNC   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 124
ccs 36
cts 39
cp 0.9231
rs 10
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A toText() 0 3 1
A toWire() 0 3 1
A getTypes() 0 3 1
A addType() 0 3 1
A clearTypes() 0 3 1
A fromWire() 0 13 1
A getFlags() 0 3 1
A getSoaSerial() 0 3 1
A setFlags() 0 3 1
A fromText() 0 9 1
A setSoaSerial() 0 3 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
18
/**
19
 * {@link https://tools.ietf.org/html/rfc7477}.
20
 */
21
class CSYNC implements RdataInterface
22
{
23
    use RdataTrait;
24
25
    const TYPE = 'CSYNC';
26
    const TYPE_CODE = 62;
27
28
    /**
29
     * @var int
30
     */
31
    private $soaSerial;
32
33
    /**
34
     * @var int
35
     */
36
    private $flags;
37
38
    /**
39
     * @var array
40
     */
41
    private $types = [];
42
43
    /**
44
     * @param string $type
45
     */
46 5
    public function addType(string $type): void
47
    {
48 5
        $this->types[] = $type;
49 5
    }
50
51
    /**
52
     * Clears the types from the RDATA.
53
     */
54
    public function clearTypes(): void
55
    {
56
        $this->types = [];
57
    }
58
59
    /**
60
     * @return array
61
     */
62 1
    public function getTypes(): array
63
    {
64 1
        return $this->types;
65
    }
66
67
    /**
68
     * @return int
69
     */
70 1
    public function getSoaSerial(): int
71
    {
72 1
        return $this->soaSerial;
73
    }
74
75
    /**
76
     * @param int $soaSerial
77
     */
78 5
    public function setSoaSerial(int $soaSerial): void
79
    {
80 5
        $this->soaSerial = $soaSerial;
81 5
    }
82
83
    /**
84
     * @return int
85
     */
86 1
    public function getFlags(): int
87
    {
88 1
        return $this->flags;
89
    }
90
91
    /**
92
     * @param int $flags
93
     */
94 5
    public function setFlags(int $flags): void
95
    {
96 5
        $this->flags = $flags;
97 5
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 1
    public function toText(): string
103
    {
104 1
        return sprintf('%d %d %s', $this->soaSerial, $this->flags, implode(Tokens::SPACE, $this->types));
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 1
    public function toWire(): string
111
    {
112 1
        return pack('Nn', $this->soaSerial, $this->flags).NSEC::renderBitmap($this->types);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public static function fromText(string $text): RdataInterface
119
    {
120 1
        $rdata = explode(Tokens::SPACE, $text);
121 1
        $csync = new static();
122 1
        $csync->setSoaSerial((int) array_shift($rdata));
123 1
        $csync->setFlags((int) array_shift($rdata));
124 1
        array_map([$csync, 'addType'], $rdata);
125
126 1
        return $csync;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1
    public static function fromWire(string $rdata): RdataInterface
133
    {
134 1
        $offset = 0;
135 1
        $integers = unpack('Nserial/nflags', $rdata, $offset);
136 1
        $offset += 6;
137 1
        $types = NSEC::parseBitmap($rdata, $offset);
138
139 1
        $csync = new static();
140 1
        $csync->setSoaSerial((int) $integers['serial']);
141 1
        $csync->setFlags((int) $integers['flags']);
142 1
        array_map([$csync, 'addType'], $types);
143
144 1
        return $csync;
145
    }
146
}
147