136 lines
5.0 KiB
PHP
136 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// SettingService — Servizio per le impostazioni dinamiche
|
|
//
|
|
// Questo service è il punto unico di accesso alle impostazioni.
|
|
// Gestisce:
|
|
// 1. Lettura con fallback ai default di config/settings.php
|
|
// 2. Cache Redis per performance (evita query ad ogni richiesta)
|
|
// 3. Cast dei valori al tipo corretto (string → int, "1" → true, ecc.)
|
|
// 4. Scrittura e invalidazione cache
|
|
//
|
|
// DEPENDENCY INJECTION:
|
|
// Laravel inietta automaticamente questo service quando lo dichiari
|
|
// nel costruttore di un Controller:
|
|
//
|
|
// public function __construct(private SettingService $settings) {}
|
|
//
|
|
// Poi lo usi con: $this->settings->get('items_per_page')
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
use App\Models\Setting;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class SettingService
|
|
{
|
|
private const CACHE_KEY = 'app_settings_all';
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
// ─── Leggi un'impostazione ─────────────────────────────────────────────
|
|
// $key → chiave dell'impostazione (es. 'items_per_page')
|
|
// $default → valore di ritorno se la chiave non esiste
|
|
public function get(string $key, mixed $default = null): mixed
|
|
{
|
|
$all = $this->all();
|
|
|
|
if (isset($all[$key])) {
|
|
return $all[$key];
|
|
}
|
|
|
|
// Prova i default di config/settings.php
|
|
$configDefault = config("settings.defaults.{$key}");
|
|
|
|
return $configDefault ?? $default;
|
|
}
|
|
|
|
// ─── Leggi tutte le impostazioni (con cache) ─────────────────────────
|
|
public function all(): array
|
|
{
|
|
$ttl = config('settings.cache_ttl_minutes', 60) * 60;
|
|
|
|
// Cache::remember: se la chiave è in cache, la restituisce;
|
|
// altrimenti esegue la closure, salva il risultato e lo restituisce.
|
|
return Cache::remember(self::CACHE_KEY, $ttl, function () {
|
|
return $this->loadFromDatabase();
|
|
});
|
|
}
|
|
|
|
// ─── Scrivi un'impostazione ───────────────────────────────────────────
|
|
public function set(string $key, mixed $value): void
|
|
{
|
|
Setting::updateOrCreate(
|
|
['key' => $key],
|
|
['value' => $this->serialize($value)]
|
|
);
|
|
|
|
// Invalida la cache: al prossimo accesso verrà riletta dal DB
|
|
$this->clearCache();
|
|
}
|
|
|
|
// ─── Scrivi più impostazioni in una volta ─────────────────────────────
|
|
public function setMany(array $settings): void
|
|
{
|
|
foreach ($settings as $key => $value) {
|
|
Setting::updateOrCreate(
|
|
['key' => $key],
|
|
['value' => $this->serialize($value)]
|
|
);
|
|
}
|
|
|
|
$this->clearCache();
|
|
}
|
|
|
|
// ─── Svuota la cache delle impostazioni ───────────────────────────────
|
|
public function clearCache(): void
|
|
{
|
|
Cache::forget(self::CACHE_KEY);
|
|
}
|
|
|
|
// ─── Leggi dal database e applica i cast ──────────────────────────────
|
|
private function loadFromDatabase(): array
|
|
{
|
|
$types = config('settings.types', []);
|
|
$result = [];
|
|
|
|
foreach (Setting::all() as $setting) {
|
|
$type = $types[$setting->key] ?? 'string';
|
|
$result[$setting->key] = $this->cast($setting->value, $type);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
// ─── Converti il valore al tipo corretto ──────────────────────────────
|
|
// Necessario perché nel DB tutto è stringa VARCHAR.
|
|
private function cast(mixed $value, string $type): mixed
|
|
{
|
|
return match ($type) {
|
|
'integer' => (int) $value,
|
|
'boolean' => filter_var($value, FILTER_VALIDATE_BOOLEAN),
|
|
'float' => (float) $value,
|
|
'json' => json_decode($value, true),
|
|
default => (string) $value, // 'string', 'text'
|
|
};
|
|
}
|
|
|
|
// ─── Serializza per il salvataggio in DB ──────────────────────────────
|
|
private function serialize(mixed $value): string
|
|
{
|
|
if (is_bool($value)) {
|
|
return $value ? '1' : '0';
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
return json_encode($value);
|
|
}
|
|
|
|
return (string) $value;
|
|
}
|
|
}
|