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

PolymorphicRdata::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
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
 * Used to create RData of types that have not yet been implemented in the library.
18
 */
19
class PolymorphicRdata implements RdataInterface
20
{
21
    /**
22
     * The RData type.
23
     *
24
     * @var string
25
     */
26
    private $type;
27
28
    /**
29
     * @var string|null
30
     */
31
    private $data;
32
33
    /**
34
     * @var int|null
35
     */
36
    private $typeCode;
37
38
    /**
39
     * PolymorphicRdata constructor.
40
     *
41
     * @param string|null $type
42
     * @param string|null $data
43
     */
44 1
    public function __construct(?string $type = null, ?string $data = null)
45
    {
46 1
        if (null !== $type) {
47 1
            $this->setType($type);
48
        }
49
50 1
        if (null !== $data) {
51 1
            $this->setData($data);
52
        }
53 1
    }
54
55
    /**
56
     * @param string $type
57
     */
58 1
    public function setType(string $type): void
59
    {
60
        try {
61 1
            $this->typeCode = Types::getTypeCode($type);
62
        } catch (UnsupportedTypeException $e) {
63
            $this->typeCode = 0;
64
        }
65 1
        $this->type = $type;
66 1
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function getType(): string
72
    {
73 1
        return $this->type;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getTypeCode(): int
80
    {
81
        return $this->getTypeCode();
82
    }
83
84
    /**
85
     * @param string $data
86
     */
87 1
    public function setData(string $data): void
88
    {
89 1
        $this->data = $data;
90 1
    }
91
92
    /**
93
     * @return string|null
94
     */
95 1
    public function getData(): ?string
96
    {
97 1
        return $this->data;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function toText(): string
104
    {
105 1
        return $this->getData() ?? '';
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function toWire(): string
112
    {
113
        return $this->data ?? '';
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public static function fromText(string $text): RdataInterface
120
    {
121
        return new self(null, $text);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public static function fromWire(string $rdata): RdataInterface
128
    {
129
        return new self(null, $rdata);
130
    }
131
}
132