mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-21 13:23:04 +01:00
Refactor threshold error msg messages into helper
This commit is contained in:
67
app/Helpers/NotificationsHelper.php
Normal file
67
app/Helpers/NotificationsHelper.php
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user