From 7ba8fb04cf308e147ef029462ffbc6490d6a1816 Mon Sep 17 00:00:00 2001 From: ulrik nielsen Date: Mon, 14 Dec 2015 13:54:37 +0100 Subject: [PATCH] cleanup --- .../Events/Incident/IncidentReported.php | 2 +- .../Events/Incident/IncidentUpdated.php | 13 +++------- src/Utils.php | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Handlers/Events/Incident/IncidentReported.php b/src/Handlers/Events/Incident/IncidentReported.php index 7f03c71..66c813b 100644 --- a/src/Handlers/Events/Incident/IncidentReported.php +++ b/src/Handlers/Events/Incident/IncidentReported.php @@ -38,7 +38,7 @@ public function handle(IncidentWasReportedEvent $event) $attachment = [ 'fallback' => trans('slack::messages.incident.created.fallback', $replacements), - 'color' => 'danger', + 'color' => Utils::statusToColor($event->incident->status), 'title' => trans('slack::messages.incident.created.title', $replacements), 'title_link' => url('status-page'), 'text' => $event->incident->message, diff --git a/src/Handlers/Events/Incident/IncidentUpdated.php b/src/Handlers/Events/Incident/IncidentUpdated.php index e4bbe2c..ec895df 100644 --- a/src/Handlers/Events/Incident/IncidentUpdated.php +++ b/src/Handlers/Events/Incident/IncidentUpdated.php @@ -41,14 +41,9 @@ public function handle(IncidentWasUpdatedEvent $event) $statuses = trans('cachet.incidents.status'); $closed = max(array_keys($statuses)); - $color = 'danger'; - $state = 'updated'; - if ($newStatus == $closed) { - $color = 'good'; - $state = 'closed'; - } elseif (in_array($newStatus, [2, 3])) { - $color = 'warning'; - } + $state = $newStatus == $closed + ? 'closed' + : 'updated'; $replacements = [ 'id' => $event->incident->id, @@ -65,7 +60,7 @@ public function handle(IncidentWasUpdatedEvent $event) $attachment = [ 'fallback' => trans('slack::messages.incident.updated.fallback', $replacements), - 'color' => $color, + 'color' => Utils::statusToColor($newStatus), 'title' => trans('slack::messages.incident.updated.title', $replacements), 'title_link' => url('status-page'), 'text' => $message, diff --git a/src/Utils.php b/src/Utils.php index 67c0070..9cd5d89 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -54,6 +54,8 @@ public static function getChanges($model) } /** + * Find the status on a component. + * * @param string $componentId * * @return string @@ -73,4 +75,28 @@ public static function getComponentStatus($componentId = '') return 'n/a'; } + + /** + * Map status ids to a slack color. + * + * @param int $status + * + * @return string + */ + public static function statusToColor($status) + { + $colormap = [ + 0 => 'good', // 'Scheduled' + 1 => 'danger', // 'Investigating' + 2 => 'warning', // 'Identified' + 3 => 'warning', // 'Watching' + 4 => 'good', // 'Fixed' + ]; + + if (isset($colormap[$status])) { + return $colormap[$status]; + } + + return ''; + } }