Completed
Branch Version3 (97e220)
by Sam
01:25
created

PolymorphicRdata::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
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|null
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 = TypeCodes::getTypeCode($type);
62 1
        } catch (\Exception $e) {
63 1
            $this->typeCode = 65535;
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
     * @deprecated
86
     *
87
     * @return string
88
     */
89
    public function output(): string
90
    {
91
        @trigger_error('Method RdataInterface::output() has been deprecated. Use RdataInterface::toText().', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
92
93
        return $this->toText();
94
    }
95
96
    /**
97
     * @param string $data
98
     */
99 1
    public function setData(string $data): void
100
    {
101 1
        $this->data = $data;
102 1
    }
103
104
    /**
105
     * @return string|null
106
     */
107 1
    public function getData(): ?string
108
    {
109 1
        return $this->data;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function toText(): string
116
    {
117 1
        return $this->getData() ?? '';
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function toWire(): string
124
    {
125
        // TODO: Implement toWire() method.
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public static function fromText(string $text): RdataInterface
132
    {
133
        // TODO: Implement fromText() method.
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public static function fromWire(string $rdata): RdataInterface
140
    {
141
        // TODO: Implement fromWire() method.
142
    }
143
}
144