Completed
Branch Message (d15af3)
by Sam
05:12
created

HINFO   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 91.18%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 116
ccs 31
cts 34
cp 0.9118
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCpu() 0 3 1
A setOs() 0 3 1
A getOs() 0 3 1
A setCpu() 0 3 1
A toText() 0 3 1
A toWire() 0 3 1
A fromText() 0 8 1
A fromWire() 0 5 1
A extractText() 0 19 5
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\StringIterator;
17
use Badcow\DNS\Parser\Tokens;
18
19
/**
20
 * @see https://tools.ietf.org/html/rfc1035#section-3.3.2
21
 */
22
class HINFO implements RdataInterface
23
{
24
    use RdataTrait;
25
26
    const TYPE = 'HINFO';
27
    const TYPE_CODE = 13;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $cpu;
33
34
    /**
35
     * @var string|null
36
     */
37
    private $os;
38
39
    /**
40
     * @param string $cpu
41
     */
42 15
    public function setCpu(string $cpu): void
43
    {
44 15
        $this->cpu = $cpu;
45 15
    }
46
47
    /**
48
     * @return string
49
     */
50 4
    public function getCpu(): ?string
51
    {
52 4
        return $this->cpu;
53
    }
54
55
    /**
56
     * @param string $os
57
     */
58 15
    public function setOs(string $os): void
59
    {
60 15
        $this->os = $os;
61 15
    }
62
63
    /**
64
     * @return string
65
     */
66 4
    public function getOs(): ?string
67
    {
68 4
        return $this->os;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 4
    public function toText(): string
75
    {
76 4
        return sprintf('"%s" "%s"', $this->cpu ?? '', $this->os ?? '');
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function toWire(): string
83
    {
84 1
        return $this->toText();
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     *
90
     * @return HINFO
91
     */
92 1
    public static function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): RdataInterface
93
    {
94 1
        $offset += strlen($rdata);
95
96 1
        return self::fromText($rdata);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     *
102
     * @return HINFO
103
     */
104 2
    public static function fromText(string $text): RdataInterface
105
    {
106 2
        $string = new StringIterator($text);
107 2
        $hinfo = new self();
108 2
        $hinfo->setCpu(self::extractText($string));
109 2
        $hinfo->setOs(self::extractText($string));
110
111 2
        return $hinfo;
112
    }
113
114
    /**
115
     * @param StringIterator $string
116
     *
117
     * @return string
118
     */
119 2
    private static function extractText(StringIterator $string): string
120
    {
121 2
        $txt = new StringIterator();
122
123 2
        if ($string->is(Tokens::DOUBLE_QUOTES)) {
124 2
            TXT::handleTxt($string, $txt);
125 2
            $string->next();
126
        } else {
127
            while ($string->isNot(Tokens::SPACE) && $string->valid()) {
128
                $txt->append($string->current());
129
                $string->next();
130
            }
131
        }
132
133 2
        if ($string->is(Tokens::SPACE)) {
134 2
            $string->next();
135
        }
136
137 2
        return (string) $txt;
138
    }
139
}
140