42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\PreregistrationPage;
|
|
use App\Models\Subscriber;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
final class DashboardStatisticsService
|
|
{
|
|
/**
|
|
* @return array{total_pages: int, total_subscribers: int, active_pages: int}
|
|
*/
|
|
public function forUser(User $user): array
|
|
{
|
|
$pagesQuery = PreregistrationPage::query();
|
|
if (! $user->isSuperadmin()) {
|
|
$pagesQuery->where('user_id', $user->id);
|
|
}
|
|
|
|
$now = Carbon::now();
|
|
$totalPages = (clone $pagesQuery)->count();
|
|
$pageIds = (clone $pagesQuery)->pluck('id');
|
|
$totalSubscribers = $pageIds->isEmpty()
|
|
? 0
|
|
: Subscriber::whereIn('preregistration_page_id', $pageIds)->count();
|
|
$activePages = (clone $pagesQuery)
|
|
->where('start_date', '<=', $now)
|
|
->where('end_date', '>=', $now)
|
|
->count();
|
|
|
|
return [
|
|
'total_pages' => $totalPages,
|
|
'total_subscribers' => $totalSubscribers,
|
|
'active_pages' => $activePages,
|
|
];
|
|
}
|
|
}
|