Skip to content

Commit 99a9b39

Browse files
TinyMemoriaBoy132
andauthored
fix subdomain edit and improved error handling (#90)
* fix subdomain edit and improved error handling * changed variable to camelCase and response handling * cleanup --------- Co-authored-by: Boy132 <mail@boy132.de>
1 parent d249909 commit 99a9b39

2 files changed

Lines changed: 46 additions & 25 deletions

File tree

subdomains/src/Models/Subdomain.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,6 @@ public function upsertOnCloudflare(): void
6262
throw new Exception('Server has no allocation');
6363
}
6464

65-
// @phpstan-ignore staticMethod.notFound
66-
$response = Http::cloudflare()->get("zones/{$this->domain->cloudflare_id}/dns_records/$this->cloudflare_id", ['search' => $this->name])->json();
67-
68-
if ($response['success']) {
69-
if (!empty($response['result'])) {
70-
throw new Exception('A subdomain with that name already exists');
71-
}
72-
} else {
73-
if ($response['errors'] && count($response['errors']) > 0) {
74-
throw new Exception($response['errors'][0]['message']);
75-
}
76-
}
77-
78-
$payload = [
79-
'name' => $this->name,
80-
'type' => $this->record_type,
81-
'comment' => 'Created by Pelican Subdomains plugin',
82-
'content' => $this->server->allocation->ip,
83-
'proxied' => false,
84-
];
85-
8665
if ($this->record_type === 'SRV') {
8766
$srvTarget = $this->server->node->srv_target; // @phpstan-ignore property.notFound
8867

@@ -96,8 +75,10 @@ public function upsertOnCloudflare(): void
9675
throw new Exception('Server has no SRV type');
9776
}
9877

78+
$searchName = "$srvServiceType->value.$this->name";
79+
9980
$payload = [
100-
'name' => "$srvServiceType->value.$this->name",
81+
'name' => $searchName,
10182
'type' => $this->record_type,
10283
'comment' => 'Created by Pelican Subdomains plugin',
10384
'data' => [
@@ -112,6 +93,36 @@ public function upsertOnCloudflare(): void
11293
if ($this->server->allocation->ip === '0.0.0.0' || $this->server->allocation->ip === '::') {
11394
throw new Exception('Server has invalid allocation ip (0.0.0.0 or ::)');
11495
}
96+
97+
$searchName = $this->name;
98+
99+
$payload = [
100+
'name' => $searchName,
101+
'type' => $this->record_type,
102+
'comment' => 'Created by Pelican Subdomains plugin',
103+
'content' => $this->server->allocation->ip,
104+
'proxied' => false,
105+
];
106+
}
107+
108+
// @phpstan-ignore staticMethod.notFound
109+
$searchResponse = Http::cloudflare()->get("zones/{$this->domain->cloudflare_id}/dns_records", [
110+
'name' => $searchName,
111+
'type' => $this->record_type,
112+
])->json();
113+
114+
if ($searchResponse['success']) {
115+
$results = $searchResponse['result'] ?? [];
116+
117+
foreach ($results as $record) {
118+
if ($record['id'] !== $this->cloudflare_id) {
119+
throw new Exception('A subdomain with that name already exists');
120+
}
121+
}
122+
} else {
123+
if ($searchResponse['errors'] && count($searchResponse['errors']) > 0) {
124+
throw new Exception($searchResponse['errors'][0]['message']);
125+
}
115126
}
116127

117128
if ($this->cloudflare_id) {

subdomains/src/Services/SubdomainService.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ class SubdomainService
1414
*/
1515
public function handle(array $data, ?Subdomain $subdomain = null): Subdomain
1616
{
17-
$subdomain ??= Subdomain::create($data);
18-
$subdomain->update($data);
17+
$newSubdomain = true;
18+
19+
if (is_null($subdomain)) {
20+
$subdomain = Subdomain::create($data);
21+
} else {
22+
$subdomain->update($data);
23+
$newSubdomain = false;
24+
}
25+
26+
$subdomain->refresh();
1927

2028
try {
2129
$subdomain->upsertOnCloudflare();
2230
} catch (Exception $exception) {
23-
$subdomain->delete();
31+
if ($newSubdomain) {
32+
$subdomain->delete();
33+
}
2434

2535
throw $exception;
2636
}

0 commit comments

Comments
 (0)