CSYNC::toWire()   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
18
/**
19
 * {@link https://tools.ietf.org/html/rfc7477}.
20
 */
21
class CSYNC implements RdataInterface
22
{
23 1
    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 6
    public function addType(string $type): void
44
    {
45 6
        $this->types[] = $type;
46 6
    }
47
48
    /**
49
     * Clears the types from the RDATA.
50
     */
51 1
    public function clearTypes(): void
52
    {
53 1
        $this->types = [];
54 1
    }
55
56 2
    public function getTypes(): array
57
    {
58 2
        return $this->types;
59
    }
60
61 1
    public function getSoaSerial(): int
62
    {
63 1
        return $this->soaSerial;
64
    }
65
66 5
    public function setSoaSerial(int $soaSerial): void
67
    {
68 5
        $this->soaSerial = $soaSerial;
69 5
    }
70
71 1
    public function getFlags(): int
72
    {
73 1
        return $this->flags;
74
    }
75
76 5
    public function setFlags(int $flags): void
77
    {
78 5
        $this->flags = $flags;
79 5
    }
80
81 1
    public function toText(): string
82
    {
83 1
        return sprintf('%d %d %s', $this->soaSerial, $this->flags, implode(Tokens::SPACE, $this->types));
84
    }
85
86
    /**
87
     * @throws UnsupportedTypeException
88
     */
89 1
    public function toWire(): string
90
    {
91 1
        return pack('Nn', $this->soaSerial, $this->flags).NSEC::renderBitmap($this->types);
92
    }
93
94 1
    public function fromText(string $text): void
95
    {
96 1
        $rdata = explode(Tokens::SPACE, $text);
97 1
        $this->setSoaSerial((int) array_shift($rdata));
98 1
        $this->setFlags((int) array_shift($rdata));
99 1
        array_map([$this, 'addType'], $rdata);
100 1
    }
101
102
    /**
103
     * @throws UnsupportedTypeException|DecodeException
104
     */
105 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
106
    {
107 1
        if (false === $integers = unpack('Nserial/nflags', $rdata, $offset)) {
108 1
            throw new DecodeException(static::TYPE, $rdata);
109 1
        }
110
        $offset += 6;
111 1
        $types = NSEC::parseBitmap($rdata, $offset);
112 1
113 1
        $this->setSoaSerial((int) $integers['serial']);
114 1
        $this->setFlags((int) $integers['flags']);
115
        array_map([$this, 'addType'], $types);
116
    }
117
}
118