From e23fe7f5857016c002b3dd14705448232e459efd Mon Sep 17 00:00:00 2001 From: Michal Borychowski <807297+boryn@users.noreply.github.com> Date: Wed, 6 Dec 2023 16:01:05 +0100 Subject: [PATCH] added pushoverKeyHasWrongLength() check --- src/Exceptions/CouldNotSendNotification.php | 11 +++++++++++ src/PushoverChannel.php | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/src/Exceptions/CouldNotSendNotification.php b/src/Exceptions/CouldNotSendNotification.php index 61e0f50..93ba3bd 100644 --- a/src/Exceptions/CouldNotSendNotification.php +++ b/src/Exceptions/CouldNotSendNotification.php @@ -29,4 +29,15 @@ public static function serviceRespondedWithAnError(ResponseInterface $response, return new static($exceptionMessage, $statusCode); } + + public static function pushoverKeyHasWrongLength($notifiable) + { + $exceptionMessage = sprintf( + "Pushover key has wrong length for notifiable '%s' with id '%s'. It needs to be 30 characters long.", + get_class($notifiable), + $notifiable->getKey() + ); + + return new static($exceptionMessage); + } } diff --git a/src/PushoverChannel.php b/src/PushoverChannel.php index c42f960..072f9e4 100644 --- a/src/PushoverChannel.php +++ b/src/PushoverChannel.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Notifications\Events\NotificationFailed; use Illuminate\Notifications\Notification; +use NotificationChannels\Pushover\Exceptions\CouldNotSendNotification; use NotificationChannels\Pushover\Exceptions\ServiceCommunicationError; class PushoverChannel @@ -41,6 +42,12 @@ public function send($notifiable, Notification $notification) } if (is_string($pushoverReceiver)) { + // From https://pushover.net/api: + // "User and group identifiers are 30 characters long, ..." + if (strlen($pushoverReceiver) !== 30) { + throw CouldNotSendNotification::pushoverKeyHasWrongLength($notifiable); + } + $pushoverReceiver = PushoverReceiver::withUserKey($pushoverReceiver); }