fromServer('services.shinkan.url') ?? ($this->stored()['url'] ?? null); return filled($value) ? rtrim(trim((string) $value), '/') : null; } public function apiKey(): ?string { $server = $this->fromServer('services.shinkan.key'); if ($server !== null) { return $server; } $encrypted = $this->stored()['key'] ?? null; if (! is_string($encrypted) || $encrypted === '') { return null; } try { return Crypt::decryptString($encrypted); } catch (DecryptException) { return null; } } /** * Woher kommt der Wert? `server`, `verwaltung` oder null. */ public function source(string $field): ?string { $config = $field === 'url' ? 'services.shinkan.url' : 'services.shinkan.key'; if ($this->fromServer($config) !== null) { return self::SERVER; } return filled($this->stored()[$field] ?? null) && ($field === 'url' || $this->apiKey() !== null) ? self::ADMIN : null; } public function configured(): bool { return $this->url() !== null && $this->apiKey() !== null; } /** * Speichert die in der Verwaltung gepflegten Werte. Ein leerer Schlüssel lässt den gespeicherten unverändert. * Werte aus der Umgebung des Servers bleiben davon unberührt und behalten den Vorrang. */ public function save(?string $url, ?string $key, ?User $by = null): void { $stored = $this->stored(); $url = trim((string) $url); if ($url !== '') { $parts = parse_url($url); if ($parts === false || ! in_array($parts['scheme'] ?? '', ['http', 'https'], true) || empty($parts['host'])) { throw new DomainRuleException('Die Adresse ist keine gültige Internetadresse. Sie beginnt mit https:// (oder http://).'); } $stored['url'] = rtrim($url, '/'); } else { unset($stored['url']); } $key = trim((string) $key); if ($key !== '') { $stored['key'] = Crypt::encryptString($key); } $this->write($stored, $by); } public function forgetKey(?User $by = null): void { $stored = $this->stored(); unset($stored['key']); $this->write($stored, $by); } /** * @return array */ private function stored(): array { $value = SiteSetting::query()->find(self::SETTING)?->value; return is_array($value) ? $value : []; } /** * @param array $stored */ private function write(array $stored, ?User $by): void { SiteSetting::query()->updateOrCreate(['key' => self::SETTING], ['value' => $stored, 'updated_by' => $by?->id]); } private function fromServer(string $config): ?string { $value = config($config); return is_string($value) && trim($value) !== '' ? trim($value) : null; } }