Files
Atomcms-edit/app/Services/HousekeepingPermissionsService.php
T
root 1acd96b78a Optimize queries: fix N+1, SELECT *, and missing eager loading
N+1 fixes:
- Add withCount('furniture') to rare categories sidebar (prevents N queries)
- Add tags and user.permission eager load in ArticleController
- Add rooms and photos.user eager load in ProfileController
- Add user eager load in MediaApiController (API was returning null user data)

SELECT * fixes:
- Replace WebsitePermission::all()->pluck() with direct pluck()
- Replace WebsiteHousekeepingPermission::all()->pluck() with direct pluck()
- Add select(['id', 'public_name']) to ItemBase query in furniItems()
- Add select(['id', 'name']) to help categories query
- Add select(['id', 'name', 'code']) to languages query

Memory/performance:
- Replace full collection load with aggregate queries in getRareStatistics()
- Add limit(50) to open tickets query in TicketController
2026-06-29 18:50:32 +02:00

44 lines
1.1 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\WebsiteHousekeepingPermission;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
readonly class HousekeepingPermissionsService
{
private const string CACHE_KEY = 'housekeeping_permissions';
private const int CACHE_DURATION_MINUTES = 30;
private Collection $permissions;
public function __construct()
{
$this->permissions = Cache::remember(
self::CACHE_KEY,
now()->addMinutes(self::CACHE_DURATION_MINUTES),
fn (): Collection => WebsiteHousekeepingPermission::pluck('min_rank', 'permission'),
);
}
public function getOrDefault(string $permissionName, bool $default = false): bool
{
if (! $this->permissions->has($permissionName)) {
return $default;
}
$requiredRank = (int) $this->permissions->get($permissionName);
return auth()->check() && auth()->user()->rank >= $requiredRank;
}
public static function clearCache(): void
{
Cache::forget(self::CACHE_KEY);
}
}