|
21 | 21 | use OCP\IEventSource; |
22 | 22 | use OCP\IL10N; |
23 | 23 |
|
| 24 | +/** |
| 25 | + * Handles repair feedback events and sends localized progress, info, warnings, |
| 26 | + * and error messages to the UI/event source during system repair operations. |
| 27 | + */ |
24 | 28 | class FeedBackHandler { |
25 | | - private int $progressStateMax = 100; |
26 | | - private int $progressStateStep = 0; |
27 | | - private string $currentStep = ''; |
| 29 | + private int $totalSteps = 100; |
| 30 | + private int $completedSteps = 0; |
| 31 | + private string $currentStepName = ''; |
28 | 32 |
|
29 | 33 | public function __construct( |
30 | 34 | private IEventSource $eventSource, |
31 | 35 | private IL10N $l10n, |
32 | 36 | ) { |
33 | 37 | } |
34 | 38 |
|
| 39 | + /** |
| 40 | + * Handles feedback events and dispatches localized messages. |
| 41 | + */ |
35 | 42 | public function handleRepairFeedback(Event $event): void { |
36 | 43 | if ($event instanceof RepairStartEvent) { |
37 | | - $this->progressStateMax = $event->getMaxStep(); |
38 | | - $this->progressStateStep = 0; |
39 | | - $this->currentStep = $event->getCurrentStepName(); |
| 44 | + $this->onStart($event); |
40 | 45 | } elseif ($event instanceof RepairAdvanceEvent) { |
41 | | - $this->progressStateStep += $event->getIncrement(); |
42 | | - $desc = $event->getDescription(); |
43 | | - if (empty($desc)) { |
44 | | - $desc = $this->currentStep; |
45 | | - } |
46 | | - $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc])); |
| 46 | + $this->onAdvance($event); |
47 | 47 | } elseif ($event instanceof RepairFinishEvent) { |
48 | | - $this->progressStateMax = $this->progressStateStep; |
49 | | - $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep])); |
| 48 | + $this->onFinish($event); |
50 | 49 | } elseif ($event instanceof RepairStepEvent) { |
51 | | - $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getStepName()); |
| 50 | + $this->onStep($event); |
52 | 51 | } elseif ($event instanceof RepairInfoEvent) { |
53 | | - $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getMessage()); |
| 52 | + $this->onInfo($event); |
54 | 53 | } elseif ($event instanceof RepairWarningEvent) { |
55 | | - $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage()); |
| 54 | + $this->onWarning($event); |
56 | 55 | } elseif ($event instanceof RepairErrorEvent) { |
57 | | - $this->eventSource->send('error', $this->l10n->t('Repair error:') . ' ' . $event->getMessage()); |
| 56 | + $this->onError($event); |
58 | 57 | } |
| 58 | + // TODO: handle unknown event types |
| 59 | + // e.g., log or $this->eventSource->send('notice', $this->l10n->t('Unknown repair event type: %s', [get_class($event)])); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private function onStart(RepairStartEvent $event): void { |
| 64 | + $this->totalSteps = $event->getMaxStep(); |
| 65 | + $this->completedSteps = 0; |
| 66 | + $this->currentStepName = (string)$event->getCurrentStepName(); |
| 67 | + } |
| 68 | + |
| 69 | + private function onAdvance(RepairAdvanceEvent $event): void { |
| 70 | + $this->completedSteps += $event->getIncrement(); |
| 71 | + $description = trim((string)$event->getDescription()) ?: $this->currentStepName; |
| 72 | + $message = $this->l10n->t( |
| 73 | + '[%d / %d]: %s', |
| 74 | + [$this->completedSteps, $this->totalSteps, $description] |
| 75 | + ); |
| 76 | + $this->eventSource->send('success', (string)$message); |
| 77 | + } |
| 78 | + |
| 79 | + private function onFinish(RepairFinishEvent $event): void { |
| 80 | + $message = $this->l10n->t( |
| 81 | + '[%d / %d]: %s', |
| 82 | + [$this->completedSteps, $this->totalSteps, $this->currentStepName] |
| 83 | + ); |
| 84 | + $this->eventSource->send('success', (string)$message); |
| 85 | + } |
| 86 | + |
| 87 | + private function onStep(RepairStepEvent $event): void { |
| 88 | + $stepName = trim((string)$event->getStepName()); |
| 89 | + $message = $this->l10n->t('Repair step:') . ' ' . $stepName; |
| 90 | + $this->eventSource->send('success', (string)$message); |
| 91 | + } |
| 92 | + |
| 93 | + private function onInfo(RepairInfoEvent $event): void { |
| 94 | + $info = trim((string)$event->getMessage()); |
| 95 | + $message = $this->l10n->t('Repair info:') . ' ' . $info; |
| 96 | + $this->eventSource->send('success', (string)$message); |
| 97 | + } |
| 98 | + |
| 99 | + private function onWarning(RepairWarningEvent $event): void { |
| 100 | + $warning = trim((string)$event->getMessage()); |
| 101 | + $message = $this->l10n->t('Repair warning:') . ' ' . $warning; |
| 102 | + $this->eventSource->send('notice', (string)$message); |
| 103 | + } |
| 104 | + |
| 105 | + private function onError(RepairErrorEvent $event): void { |
| 106 | + $error = trim((string)$event->getMessage()); |
| 107 | + $message = $this->l10n->t('Repair error:') . ' ' . $error; |
| 108 | + $this->eventSource->send('error', (string)$message); |
59 | 109 | } |
60 | 110 | } |
0 commit comments