1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Cocotte\DigitalOcean; |
4
|
|
|
|
5
|
|
|
use Darsyn\IP\IP; |
6
|
|
|
use DigitalOceanV2\Api; |
7
|
|
|
use DigitalOceanV2\Entity; |
8
|
|
|
|
9
|
|
|
final class DomainRecord |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
const A = 'A'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Api\DomainRecord |
16
|
|
|
*/ |
17
|
|
|
private $domainRecordApi; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Domain |
21
|
|
|
*/ |
22
|
|
|
private $domain; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Api\DomainRecord $domainRecordApi |
26
|
|
|
* @param Domain $domain |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Api\DomainRecord $domainRecordApi, Domain $domain) |
29
|
|
|
{ |
30
|
|
|
$this->domainRecordApi = $domainRecordApi; |
31
|
|
|
$this->domain = $domain; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function update(Hostname $hostname, IP $ip): Entity\DomainRecord |
35
|
|
|
{ |
36
|
|
|
$record = $this->get($hostname); |
37
|
|
|
|
38
|
|
|
return $this->domainRecordApi->updateData( |
39
|
|
|
$hostname->domainName(), |
40
|
|
|
$record->id, |
41
|
|
|
$ip->getShortAddress() |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function create(Hostname $hostname, IP $ip): Entity\DomainRecord |
46
|
|
|
{ |
47
|
|
|
return $this->domainRecordApi->create( |
48
|
|
|
$hostname->domainName(), |
49
|
|
|
self::A, |
50
|
|
|
$hostname->recordName(), |
51
|
|
|
$ip->getShortAddress() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function delete(Hostname $hostname): void |
56
|
|
|
{ |
57
|
|
|
$record = $this->get($hostname); |
58
|
|
|
|
59
|
|
|
$this->domainRecordApi->delete( |
60
|
|
|
$hostname->domainName(), |
61
|
|
|
$record->id |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function get(Hostname $hostname): Entity\DomainRecord |
66
|
|
|
{ |
67
|
|
|
$records = $this->domainRecordApi->getAll($hostname->domainName()); |
68
|
|
|
|
69
|
|
|
foreach ($records as $record) { |
70
|
|
|
if (!$this->isTypeARecord($record)) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
if ($hostname->matchDomainRecord($record)) { |
74
|
|
|
return $record; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw new \Exception('record does not exist'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function exists(Hostname $hostname): bool |
82
|
|
|
{ |
83
|
|
|
$records = $this->domainRecordApi->getAll($hostname->domainName()); |
84
|
|
|
|
85
|
|
|
foreach ($records as $record) { |
86
|
|
|
if (!$this->isTypeARecord($record)) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
if ($hostname->matchDomainRecord($record)) { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function isUpToDate(Hostname $hostname, IP $ip): bool |
98
|
|
|
{ |
99
|
|
|
return $this->get($hostname)->data === $ip->getShortAddress(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function isTypeARecord($record): bool |
103
|
|
|
{ |
104
|
|
|
return $record->type === self::A; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |