refactor: improve code quality across controllers and services

- DRY FurniEditorController: extract duplicate try/catch blocks into handleApiError(),
  formatItemData(), buildUpdateData(), buildInsertData(), castValue() methods
- ProfileController: replace 45 lines of manual date formatting with Carbon's diffForHumans()
- Replace custom Password rule (180 lines) with Laravel's built-in Password::min() rule
- RadioController: extract RadioStreamService and RadioScheduleService, reducing from 608 to 323 lines
- Add RadioSettings enum to replace magic strings throughout radio feature
- Add CurrencyTypes::columnName() helper method
- Add consistent return types (JsonResponse, View, RedirectResponse) to all controller methods
This commit is contained in:
root
2026-05-19 19:16:59 +02:00
parent 8567ce6951
commit 81e99933e4
9 changed files with 636 additions and 731 deletions
+33 -44
View File
@@ -5,11 +5,12 @@ namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Models\Miscellaneous\WebsiteSetting;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Carbon;
class ProfileController extends Controller
{
public function __invoke(User $user)
public function __invoke(User $user): View
{
$user->load([
'friends.friend:id,username,look',
@@ -19,13 +20,8 @@ class ProfileController extends Controller
'badges',
]);
$showStats = WebsiteSetting::where('key', 'profile_show_stats')->first()?->value ?? '1';
$showOnline = WebsiteSetting::where('key', 'profile_show_online_status')->first()?->value ?? '1';
$accountAge = $this->getAccountAge($user->account_created);
$lastLogin = $this->getLastLogin($user->last_login);
$totalFriends = $user->friends()->count();
$totalGuilds = $user->guilds()->count();
$showStats = (bool) (WebsiteSetting::where('key', 'profile_show_stats')->first()?->value ?? '1');
$showOnline = (bool) (WebsiteSetting::where('key', 'profile_show_online_status')->first()?->value ?? '1');
return view('user.profile', [
'user' => $user,
@@ -36,56 +32,49 @@ class ProfileController extends Controller
'badges' => $user->badges->take(3),
'showStats' => $showStats,
'showOnline' => $showOnline,
'accountAge' => $accountAge,
'lastLogin' => $lastLogin,
'totalFriends' => $totalFriends,
'totalGuilds' => $totalGuilds,
'accountAge' => $this->getAccountAge($user->account_created),
'lastLogin' => $this->getLastLogin($user->last_login),
'totalFriends' => $user->friends()->count(),
'totalGuilds' => $user->guilds()->count(),
]);
}
private function getAccountAge(int $timestamp): string
{
$created = Carbon::createFromTimestamp($timestamp);
$now = Carbon::now();
$days = $created->diffInDays($now);
if ($days < 7) {
return $days . ' day' . ($days !== 1 ? 's' : '');
} elseif ($days < 30) {
$weeks = floor($days / 7);
return $weeks . ' week' . ($weeks !== 1 ? 's' : '');
} elseif ($days < 365) {
$months = floor($days / 30);
return $months . ' month' . ($months !== 1 ? 's' : '');
} else {
$years = floor($days / 365);
return $years . ' year' . ($years !== 1 ? 's' : '');
if ($created->diffInYears() >= 1) {
return $created->diffInYears() . ' ' . str('year')->plural($created->diffInYears());
}
if ($created->diffInMonths() >= 1) {
return $created->diffInMonths() . ' ' . str('month')->plural($created->diffInMonths());
}
if ($created->diffInWeeks() >= 1) {
return $created->diffInWeeks() . ' ' . str('week')->plural($created->diffInWeeks());
}
return $created->diffInDays() . ' ' . str('day')->plural($created->diffInDays());
}
private function getLastLogin(int $timestamp): string
{
$lastLogin = Carbon::createFromTimestamp($timestamp);
$now = Carbon::now();
$diff = $now->diffInMinutes($lastLogin);
$diffInMinutes = $lastLogin->diffInMinutes();
if ($diff < 1) {
if ($diffInMinutes < 1) {
return 'Just now';
} elseif ($diff < 60) {
return $diff . ' minute' . ($diff !== 1 ? 's' : '') . ' ago';
} elseif ($diff < 1440) {
$hours = floor($diff / 60);
return $hours . ' hour' . ($hours !== 1 ? 's' : '') . ' ago';
} elseif ($diff < 10080) {
$days = floor($diff / 1440);
return $days . ' day' . ($days !== 1 ? 's' : '') . ' ago';
} else {
return $lastLogin->format('d M Y');
}
if ($diffInMinutes < 60) {
return $lastLogin->diffForHumans();
}
if ($diffInMinutes < 10080) {
return $lastLogin->diffForHumans();
}
return $lastLogin->format('d M Y');
}
}