A   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 64
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toText() 0 4 1
A setAddress() 0 8 2
A getAddress() 0 4 1
A toWire() 0 8 2
A fromText() 0 4 1
A fromWire() 0 9 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
use Badcow\DNS\Validator;
17
18
/**
19
 * @see https://tools.ietf.org/html/rfc1035#section-3.4.1
20
 */
21
class A implements RdataInterface
22
{
23
    use RdataTrait;
24
25
    const TYPE = 'A';
26
    const TYPE_CODE = 1;
27
28
    /**
29
     * @var string
30
     */
31
    protected $address;
32
33 41
    public function setAddress(string $address): void
34
    {
35 41
        if (!Validator::ipv4($address)) {
36 1
            throw new \InvalidArgumentException(sprintf('The address "%s" is not a valid IPv4 address.', $address));
37
        }
38
39 41
        $this->address = $address;
40 41
    }
41
42
    /**
43
     * @return string
44
     */
45 15
    public function getAddress(): ?string
46
    {
47 15
        return $this->address;
48
    }
49
50 10
    public function toText(): string
51
    {
52 10
        return $this->address ?? '';
53
    }
54
55
    /**
56
     * @throws \InvalidArgumentException
57
     */
58 7
    public function toWire(): string
59
    {
60 7
        if (false === $encoded = @inet_pton($this->address)) {
61 2
            throw new \InvalidArgumentException(sprintf('The IP address "%s" cannot be encoded. Check that it is a valid IP address.', $this->address));
62
        }
63
64 5
        return $encoded;
65
    }
66
67 14
    public function fromText(string $text): void
68
    {
69 14
        $this->setAddress($text);
70 14
    }
71
72
    /**
73
     * @throws DecodeException
74
     */
75 12
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
76
    {
77 12
        if (false === $address = @inet_ntop(substr($rdata, $offset, 4))) {
78 1
            throw new DecodeException(static::TYPE, $rdata);
79
        }
80 12
        $offset += 4;
81
82 12
        $this->setAddress($address);
83 12
    }
84
}
85