Skip to content

Commit 03f405b

Browse files
done
1 parent f715a06 commit 03f405b

6 files changed

Lines changed: 62 additions & 40 deletions

File tree

app/Services/CloudflareService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public function dnsNames () {
2222
return $this->findDomain($this->zoneId)['name_servers'] ?? [];
2323

2424
}
25-
public function setZone ( string $nameOrZoneId ) {
25+
public function setZone ( string $name = null ) {
2626

27-
$this->zoneId = !str_contains($nameOrZoneId, '.') ? $nameOrZoneId : $this->getDomain($nameOrZoneId)['id'] ?? null;
27+
$name = $name ?: $this->zoneId;
28+
$this->zoneId = !str_contains($name, '.') ? $name : $this->getDomain($name)['id'] ?? null;
2829
return $this;
2930

3031
}

app/Services/DomainService.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,43 @@ public function __construct(
1414
protected VercelService $vercelService,
1515
) { parent::__construct($domainRepository); }
1616

17-
public function resolve ( array $data = [] ) {
17+
public function resolve ( array $data = [], bool $defaults = true, bool $filled = true ) {
1818

19-
return collect([
20-
['dest' => 'client', 'name' => $client = data_get($data, 'client_domain_name', 'www')],
21-
['dest' => 'admin', 'name' => data_get($data, 'admin_domain_name', $client === 'www' ? 'admin' : null)],
19+
$list = collect([
20+
['dest' => 'client', 'name' => data_get($data, 'client_domain_name') ?: ($defaults ? 'www' : '')],
21+
['dest' => 'admin', 'name' => data_get($data, 'admin_domain_name') ?: ($defaults ? 'admin' : '')],
2222
['dest' => 'vendor', 'name' => data_get($data, 'vendor_domain_name')],
2323
['dest' => 'delivery', 'name' => data_get($data, 'delivery_domain_name')],
2424
['dest' => 'affiliate', 'name' => data_get($data, 'affiliate_domain_name')],
2525
['dest' => 'blog', 'name' => data_get($data, 'blog_domain_name')],
2626
['dest' => 'app', 'name' => data_get($data, 'app_domain_name')],
2727
['dest' => 'api', 'name' => data_get($data, 'api_domain_name')],
2828
['dest' => 'cdn', 'name' => data_get($data, 'cdn_domain_name')],
29-
])->filter(fn($item) => $item['name'])->all();
29+
]);
30+
31+
return $filled ? $list->filter(fn($item) => $item['name']) : $list->filter(fn($item) => !$item['name']);
3032

3133
}
32-
public function sync ( Domain $domain ) {
34+
public function clean ( array $items = [] ) {
3335

34-
$this->domainRepository->query()->where('dest', $domain->dest)->where('name', '!=', $domain->name)->get()->each(function ($item) {
36+
collect($items)->each(fn($item) => $this->domainRepository->query()->where('dest', $item['dest'])->get()->each(fn($d) =>
37+
$this->remove($d)
38+
));
3539

36-
$this->vercelService->setProject($item->dest)->deleteDomain($item->name);
37-
$this->cloudflareService->deleteSubDomain($item->provider_id ?? $item->name);
38-
$this->delete($item->id);
40+
}
41+
public function remove ( Domain $domain ) {
3942

40-
});
43+
$this->vercelService->setProject($domain->dest)->deleteDomain($domain->name);
44+
$this->cloudflareService->setZone($domain->zone?->provider_id)->deleteSubDomain($domain->provider_id ?? $domain->name);
45+
$this->domainRepository->forceDelete($domain->id);
46+
47+
}
48+
public function sync ( Domain $domain ) {
4149

50+
$this->domainRepository->query()->where('id', '!=', $domain->id)->where('dest', $domain->dest)->get()->each(fn($d) =>
51+
$this->remove($d)
52+
);
53+
4254
$this->deleteCache();
4355

4456
}
@@ -84,19 +96,22 @@ public function register ( Zone $zone, string $name, string $dest ) {
8496
public function check ( string $domain = null, array $data = [] ) {
8597

8698
if ( $domain && store()?->zone?->name !== $domain ) return (bool) $this->zoneService->validate($domain);
87-
if ( !$zone = $this->zoneService->currentZone() ) return false;
99+
if ( !$zone = $this->zoneService->current() ) return false;
88100

89-
return collect($this->resolve($data))->every(fn($item) => $this->validate($zone, $item['name']));
101+
return $this->resolve($data, (bool) $domain)->every(fn($item) => $this->validate($zone, $item['name']));
90102

91103
}
92104
public function apply ( array $data = [] ) {
93105

94106
$domain = string(data_get($data, 'domain_name'));
95-
107+
96108
if ( !$this->check($domain, $data) ) return false;
97109
if ( !$zone = $this->zoneService->apply($domain) ) return false;
98110

99-
return collect($this->resolve($data))->every(fn($item) => $this->register($zone, $item['name'], $item['dest']));
111+
$result = $this->resolve($data, (bool) $domain)->every(fn($item) => $this->register($zone, $item['name'], $item['dest']));
112+
if ( $result ) $this->runJob([static::class, 'clean'], [$this->resolve($data, (bool) $domain, false)->all()]);
113+
114+
return $result;
100115

101116
}
102117

app/Services/StoreService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function subscribe ( Store $store, array $data = [] ) {
1818

1919
if ( !$this->subscriptionService->subscribeOrRenew($store, $data) ) throwError('subscription', 'subscription failed');
2020
if ( !$this->withTenant($store->id, callback: fn() => $this->domainService->apply($data)) ) throwError('domains', 'invalid domains');
21-
21+
2222
}
2323
public function store ( array $data = [], array $scopes = [] ) {
2424

@@ -28,9 +28,9 @@ public function store ( array $data = [], array $scopes = [] ) {
2828
$store = $this->storeRepository->create(array_merge($data, ['owner_id' => $owner->id]), $scopes);
2929

3030
$this->subscribe($store, $data);
31-
$this->deleteCache();
32-
$this->runJob([SeedingService::class, 'run'], [$store, $data]);
31+
$this->runJob([SeedingService::class, 'run'], [$store, withoutFiles($data)]);
3332

33+
$this->deleteCache();
3434
return parent::show($store->id);
3535

3636
});

app/Services/ZoneService.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,48 @@
22

33
namespace App\Services;
44
use App\Repositories\ZoneRepository;
5+
use App\Repositories\DomainRepository;
56
use App\Models\Zone;
67

78
class ZoneService extends BaseService {
89

910
public function __construct(
1011
protected ZoneRepository $zoneRepository,
12+
protected DomainRepository $domainRepository,
1113
protected CloudflareService $cloudflareService,
1214
protected VercelService $vercelService,
1315
) { parent::__construct($zoneRepository); }
1416

15-
public function currentZone () {
16-
17-
$zone = store()?->zone;
18-
if ( $zone ) return $zone->isValid() ? $zone : null;
17+
public function parent () {
1918

2019
$zone = $this->withTenant(store_parent_id(), callback: fn() => store_parent()?->zone);
2120
if ( $zone ) return $zone->isValid() ? $zone : null;
2221

2322
return $this->withoutTenant(fn() => $this->zoneRepository->findByName(config('settings.default.domains.base.name')));
2423

2524
}
26-
public function sync ( Zone $zone ) {
25+
public function current () {
2726

28-
$this->zoneRepository->query()->where('name', '!=', $zone->name)->get()->each(function ($item) {
27+
$zone = store()?->zone;
28+
return !$zone ? $this->parent() : ($zone->isValid() ? $zone : null);
2929

30-
$this->vercelService->deleteDomain($item->name);
31-
32-
$item->domains?->each(function($domain) {
33-
$this->vercelService->setProject($domain->dest)->deleteDomain($domain->name);
34-
$domain->delete();
35-
});
30+
}
31+
public function remove ( Zone $zone ) {
3632

37-
$this->cloudflareService->deleteDomain($item->provider_id ?? $item->name);
38-
$this->delete($item->id);
33+
$this->vercelService->deleteDomain($zone->name);
3934

35+
$zone->domains?->each(function($domain) {
36+
$this->vercelService->setProject($domain->dest)->deleteDomain($domain->name);
37+
$this->domainRepository->forceDelete($domain->id);
4038
});
4139

40+
$this->cloudflareService->deleteDomain($zone->provider_id ?? $zone->name);
41+
$this->zoneRepository->forceDelete($zone->id);
42+
43+
}
44+
public function sync ( Zone $zone ) {
45+
46+
$this->zoneRepository->query()->where('id', '!=', $zone->id)->get()->each(fn($z) => $this->remove($z));
4247
$this->deleteCache();
4348

4449
}
@@ -89,7 +94,10 @@ public function register ( string $name ) {
8994
}
9095
public function apply ( string $name = null ) {
9196

92-
return $name && !$this->zoneRepository->findBy('name', $name) ? $this->register($name) : $this->currentZone();
97+
if ( $name ) return $this->zoneRepository->findBy('name', $name) ? $this->current() : $this->register($name);
98+
99+
if ( store()?->zone ) $this->runJob([static::class, 'remove'], [store()->zone]);
100+
return $this->parent();
93101

94102
}
95103

app/Traits/Bases/HasBaseService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function failNow ( string $name = null ) {
7070
}
7171
protected function runJob ( mixed $job, array $args = [] ) {
7272

73-
return dispatch(new ExecuteJob($job, $args));
73+
return dispatch(new ExecuteJob($job, $args))->afterCommit();
7474

7575
}
7676
protected function withTenant ( int $id = null, int $userId = null, callable $callback = null ) {

app/Traits/Model/HasBootedLogs.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ trait HasBootedLogs {
99

1010
public function handleRequestFiles () {
1111

12-
if ( !request()?->all() ) return;
12+
if ( app()->bound('files.handled') || !request()?->all() ) return;
13+
app()->instance('files.handled', true);
1314

1415
if ( request()->hasFile('image') ) {
1516

@@ -28,9 +29,6 @@ public function handleRequestFiles () {
2829
if ( parse(request('deleted_files')) ) $this->deleteFiles(parse(request('deleted_files')));
2930
if ( request()->has('active') && is_admin() ) $this->update(['active' => bool(request('active'))]);
3031

31-
request()->replace(request()->except(['delete_image', 'deleted_files', 'active']));
32-
request()->files->replace([]);
33-
3432
}
3533
public function modelBooted ( string $event ) {
3634

0 commit comments

Comments
 (0)