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

A   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 80
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toWire() 0 7 2
A getAddress() 0 3 1
A fromText() 0 6 1
A setAddress() 0 7 2
A toText() 0 3 1
A fromWire() 0 10 2
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.4.1
18
 */
19
class A implements RdataInterface
20
{
21
    use RdataTrait;
22
23
    const TYPE = 'A';
24
    const TYPE_CODE = 1;
25
26
    /**
27
     * @var string
28
     */
29
    protected $address;
30
31
    /**
32
     * @param string $address
33
     */
34 29
    public function setAddress(string $address): void
35
    {
36 29
        if (false === @inet_pton($address)) {
37
            throw new \InvalidArgumentException(sprintf('The address "%s" is not a valid IP address.', $address));
38
        }
39
40 29
        $this->address = $address;
41 29
    }
42
43
    /**
44
     * @return string
45
     */
46 10
    public function getAddress(): ?string
47
    {
48 10
        return $this->address;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 7
    public function toText(): string
55
    {
56 7
        return $this->address ?? '';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @throws \InvalidArgumentException
63
     */
64 2
    public function toWire(): string
65
    {
66 2
        if (false === $encoded = inet_pton($this->address)) {
67
            throw new \InvalidArgumentException(sprintf('The IP address "%s" cannot be encoded. Check that it is a valid IP address.', $this->address));
68
        }
69
70 2
        return $encoded;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 9
    public static function fromText(string $text): RdataInterface
77
    {
78 9
        $a = new static();
79 9
        $a->setAddress($text);
80
81 9
        return $a;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * @throws DecodeException
88
     */
89 6
    public static function fromWire(string $rdata): RdataInterface
90
    {
91 6
        if (false === $address = @inet_ntop($rdata)) {
92 2
            throw new DecodeException(static::TYPE, $rdata);
93
        }
94
95 4
        $a = new static();
96 4
        $a->setAddress($address);
97
98 4
        return $a;
99
    }
100
}
101