Loheide-eu-mitarbeiterplatt.../plugins-core/it-service/src/OpenNit/Settings.php
Friederich Loheide 7969a7ac83
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m35s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m32s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m53s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m1s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m17s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m31s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m11s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m33s
IT-Service: /api/ex ergänzen, HTML-Antworten als Fehler, Kategorie und Standort Pflicht
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:17:39 +00:00

108 lines
2.8 KiB
PHP

<?php
namespace Inhaus\Plugins\ItService\OpenNit;
use App\Support\Settings as PortalSettings;
use Illuminate\Support\Facades\Crypt;
use Throwable;
/**
* Zugangsdaten zu OpenNIT. Token und Signing-Secret liegen verschlüsselt in den Einstellungen.
*/
class Settings
{
public const URL = 'it_service.url';
public const TOKEN = 'it_service.token';
public const SECRET = 'it_service.secret';
/** null = Einstellung in OpenNIT, true = sofort reservieren, false = immer Anfrage */
public const AUTO_RESERVE = 'it_service.auto_reserve';
public const INTRO = 'it_service.intro';
public function __construct(private readonly PortalSettings $settings) {}
public function url(): string
{
return self::normalizeUrl((string) $this->settings->get(self::URL, ''));
}
/**
* Nur die Adresse des Servers eingetragen? Dann gehört „/api/ex“ dazu.
*/
public static function normalizeUrl(string $url): string
{
$url = rtrim(trim($url), '/');
if ($url !== '' && in_array((string) parse_url($url, PHP_URL_PATH), ['', '/'], true)) {
$url .= '/api/ex';
}
return $url;
}
public function token(): string
{
return $this->decrypt(self::TOKEN);
}
public function secret(): string
{
return $this->decrypt(self::SECRET);
}
public function configured(): bool
{
return $this->url() !== '' && $this->token() !== '' && $this->secret() !== '';
}
public function autoReserve(): ?bool
{
$value = $this->settings->get(self::AUTO_RESERVE);
return $value === null ? null : (bool) $value;
}
public function intro(): ?string
{
return $this->settings->get(self::INTRO) ?: null;
}
/**
* @param array{url: string, token?: ?string, secret?: ?string, auto_reserve: ?string, intro: ?string} $data leere Token/Secret = unverändert
*/
public function save(array $data): void
{
$this->settings->set(self::URL, self::normalizeUrl($data['url']));
foreach ([self::TOKEN => $data['token'] ?? null, self::SECRET => $data['secret'] ?? null] as $key => $value) {
if (filled($value)) {
$this->settings->set($key, Crypt::encryptString(trim($value)));
}
}
$this->settings->set(self::AUTO_RESERVE, match ($data['auto_reserve'] ?? '') {
'yes' => true,
'no' => false,
default => null,
});
$this->settings->set(self::INTRO, $data['intro'] ?? null);
}
private function decrypt(string $key): string
{
$value = $this->settings->get($key);
if (! is_string($value) || $value === '') {
return '';
}
try {
return Crypt::decryptString($value);
} catch (Throwable) {
return '';
}
}
}