🆙 Fix settings service better en more stable 🆙

This commit is contained in:
Remco
2026-01-21 13:26:10 +01:00
parent 4e868e8912
commit 1776e75a6c
+52 -21
View File
@@ -1,5 +1,4 @@
<?php <?php
namespace App\Services; namespace App\Services;
use App\Models\Miscellaneous\WebsiteSetting; use App\Models\Miscellaneous\WebsiteSetting;
@@ -10,49 +9,81 @@ use Throwable;
class SettingsService class SettingsService
{ {
/** @var Collection<string, string> */ /**
* Store settings as string-to-string collection for type safety.
* @var Collection<string, string>
*/
public private(set) Collection $settings; public private(set) Collection $settings;
public function __construct() public function __construct()
{ {
$this->settings = new Collection();
$this->refresh(); $this->refresh();
} }
public function getOrDefault(string $settingName, ?string $default = null): string /**
* Get a setting value or return a default.
*/
public function getOrDefault(string $settingName, string $default = ''): string
{ {
return (string) $this->settings->get($settingName, $default); return $this->settings->get($settingName, $default);
} }
/**
* Refresh settings from database/cache.
*/
public function refresh(): void public function refresh(): void
{ {
Cache::forget('website_settings'); Cache::forget('website_settings');
try { try {
/** @var mixed $result */
$result = Cache::remember( $result = Cache::remember(
key: 'website_settings', key: 'website_settings',
ttl: now()->addMinutes(5), ttl: now()->addMinutes(5),
callback: fn () => Schema::hasTable('website_settings') callback: function () {
? WebsiteSetting::pluck('value', 'key') if (!Schema::hasTable('website_settings')) {
: collect() return collect([]);
}
return WebsiteSetting::pluck('value', 'key');
}
); );
/** @var array<string, string> $data */ $data = $this->normalizeSettings($result);
$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 : '';
}
}
/** @var Collection<string, string> */
$this->settings = new Collection($data); $this->settings = new Collection($data);
} catch (Throwable) { } catch (Throwable) {
$this->settings = new Collection(); /** @var Collection<string, string> */
$this->settings = new Collection([]);
} }
} }
/**
* Normalize settings data to ensure all keys and values are strings.
*
* @param Collection<array-key, mixed>|array<array-key, mixed> $result
* @return array<string, string>
*/
private function normalizeSettings(Collection|array $result): array
{
$data = [];
foreach ($result as $key => $value) {
// array-key is int|string, so we can safely cast to string
$safeKey = (string) $key;
// Ensure value is a string
if (is_scalar($value)) {
$safeValue = (string) $value;
} elseif (is_null($value)) {
$safeValue = '';
} else {
continue; // Skip non-scalar, non-null values
}
$data[$safeKey] = $safeValue;
}
return $data;
}
} }