Refactor threshold error msg messages into helper

This commit is contained in:
Henry Whitaker
2020-08-21 23:14:28 +01:00
parent 375ecf650c
commit 6e712cbbdc
3 changed files with 77 additions and 68 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace App\Helpers;
class NotificationsHelper {
/**
* Parse $errors and format message
*
* @param array $errors
* @return String
*/
public static function formatPercentageThresholdMessage(array $errors)
{
$msg = NotificationsHelper::thresholdMessageStart($errors);
$msg = $msg . 'exceeded the percentage threshold';
return $msg;
}
/**
* Parse $errors and format message
*
* @param array $errors
* @return String
*/
public static function formatAbsoluteThresholdMessage(array $errors)
{
$msg = NotificationsHelper::thresholdMessageStart($errors);
$msg = $msg . 'exceeded the absolute threshold';
return $msg;
}
/**
* Iterate through errors to format message
*
* @param array $errors
* @return String
*/
public static function thresholdMessageStart(array $errors)
{
$msg = 'For the latest speedtest, the ';
for($i = 0; $i < sizeof($errors); $i++) {
$key = $errors[$i];
$msg = $msg . $key;
if(sizeof($errors) > 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;
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Notifications; namespace App\Notifications;
use App\Helpers\NotificationsHelper;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -44,43 +45,13 @@ class SpeedtestAbsoluteThresholdNotificationSlack extends Notification
*/ */
public function toSlack($notifiable) public function toSlack($notifiable)
{ {
$msg = NotificationsHelper::formatAbsoluteThresholdMessage($this->errors);
return (new SlackMessage) return (new SlackMessage)
->warning() ->warning()
->attachment(function ($attachment) { ->attachment(function ($attachment) use ($msg) {
$attachment->title('Speedtest absolute threshold error') $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;
}
} }

View File

@@ -2,6 +2,7 @@
namespace App\Notifications; namespace App\Notifications;
use App\Helpers\NotificationsHelper;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -44,43 +45,13 @@ class SpeedtestPercentageThresholdNotificationSlack extends Notification
*/ */
public function toSlack($notifiable) public function toSlack($notifiable)
{ {
$msg = NotificationsHelper::formatPercentageThresholdMessage($this->errors);
return (new SlackMessage) return (new SlackMessage)
->warning() ->warning()
->attachment(function ($attachment) { ->attachment(function ($attachment) use ($msg) {
$attachment->title('Speedtest percentage threshold error') $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;
}
} }