'components', 'incidents' => 'incidents', 'subscribers' => 'subscribers']; public function delete(StatusPage $page): void { $id = (int) $page->getKey(); $settingKeys = DB::table('status_page_settings')->where('status_page_id', $id)->pluck('key'); DB::transaction(function () use ($page, $id): void { // Written first: once the page is gone the line keeps its name and loses its page. Audit::record('page.deleted', $page, $this->countsByPage()[$id] ?? []); foreach (self::OWNED as $table) { DB::table($table)->where('status_page_id', $id)->delete(); } $page->delete(); }); foreach ($settingKeys as $key) { Cache::forget("status_page.$id.setting.$key"); } Storage::disk('public')->deleteDirectory('brand/pages/'.$id); } /** * What each page would take with it, for every page in three queries. * * @return array> */ public function countsByPage(): array { $counts = []; foreach (self::COUNTED as $label => $table) { $totals = DB::table($table)->groupBy('status_page_id') ->selectRaw('status_page_id, count(*) as total')->pluck('total', 'status_page_id'); foreach ($totals as $pageId => $total) { $counts[(int) $pageId][$label] = (int) $total; } } return array_map(fn (array $own) => array_merge(array_fill_keys(array_keys(self::COUNTED), 0), $own), $counts); } /** * "4 services, 12 incidents and 48 subscribers": only what there is. * * @param array $counts */ public static function summary(array $counts): string { $parts = []; foreach (array_keys(self::COUNTED) as $label) { $total = (int) ($counts[$label] ?? 0); if ($total > 0) { $parts[] = $total.' '.Str::plural(Str::singular($label), $total); } } $last = array_pop($parts); return $parts === [] ? (string) $last : implode(', ', $parts).' and '.$last; } }