UnknownType   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 1
dl 0
loc 83
ccs 28
cts 31
cp 0.9032
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setTypeCode() 0 4 1
A getData() 0 4 1
A setData() 0 4 1
A getType() 0 4 1
A getTypeCode() 0 4 1
A toText() 0 8 2
A toWire() 0 4 1
A fromText() 0 17 4
A fromWire() 0 4 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\ParseException;
17
use Badcow\DNS\Parser\Tokens;
18
19
/**
20
 * {@link https://tools.ietf.org/html/rfc3597}.
21
 */
22
class UnknownType implements RdataInterface
23
{
24
    /**
25
     * @var int
26
     */
27
    private $typeCode;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $data;
33
34 3
    public function setTypeCode(int $typeCode): void
35
    {
36 3
        $this->typeCode = $typeCode;
37 3
    }
38
39
    /**
40
     * @return string
41
     */
42 3
    public function getData(): ?string
43
    {
44 3
        return $this->data;
45
    }
46
47
    /**
48
     * @param string $data
49
     */
50 5
    public function setData(?string $data): void
51
    {
52 5
        $this->data = $data;
53 5
    }
54
55 1
    public function getType(): string
56
    {
57 1
        return 'TYPE'.$this->typeCode;
58
    }
59
60 2
    public function getTypeCode(): int
61
    {
62 2
        return $this->typeCode;
63
    }
64
65 2
    public function toText(): string
66
    {
67 2
        if (null === $this->data) {
68
            return '\# 0';
69
        }
70
71 2
        return sprintf('\# %d %s', strlen($this->data), bin2hex($this->data));
72
    }
73
74 1
    public function toWire(): string
75
    {
76 1
        return $this->data ?? '';
77
    }
78
79
    /**
80
     * @throws ParseException
81
     */
82 3
    public function fromText(string $text): void
83
    {
84 3
        if (1 !== preg_match('/^\\\#\s+(\d+)(\s[a-f0-9\s]+)?$/i', $text, $matches)) {
85
            throw new ParseException('Could not parse rdata of unknown type. Malformed string.');
86
        }
87
88 3
        if ('0' === $matches[1]) {
89 1
            return;
90
        }
91
92 3
        $hexVal = str_replace(Tokens::SPACE, '', $matches[2]);
93
94 3
        if (false === $data = hex2bin($hexVal)) {
95
            throw new ParseException(sprintf('Could not parse hexadecimal data "%s".', $hexVal));
96
        }
97 3
        $this->setData($data);
98 3
    }
99
100 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
101
    {
102 1
        $this->setData(substr($rdata, $offset, $rdLength ?? strlen($rdata)));
103 1
    }
104
}
105