Passed
Push — master ( 87c83f...ac88b3 )
by Sam
03:00
created

CNAME::toWire()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
/**
17
 * @see https://tools.ietf.org/html/rfc1035#section-3.3.1
18
 */
19
class CNAME implements RdataInterface
20
{
21
    use RdataTrait;
22
23
    const TYPE = 'CNAME';
24
    const TYPE_CODE = 5;
25
26
    /**
27
     * @var string|null
28
     */
29
    protected $target;
30
31
    /**
32
     * @param string $target
33
     */
34 42
    public function setTarget(string $target): void
35
    {
36 42
        $this->target = $target;
37 42
    }
38
39
    /**
40
     * @return string
41
     */
42 15
    public function getTarget(): ?string
43
    {
44 15
        return $this->target;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 10
    public function toText(): string
51
    {
52 10
        return $this->target ?? '';
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 5
    public function toWire(): string
59
    {
60 5
        if (null === $this->target) {
61 1
            throw new \InvalidArgumentException('Target must be set.');
62
        }
63
64 5
        return self::encodeName($this->target);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 14
    public static function fromText(string $text): RdataInterface
71
    {
72 14
        $cname = new static();
73 14
        $cname->setTarget($text);
74
75 14
        return $cname;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 10
    public static function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): RdataInterface
82
    {
83 10
        $cname = new static();
84 10
        $cname->setTarget(self::decodeName($rdata, $offset));
85
86 10
        return $cname;
87
    }
88
}
89