Files
mareike/app/Repositories/GlobalSettingRepository.php
T
2026-09-29 14:35:33 +02:00

36 lines
1.1 KiB
PHP

<?php
namespace App\Repositories;
use App\Models\GlobalSetting;
/**
* Zugriff auf die app-weiten Einstellungen. Im Container als `scoped` registriert -- über
* `app(GlobalSettingRepository::class)` beziehen, dann werden die Werte nur einmal pro Request gelesen
* (die Bezeichnungen werden je Teili-Zeile einer Liste gebraucht).
*/
class GlobalSettingRepository {
/** @var array<string, string>|null */
private ?array $values = null;
public function get(string $key, string $default) : string {
$value = $this->all()[$key] ?? null;
return ($value === null || trim($value) === '') ? $default : $value;
}
/** @param array<string, string> $values */
public function setMany(array $values) : void {
foreach ($values as $key => $value) {
GlobalSetting::updateOrCreate(['key' => $key], ['value' => $value]);
}
$this->values = null;
}
/** @return array<string, string> */
private function all() : array {
return $this->values ??= GlobalSetting::pluck('value', 'key')->all();
}
}