Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 68 additions & 18 deletions core/Listener/FeedBackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,90 @@
use OCP\IEventSource;
use OCP\IL10N;

/**
* Handles repair feedback events and sends localized progress, info, warnings,
* and error messages to the UI/event source during system repair operations.
*/
class FeedBackHandler {
private int $progressStateMax = 100;
private int $progressStateStep = 0;
private string $currentStep = '';
private int $totalSteps = 100;
private int $completedSteps = 0;
private string $currentStepName = '';

public function __construct(
private IEventSource $eventSource,
private IL10N $l10n,
) {
}

/**
* Handles feedback events and dispatches localized messages.
*/
public function handleRepairFeedback(Event $event): void {
if ($event instanceof RepairStartEvent) {
$this->progressStateMax = $event->getMaxStep();
$this->progressStateStep = 0;
$this->currentStep = $event->getCurrentStepName();
$this->onStart($event);
} elseif ($event instanceof RepairAdvanceEvent) {
$this->progressStateStep += $event->getIncrement();
$desc = $event->getDescription();
if (empty($desc)) {
$desc = $this->currentStep;
}
$this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc]));
$this->onAdvance($event);
} elseif ($event instanceof RepairFinishEvent) {
$this->progressStateMax = $this->progressStateStep;
$this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
$this->onFinish($event);
} elseif ($event instanceof RepairStepEvent) {
$this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getStepName());
$this->onStep($event);
} elseif ($event instanceof RepairInfoEvent) {
$this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getMessage());
$this->onInfo($event);
} elseif ($event instanceof RepairWarningEvent) {
$this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage());
$this->onWarning($event);
} elseif ($event instanceof RepairErrorEvent) {
$this->eventSource->send('error', $this->l10n->t('Repair error:') . ' ' . $event->getMessage());
$this->onError($event);
}
// TODO: handle unknown event types
// e.g., log or $this->eventSource->send('notice', $this->l10n->t('Unknown repair event type: %s', [get_class($event)]));

}

private function onStart(RepairStartEvent $event): void {
$this->totalSteps = $event->getMaxStep();
$this->completedSteps = 0;
$this->currentStepName = $event->getCurrentStepName();
}

private function onAdvance(RepairAdvanceEvent $event): void {
$this->completedSteps += $event->getIncrement();
$description = trim($event->getDescription()) ?: $this->currentStepName;
$message = $this->l10n->t(
'[%d / %d]: %s',
[$this->completedSteps, $this->totalSteps, $description]
);
$this->eventSource->send('success', $message);
}

private function onFinish(RepairFinishEvent $event): void {
$message = $this->l10n->t(
'[%d / %d]: %s',
[$this->completedSteps, $this->totalSteps, $this->currentStepName]
);
$this->eventSource->send('success', $message);
}

private function onStep(RepairStepEvent $event): void {
$stepName = trim($event->getStepName());
$message = $this->l10n->t('Repair step:') . ' ' . $stepName;
$this->eventSource->send('success', $message);
}

private function onInfo(RepairInfoEvent $event): void {
$info = trim($event->getMessage());
$message = $this->l10n->t('Repair info:') . ' ' . $info;
$this->eventSource->send('success', $message);
}

private function onWarning(RepairWarningEvent $event): void {
$warning = trim($event->getMessage());
$message = $this->l10n->t('Repair warning:') . ' ' . $warning;
$this->eventSource->send('notice', $message);
}

private function onError(RepairErrorEvent $event): void {
$error = trim($event->getMessage());
$message = $this->l10n->t('Repair error:') . ' ' . $error;
$this->eventSource->send('error', $message);
}
}
Loading