From 6e712cbbdc5297fae132d78d320d870621538fce Mon Sep 17 00:00:00 2001 From: Henry Whitaker Date: Fri, 21 Aug 2020 23:14:28 +0100 Subject: [PATCH] Refactor threshold error msg messages into helper --- app/Helpers/NotificationsHelper.php | 67 +++++++++++++++++++ ...testAbsoluteThresholdNotificationSlack.php | 39 ++--------- ...stPercentageThresholdNotificationSlack.php | 39 ++--------- 3 files changed, 77 insertions(+), 68 deletions(-) create mode 100644 app/Helpers/NotificationsHelper.php diff --git a/app/Helpers/NotificationsHelper.php b/app/Helpers/NotificationsHelper.php new file mode 100644 index 00000000..d110299a --- /dev/null +++ b/app/Helpers/NotificationsHelper.php @@ -0,0 +1,67 @@ + 1 && $i < (sizeof($errors) - 1)) { + $msg = $msg . ', '; + } + } + + if($msg[-1] != '') { + $msg = $msg . ' '; + } + + if(sizeof($errors) > 1) { + $msg = $msg . 'values '; + } else { + $msg = $msg . 'value '; + } + + return $msg; + } +} diff --git a/app/Notifications/SpeedtestAbsoluteThresholdNotificationSlack.php b/app/Notifications/SpeedtestAbsoluteThresholdNotificationSlack.php index 2965e0d3..1f4d0fb8 100644 --- a/app/Notifications/SpeedtestAbsoluteThresholdNotificationSlack.php +++ b/app/Notifications/SpeedtestAbsoluteThresholdNotificationSlack.php @@ -2,6 +2,7 @@ namespace App\Notifications; +use App\Helpers\NotificationsHelper; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -44,43 +45,13 @@ class SpeedtestAbsoluteThresholdNotificationSlack extends Notification */ public function toSlack($notifiable) { + $msg = NotificationsHelper::formatAbsoluteThresholdMessage($this->errors); + return (new SlackMessage) ->warning() - ->attachment(function ($attachment) { + ->attachment(function ($attachment) use ($msg) { $attachment->title('Speedtest absolute threshold error') - ->content($this->formatMessage()); + ->content($msg); }); } - - /** - * Parse $this->errors and format message - * - * @return String - */ - public function formatMessage() - { - $msg = 'For the latest speedtest, the '; - - for($i = 0; $i < sizeof($this->errors); $i++) { - $key = $this->errors[$i]; - $msg = $msg . $key; - if(sizeof($this->errors) > 1 && $i < (sizeof($this->errors) - 1)) { - $msg = $msg . ', '; - } - } - - if($msg[-1] != '') { - $msg = $msg . ' '; - } - - if(sizeof($this->errors) > 1) { - $msg = $msg . 'values '; - } else { - $msg = $msg . 'value '; - } - - $msg = $msg . 'exceeded the absolute threshold'; - - return $msg; - } } diff --git a/app/Notifications/SpeedtestPercentageThresholdNotificationSlack.php b/app/Notifications/SpeedtestPercentageThresholdNotificationSlack.php index f86b7413..fd9332ba 100644 --- a/app/Notifications/SpeedtestPercentageThresholdNotificationSlack.php +++ b/app/Notifications/SpeedtestPercentageThresholdNotificationSlack.php @@ -2,6 +2,7 @@ namespace App\Notifications; +use App\Helpers\NotificationsHelper; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -44,43 +45,13 @@ class SpeedtestPercentageThresholdNotificationSlack extends Notification */ public function toSlack($notifiable) { + $msg = NotificationsHelper::formatPercentageThresholdMessage($this->errors); + return (new SlackMessage) ->warning() - ->attachment(function ($attachment) { + ->attachment(function ($attachment) use ($msg) { $attachment->title('Speedtest percentage threshold error') - ->content($this->formatMessage()); + ->content($msg); }); } - - /** - * Parse $this->errors and format message - * - * @return String - */ - public function formatMessage() - { - $msg = 'For the latest speedtest, the '; - - for($i = 0; $i < sizeof($this->errors); $i++) { - $key = $this->errors[$i]; - $msg = $msg . $key; - if(sizeof($this->errors) > 1 && $i < (sizeof($this->errors) - 1)) { - $msg = $msg . ', '; - } - } - - if($msg[-1] != '') { - $msg = $msg . ' '; - } - - if(sizeof($this->errors) > 1) { - $msg = $msg . 'values '; - } else { - $msg = $msg . 'value '; - } - - $msg = $msg . 'exceeded the percentage threshold'; - - return $msg; - } }