$data * @return array */ private function fillWebsites(array $data): array { $tenant = $this->record; if (! $tenant instanceof Tenant) { return $data; } $data['websites'] = $tenant->sites()->withoutGlobalScope('tenant')->with('domains')->get() ->map(fn (Site $site): array => [ 'id' => $site->id, 'name' => $site->name, 'hosts' => $site->domains->pluck('host')->all(), ]) ->all(); return $data; } /** * @param array $data * @return array */ private function takeWebsites(array $data): array { $this->websites = is_array($data['websites'] ?? null) ? array_values($data['websites']) : []; $this->normaliseWebsites(); unset($data['websites']); return $data; } /** * Cleans the entered addresses and refuses what already belongs elsewhere, before anything is written. */ private function normaliseWebsites(): void { $seen = []; foreach ($this->websites as $index => $website) { $hosts = []; foreach ((array) ($website['hosts'] ?? []) as $host) { $host = SiteDomain::normalise(is_string($host) ? $host : null); if ($host === '') { continue; } if (in_array($host, $seen, true)) { throw ValidationException::withMessages([ 'data.websites' => 'Die Adresse '.$host.' ist mehrfach eingetragen.', ]); } $seen[] = $host; $hosts[] = $host; } $this->websites[$index]['hosts'] = $hosts; } $this->refuseForeignHosts($seen); } /** * @param list $hosts */ private function refuseForeignHosts(array $hosts): void { $ownIds = collect($this->websites)->pluck('id')->filter()->all(); $taken = SiteDomain::query() ->whereIn('host', $hosts) ->whereNotIn('site_id', $ownIds ?: [0]) ->pluck('host'); if ($taken->isNotEmpty()) { throw ValidationException::withMessages([ 'data.websites' => 'Gehört bereits zu einer anderen Website: '.$taken->implode(', '), ]); } } /** * Writes the websites. A website that is new gets an empty home page straight away, because a website * without one answers 404 and the landlord would have no way in. */ private function saveWebsites(): void { $tenant = $this->record; if (! $tenant instanceof Tenant) { return; } app(TenantContext::class)->run($tenant, function () use ($tenant): void { $keep = []; foreach ($this->websites as $website) { $name = trim((string) ($website['name'] ?? '')); if ($name === '') { continue; } $hosts = (array) ($website['hosts'] ?? []); $site = $this->siteFor($tenant, $website['id'] ?? null, $name, $hosts); $keep[] = $site->id; $site->domains()->whereNotIn('host', $hosts ?: [''])->delete(); foreach ($hosts as $host) { SiteDomain::query()->updateOrCreate(['host' => $host], ['site_id' => $site->id]); } } $tenant->sites()->whereNotIn('id', $keep ?: [0])->each(fn (Site $site) => $site->delete()); }); } /** * @param list $hosts */ private function siteFor(Tenant $tenant, mixed $id, string $name, array $hosts): Site { $site = $id ? Site::query()->whereKey($id)->first() : null; if ($site !== null) { $site->update(['name' => $name, 'domain' => $hosts[0] ?? null]); return $site; } $site = Site::query()->create([ 'tenant_id' => $tenant->id, 'name' => $name, 'domain' => $hosts[0] ?? null, 'is_default' => $tenant->sites()->count() === 0, 'theme' => config('themes.base'), ]); Page::query()->create([ 'site_id' => $site->id, 'slug' => Page::HOME, 'title' => $name, 'sections' => [], 'is_published' => true, ]); return $site; } }