Passed
Push — master ( 9223d2...163f52 )
by Christian
55s queued 10s
created

DNSRecord::setTTL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace RemotelyLiving\PHPDNS\Entities;
3
4
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable;
5
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable;
6
7
class DNSRecord extends EntityAbstract implements Arrayable, Serializable
8
{
9
    /**
10
     * @var \RemotelyLiving\PHPDNS\Entities\DNSRecordType
11
     */
12
    private $recordType;
13
14
    /**
15
     * @var \RemotelyLiving\PHPDNS\Entities\Hostname
16
     */
17
    private $hostname;
18
19
    /**
20
     * @var int
21
     */
22
    private $TTL;
23
24
    /**
25
     * @var \RemotelyLiving\PHPDNS\Entities\IPAddress|null
26
     */
27
    private $IPAddress;
28
29
    /**
30
     * @var string
31
     */
32
    private $class;
33
34
    /**
35
     * @var \RemotelyLiving\PHPDNS\Entities\DataAbstract|null
36
     */
37
    private $data;
38
39
    public function __construct(
40
        DNSRecordType $recordType,
41
        Hostname $hostname,
42
        int $ttl,
43
        IPAddress $IPAddress = null,
44
        string $class = 'IN',
45
        DataAbstract $data = null
46
    ) {
47
        $this->recordType = $recordType;
48
        $this->hostname = $hostname;
49
        $this->TTL = $ttl;
50
        $this->IPAddress = $IPAddress;
51
        $this->class = $class;
52
        $this->data = $data;
53
    }
54
55
    public static function createFromPrimitives(
56
        string $recordType,
57
        string $hostname,
58
        int $ttl,
59
        string $IPAddress = null,
60
        string $class = 'IN',
61
        string $data = null
62
    ): DNSRecord {
63
        $type = DNSRecordType::createFromString($recordType);
64
        $hostname = Hostname::createFromString($hostname);
65
        $data = ($data !== null)
66
            ? DataAbstract::createFromTypeAndString($type, $data)
67
            : null;
68
69
        return new static(
70
            $type,
71
            $hostname,
72
            $ttl,
73
            $IPAddress ? IPAddress::createFromString($IPAddress): null,
74
            $class,
75
            $data
76
        );
77
    }
78
79
    public function getType(): DNSRecordType
80
    {
81
        return $this->recordType;
82
    }
83
84
    public function getHostname(): Hostname
85
    {
86
        return $this->hostname;
87
    }
88
89
    public function getTTL(): int
90
    {
91
        return $this->TTL;
92
    }
93
94
    public function getIPAddress(): ?IPAddress
95
    {
96
        return $this->IPAddress;
97
    }
98
99
    public function getClass(): string
100
    {
101
        return $this->class;
102
    }
103
104
    public function getData(): ?DataAbstract
105
    {
106
        return $this->data;
107
    }
108
109
    public function setTTL(int $ttl) : self
110
    {
111
        $this->TTL = $ttl;
112
        return $this;
113
    }
114
115
    public function toArray(): array
116
    {
117
        $formatted = [
118
            'hostname' => (string)$this->hostname,
119
            'type' => (string)$this->recordType,
120
            'TTL' => $this->TTL,
121
            'class' => $this->class,
122
        ];
123
124
        if ($this->IPAddress) {
125
            $formatted['IPAddress'] = (string)$this->IPAddress;
126
        }
127
128
        if ($this->data) {
129
            $formatted['data'] = (string)$this->data;
130
        }
131
132
        return $formatted;
133
    }
134
135
    public function equals(DNSRecord $record): bool
136
    {
137
        return $this->hostname->equals($record->getHostname())
138
            && $this->recordType->equals($record->getType())
139
            && (string)$this->data === (string)$record->getData() // could be null
140
            && (string)$this->IPAddress === (string)$record->getIPAddress(); // could be null
141
    }
142
143
    public function serialize(): string
144
    {
145
        return \serialize($this->toArray());
146
    }
147
148
    /**
149
     * @param string $serialized
150
     */
151
    public function unserialize($serialized): void
152
    {
153
        $unserialized = \unserialize($serialized);
154
155
        $rawIPAddres = $unserialized['IPAddress'] ?? null;
156
        $this->recordType = DNSRecordType::createFromString($unserialized['type']);
157
        $this->hostname = Hostname::createFromString($unserialized['hostname']);
158
        $this->TTL = (int) $unserialized['TTL'];
159
        $this->IPAddress = $rawIPAddres ? IPAddress::createFromString($rawIPAddres): null;
160
        $this->class = $unserialized['class'];
161
        $this->data = (isset($unserialized['data']))
162
         ? DataAbstract::createFromTypeAndString($this->recordType, $unserialized['data'])
163
         : null;
164
    }
165
166
    public function jsonSerialize(): array
167
    {
168
        return $this->toArray();
169
    }
170
}
171