You've already forked Epicnabbo-Catalogus-Updated-Daily
59 lines
1.6 KiB
PHP
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();
|
|
}
|
|
}
|
|
}
|