109 lines
3.1 KiB
PHP
109 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Enums\Portal;
|
|
use App\Enums\PressReleaseStatus;
|
|
use App\Models\PressRelease;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class AdminPerformanceCache
|
|
{
|
|
public const DashboardStats = 'admin.dashboard.stats';
|
|
|
|
public const PressReleaseStats = 'admin.press-releases.stats';
|
|
|
|
public const PressReleaseReviewCount = 'admin.press-releases.review-count';
|
|
|
|
public const PressReleaseCategoryOptions = 'admin.press-releases.category-options';
|
|
|
|
public const ActiveCategoryOptions = 'admin.categories.active-options';
|
|
|
|
public const RoleOptions = 'admin.roles.options';
|
|
|
|
public const UserStats = 'admin.users.stats';
|
|
|
|
public const NewsletterStats = 'admin.newsletter.stats';
|
|
|
|
public const PresetAreas = 'admin.presets.areas';
|
|
|
|
public const PresetTypes = 'admin.presets.types';
|
|
|
|
public const StatsTtl = 60;
|
|
|
|
public const OptionsTtl = 300;
|
|
|
|
public function remember(string $key, int $seconds, callable $callback): mixed
|
|
{
|
|
if (app()->runningUnitTests()) {
|
|
return $callback();
|
|
}
|
|
|
|
return Cache::memo()->remember($key, $seconds, $callback);
|
|
}
|
|
|
|
public function flush(): void
|
|
{
|
|
foreach ($this->keys() as $key) {
|
|
Cache::memo()->forget($key);
|
|
}
|
|
}
|
|
|
|
public function pressReleaseReviewCount(): int
|
|
{
|
|
return (int) $this->remember(self::PressReleaseReviewCount, self::StatsTtl, function (): int {
|
|
return PressRelease::query()
|
|
->withoutGlobalScopes()
|
|
->where('status', PressReleaseStatus::Review->value)
|
|
->count();
|
|
});
|
|
}
|
|
|
|
public function companiesStatsKey(?string $portal): string
|
|
{
|
|
return 'admin.companies.stats.'.($portal ?? 'all');
|
|
}
|
|
|
|
public function contactsStatsKey(?string $portal): string
|
|
{
|
|
return 'admin.contacts.stats.'.($portal ?? 'all');
|
|
}
|
|
|
|
public function categoriesStatsKey(?string $portal): string
|
|
{
|
|
return 'admin.categories.stats.'.($portal ?? 'all');
|
|
}
|
|
|
|
public function permissionGroupsKey(string $guard): string
|
|
{
|
|
return "admin.permissions.groups.{$guard}";
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
private function keys(): array
|
|
{
|
|
$portalKeys = collect([null, ...array_map(
|
|
static fn (Portal $portal): string => $portal->value,
|
|
Portal::cases(),
|
|
)]);
|
|
|
|
return [
|
|
self::DashboardStats,
|
|
self::PressReleaseStats,
|
|
self::PressReleaseReviewCount,
|
|
self::PressReleaseCategoryOptions,
|
|
self::ActiveCategoryOptions,
|
|
self::RoleOptions,
|
|
self::UserStats,
|
|
self::NewsletterStats,
|
|
self::PresetAreas,
|
|
self::PresetTypes,
|
|
$this->permissionGroupsKey('web'),
|
|
...$portalKeys->map(fn (?string $portal): string => $this->companiesStatsKey($portal))->all(),
|
|
...$portalKeys->map(fn (?string $portal): string => $this->contactsStatsKey($portal))->all(),
|
|
...$portalKeys->map(fn (?string $portal): string => $this->categoriesStatsKey($portal))->all(),
|
|
];
|
|
}
|
|
}
|