Files
Epicnabbo-Catalogus-Updated…/Updated_Cms/app/Services/SettingsService.php
T
Remco 7b9849c159 🆙 More fixes 🆙
2026-01-19 20:43:46 +01:00

59 lines
1.6 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
{
/** @var Collection<string, string> */
public private(set) Collection $settings;
public function __construct()
{
$this->refresh();
}
public function getOrDefault(string $settingName, ?string $default = null): string
{
return (string) $this->settings->get($settingName, $default);
}
public function refresh(): void
{
Cache::forget('website_settings');
try {
/** @var mixed $result */
$result = Cache::remember(
key: 'website_settings',
ttl: now()->addMinutes(5),
callback: fn () => Schema::hasTable('website_settings')
? WebsiteSetting::pluck('value', 'key')
: collect()
);
/** @var array<string, string> $data */
$data = [];
if ($result instanceof Collection) {
foreach ($result as $key => $value) {
$data[(string) $key] = is_scalar($value) ? (string) $value : '';
}
} elseif (is_array($result)) {
foreach ($result as $key => $value) {
$data[(string) $key] = is_scalar($value) ? (string) $value : '';
}
}
$this->settings = new Collection($data);
} catch (Throwable) {
$this->settings = new Collection();
}
}
}