Files
Epicnabbo-Catalogus-Updated…/Updated_Cms/app/Services/SettingsService.php
T
Remco 521f9c884c 🆙 More fixes 🆙
2026-01-19 17:32:44 +01:00

56 lines
1.5 KiB
PHP

<?php
namespace App\Services;
use App\Models\Miscellaneous\WebsiteSetting;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Throwable;
class SettingsService
{
public private(set) ?Collection $settings;
public function __construct()
{
try {
$this->settings = Cache::remember(
key: 'website_settings',
ttl: now()->addMinutes(5),
callback: fn () => Schema::hasTable('website_settings')
? WebsiteSetting::all()->pluck('value', 'key')
: collect()
);
} catch (Throwable) {
$this->settings = collect();
}
}
public function getOrDefault(string $settingName, ?string $default = null): string
{
if (! $this->settings instanceof Collection) {
return (string) $default;
}
return (string) $this->settings->get($settingName, $default);
}
public function refresh(): void
{
Cache::forget('website_settings');
try {
$this->settings = Cache::remember(
key: 'website_settings',
ttl: now()->addMinutes(5),
callback: fn () => Schema::hasTable('website_settings')
? WebsiteSetting::all()->pluck('value', 'key')
: collect()
);
} catch (Throwable) {
$this->settings = collect();
}
}
}