66namespace Icinga \Module \Notifications \View ;
77
88use Icinga \Module \Notifications \Common \Icons ;
9+ use Icinga \Module \Notifications \Common \IncidentHistoryType ;
910use Icinga \Module \Notifications \Model \IncidentHistory ;
1011use Icinga \Module \Notifications \Widget \IconBall ;
1112use ipl \Html \Attributes ;
1718use ipl \I18n \Translation ;
1819use ipl \Web \Common \ItemRenderer ;
1920use ipl \Web \Widget \TimeAgo ;
21+ use UnhandledMatchError ;
2022
2123/** @implements ItemRenderer<IncidentHistory> */
2224class IncidentHistoryRenderer implements ItemRenderer
@@ -26,7 +28,7 @@ class IncidentHistoryRenderer implements ItemRenderer
2628 public function assembleAttributes ($ item , Attributes $ attributes , string $ layout ): void
2729 {
2830 $ classes = ['incident-history ' ];
29- if ($ item ->type === ' notified ' ) {
31+ if ($ item ->type === IncidentHistoryType:: NOTIFIED ) {
3032 $ classes [] = 'notification-state ' ;
3133 if ($ item ->notification_state === 'suppressed ' ) {
3234 $ classes [] = 'suppressed ' ;
@@ -40,20 +42,16 @@ public function assembleAttributes($item, Attributes $attributes, string $layout
4042
4143 public function assembleVisual ($ item , HtmlDocument $ visual , string $ layout ): void
4244 {
43- if ($ item ->type === ' incident_severity_changed ' ) {
45+ if ($ item ->type === IncidentHistoryType:: INCIDENT_SEVERITY_CHANGED ) {
4446 $ content = $ item ->new_severity ->getIcon ();
47+ } elseif ($ item ->type === IncidentHistoryType::RECIPIENT_ROLE_CHANGED ) {
48+ $ content = new IconBall ($ this ->getRoleIcon ($ item ));
4549 } else {
46- $ content = new IconBall (match ($ item ->type ) {
47- 'opened ' => Icons::OPENED ,
48- 'muted ' => Icons::MUTE ,
49- 'unmuted ' => Icons::UNMUTE ,
50- 'recipient_role_changed ' => $ this ->getRoleIcon ($ item ),
51- 'closed ' => Icons::CLOSED ,
52- 'rule_matched ' => Icons::RULE_MATCHED ,
53- 'escalation_triggered ' => Icons::TRIGGERED ,
54- 'notified ' => Icons::NOTIFIED ,
55- default => Icons::UNDEFINED
56- });
50+ try {
51+ $ content = Icons::forEnum ($ item ->type );
52+ } catch (UnhandledMatchError ) {
53+ $ content = new IconBall (Icons::UNDEFINED );
54+ }
5755 }
5856
5957 $ visual ->addHtml ($ content );
@@ -119,18 +117,18 @@ protected function getRoleIcon(IncidentHistory $item): string
119117 protected function buildMessage (IncidentHistory $ item ): ValidHtml
120118 {
121119 switch ($ item ->type ) {
122- case ' opened ' :
120+ case IncidentHistoryType:: OPENED :
123121 $ message = sprintf (
124122 $ this ->translate ('Incident opened at severity %s ' ),
125123 $ item ->new_severity ->getLabel ()
126124 );
127125
128126 break ;
129- case ' closed ' :
127+ case IncidentHistoryType:: CLOSED :
130128 $ message = $ this ->translate ('Incident closed ' );
131129
132130 break ;
133- case " notified " :
131+ case IncidentHistoryType:: NOTIFIED :
134132 if (isset ($ item ->contactgroup ->name ) && isset ($ item ->contact ->full_name )) {
135133 if (isset ($ item ->channel ->type )) {
136134 $ message = sprintf (
@@ -200,15 +198,15 @@ protected function buildMessage(IncidentHistory $item): ValidHtml
200198 }
201199
202200 break ;
203- case ' incident_severity_changed ' :
201+ case IncidentHistoryType:: INCIDENT_SEVERITY_CHANGED :
204202 $ message = sprintf (
205203 $ this ->translate ('Incident severity changed from %s to %s ' ),
206204 $ item ->old_severity ->getLabel (),
207205 $ item ->new_severity ->getLabel ()
208206 );
209207
210208 break ;
211- case ' recipient_role_changed ' :
209+ case IncidentHistoryType:: RECIPIENT_ROLE_CHANGED :
212210 $ newRole = $ item ->new_recipient_role ;
213211 $ message = '' ;
214212 if ($ newRole === 'manager ' || (! $ newRole && $ item ->old_recipient_role === 'manager ' )) {
@@ -251,16 +249,17 @@ protected function buildMessage(IncidentHistory $item): ValidHtml
251249 }
252250
253251 break ;
254- case ' rule_matched ' :
252+ case IncidentHistoryType:: RULE_MATCHED :
255253 if (isset ($ item ->rule ->name )) {
256254 $ message = sprintf ($ this ->translate ('Rule %s matched on this incident ' ), $ item ->rule ->name );
257255 } else {
258256 $ message = $ this ->translate ('Unknown rule matched on this incident ' );
259257 }
260258
261259 break ;
262- case ' escalation_triggered ' :
260+ case IncidentHistoryType:: ESCALATION_TRIGGERED :
263261 if (isset ($ item ->rule ->name )) {
262+ // TODO: No name is no reason to claim an unknown escalation, describe conditions instead
264263 if (isset ($ item ->rule_escalation ->name )) {
265264 $ message = sprintf (
266265 $ this ->translate ('Rule %s reached escalation %s ' ),
@@ -278,11 +277,11 @@ protected function buildMessage(IncidentHistory $item): ValidHtml
278277 }
279278
280279 break ;
281- case ' muted ' :
280+ case IncidentHistoryType:: MUTED :
282281 $ message = $ this ->translate ('Notifications for this incident have been muted ' );
283282
284283 break ;
285- case ' unmuted ' :
284+ case IncidentHistoryType:: UNMUTED :
286285 $ message = $ this ->translate ('Notifications for this incident have been unmuted ' );
287286
288287 break ;
0 commit comments