🆙 More fixes 🆙

This commit is contained in:
Remco
2026-01-19 20:43:46 +01:00
parent deed2158ca
commit 7b9849c159
77 changed files with 1084 additions and 13612 deletions
+24 -21
View File
@@ -10,46 +10,49 @@ use Throwable;
class SettingsService
{
public private(set) ?Collection $settings;
/** @var Collection<string, string> */
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();
}
$this->refresh();
}
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(
/** @var mixed $result */
$result = Cache::remember(
key: 'website_settings',
ttl: now()->addMinutes(5),
callback: fn () => Schema::hasTable('website_settings')
? WebsiteSetting::all()->pluck('value', 'key')
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 = collect();
$this->settings = new Collection();
}
}
}