- Rückmeldungen an den Hersteller (docs/specs/feedback.md): Fehlerbericht und
Funktionswunsch, nur freigeschaltete Wege, Idempotency-Key je Meldung,
Warteschlange mit höchstens fünf Versuchen, 422 am Feld, 429 mit Wartezeit,
404 blendet den Weg aus, Referenznummer und „Meine Rückmeldungen“,
sichtbare technische Angaben und Datenschutzhinweis über dem Absendeknopf
- Hintergrundaufgaben ohne Dauerprozess: Cron, Web-Cron (/cron/{token}) oder
Pseudo-Cron nach dem Seitenaufruf; Queue in kurzen Häppchen
- Seite „Systemzustand“ mit Voraussetzungen, Grenzen, Betrieb, Sicherheit und
Anleitung für den Cronjob
- Web-Installer (Systemprüfung → Datenbank → Administrator), legt .env und
Anwendungsschlüssel selbst an, arbeitet vor der Einrichtung ohne Datenbank
- Datenbank-Benachrichtigungen im Admin-Bereich
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
80 lines
3 KiB
PHP
80 lines
3 KiB
PHP
<?php
|
||
|
||
namespace App\Support\Feedback;
|
||
|
||
use App\Support\License\ServerError;
|
||
use App\Support\License\VendorHttp;
|
||
|
||
/**
|
||
* Sendet Rückmeldungen an api.loheide.cloud (docs/specs/feedback.md).
|
||
* Lizenz-Kopfzeilen nur, wenn eine Lizenz hinterlegt ist; eine ungültige oder
|
||
* abgelaufene Lizenz verhindert das Senden nicht.
|
||
*/
|
||
class FeedbackClient
|
||
{
|
||
public function __construct(private readonly VendorHttp $http) {}
|
||
|
||
/**
|
||
* @param array<string, mixed> $payload
|
||
*/
|
||
public function send(string $type, array $payload, string $idempotencyKey, int $timeout = 10): FeedbackResult
|
||
{
|
||
try {
|
||
$response = VendorHttp::guard(fn () => $this->http->request($timeout, identifyDevice: false)
|
||
->withHeaders(['Idempotency-Key' => $idempotencyKey])
|
||
->post($this->url($type), $payload));
|
||
} catch (ServerError $e) {
|
||
return FeedbackResult::retry('Der Server ist nicht erreichbar.');
|
||
}
|
||
|
||
$data = $response->json() ?? [];
|
||
|
||
return match (true) {
|
||
$response->status() === 201 || ($response->successful() && isset($data['reference'])) => FeedbackResult::sent((string) ($data['reference'] ?? '')),
|
||
$response->status() === 404 => FeedbackResult::disabled(),
|
||
$response->status() === 422 => FeedbackResult::invalid($this->fieldErrors($data), $data['error'] ?? $data['message'] ?? null),
|
||
$response->status() === 429 => FeedbackResult::rateLimited($this->retryAfter($response->header('Retry-After'), $data)),
|
||
// 409: dieselbe Meldung wird gerade verarbeitet – kurz warten, dann erneut (kein Fehler).
|
||
$response->status() === 409 => FeedbackResult::retry('Wird bereits verarbeitet.', 30),
|
||
default => FeedbackResult::retry('Server antwortet mit HTTP '.$response->status().'.', $this->retryAfter($response->header('Retry-After'), $data)),
|
||
};
|
||
}
|
||
|
||
private function url(string $type): string
|
||
{
|
||
return rtrim(config('updater.api_url'), '/').'/software/'.config('updater.software').'/'.$type;
|
||
}
|
||
|
||
/**
|
||
* Unterstützt {"errors": {"feld": ["Grund"]}} und {"field": "feld", "error": "Grund"}.
|
||
*
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, string>
|
||
*/
|
||
private function fieldErrors(array $data): array
|
||
{
|
||
$errors = [];
|
||
|
||
if (is_array($data['errors'] ?? null)) {
|
||
foreach ($data['errors'] as $field => $messages) {
|
||
$errors[(string) $field] = is_array($messages) ? (string) reset($messages) : (string) $messages;
|
||
}
|
||
}
|
||
|
||
if (is_string($data['field'] ?? null)) {
|
||
$errors[$data['field']] = (string) ($data['error'] ?? $data['message'] ?? 'Ungültige Eingabe.');
|
||
}
|
||
|
||
return $errors;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
*/
|
||
private function retryAfter(?string $header, array $data): ?int
|
||
{
|
||
$value = $data['retry_after'] ?? $header;
|
||
|
||
return is_numeric($value) ? (int) $value : null;
|
||
}
|
||
}
|