|null */ private ?array $data = null; public function __construct(private readonly ?string $file = null) {} public function email(): ?string { return $this->read()['email'] ?? null; } public function key(): ?string { $encrypted = $this->read()['key'] ?? null; if (! is_string($encrypted) || $encrypted === '') { return null; } try { return Crypt::decryptString($encrypted); } catch (Throwable) { // APP_KEY hat sich geändert – Schlüssel muss neu eingegeben werden. return null; } } public function hasLicense(): bool { return filled($this->email()) && filled($this->key()); } public function setLicense(string $email, string $key): void { $this->write([ 'email' => trim($email), 'key' => Crypt::encryptString(trim($key)), ]); } public function clearLicense(): void { $this->write(['email' => null, 'key' => null, 'device_token' => null]); } public function deviceId(): string { $id = $this->read()['device_id'] ?? null; if (! is_string($id) || ! preg_match('/^[0-9a-f]{32}$/', $id)) { $id = bin2hex(random_bytes(16)); $this->write(['device_id' => $id]); } return $id; } public function deviceToken(): ?string { $token = $this->read()['device_token'] ?? null; return is_string($token) && $token !== '' ? $token : null; } /** * Neues Gerätetoken sofort speichern – auch wenn die Antwort ein Fehler war. */ public function saveDeviceToken(string $token): void { if ($token !== '' && $token !== $this->deviceToken()) { $this->write(['device_token' => $token]); } } public function hostname(): string { return (string) (gethostname() ?: 'unbekannt'); } /** * „web01:/var/www/kunde“ – damit der Kunde im Portal erkennt, welcher Geräteplatz welcher ist. */ public function deviceLabel(): string { return $this->hostname().':'.base_path(); } /** * Letztes Ergebnis der Lizenzprüfung (für die Anzeige ohne erneute Anfrage). * * @param array|null $info */ public function rememberStatus(?array $info, ?string $errorCode = null): void { $this->write(['last_status' => [ 'info' => $info, 'error_code' => $errorCode, 'checked_at' => time(), ]]); } /** * @return array{info: array|null, error_code: string|null, checked_at: int}|null */ public function lastStatus(): ?array { $status = $this->read()['last_status'] ?? null; return is_array($status) ? $status : null; } /** * @return array */ private function read(): array { if ($this->data === null) { $decoded = json_decode((string) @file_get_contents($this->path()), true); $this->data = is_array($decoded) ? $decoded : []; } return $this->data; } /** * @param array $changes */ private function write(array $changes): void { // Immer frisch von der Platte lesen: parallele Anfragen können das Token rotiert haben. $this->data = null; $data = array_merge($this->read(), $changes); AtomicFile::write($this->path(), json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); @chmod($this->path(), 0600); $this->data = $data; } private function path(): string { return $this->file ?? storage_path('app/license.json'); } }