You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
962 B
35 lines
962 B
<?php
|
|
|
|
namespace App\Domains\Identity\Queries\Dashboard;
|
|
|
|
use App\Domains\Identity\Models\User;
|
|
|
|
class GetTotalUsers
|
|
{
|
|
/**
|
|
* @return array{total_users: int, growth_rate: string}
|
|
*/
|
|
public function fetch(): array
|
|
{
|
|
$lastMonth = now()->subMonth();
|
|
|
|
$currentTotalUsers = User::count();
|
|
$totalUsersLastMonth = User::whereMonth('created_at', $lastMonth->month)
|
|
->whereYear('created_at', $lastMonth->year)
|
|
->count();
|
|
|
|
if ($totalUsersLastMonth > 0) {
|
|
$growthRate = ($currentTotalUsers - $totalUsersLastMonth) / $totalUsersLastMonth;
|
|
$growthRate = $growthRate * 100;
|
|
$growthRate = number_format($growthRate, 2, '.', '');
|
|
$growthRate = "+{$growthRate}%";
|
|
} else {
|
|
$growthRate = '+100%';
|
|
}
|
|
|
|
return [
|
|
'total_users' => $currentTotalUsers,
|
|
'growth_rate' => $growthRate,
|
|
];
|
|
}
|
|
}
|
|
|