From 5052f5c2d314b597240d05f8979adbe19a4a93de Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Tue, 11 Jun 2024 11:30:53 +0200 Subject: [PATCH] Improve readability and fix error messages --- src/PubNub/Endpoints/Push/PushEndpoint.php | 15 +++++---------- src/PubNub/Enums/PNPushType.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/PubNub/Endpoints/Push/PushEndpoint.php b/src/PubNub/Endpoints/Push/PushEndpoint.php index 394f4421..6851f7d4 100644 --- a/src/PubNub/Endpoints/Push/PushEndpoint.php +++ b/src/PubNub/Endpoints/Push/PushEndpoint.php @@ -58,20 +58,15 @@ public function pushType(string $pushType): static protected function validatePushType() { - if (!isset($this->pushType) || $this->pushType === null || strlen($this->pushType) === 0) { - throw new PubNubValidationException("Push type missing"); + if (!isset($this->pushType) || empty($this->pushType)) { + throw new PubNubValidationException("Push type is missing"); } if ($this->pushType === PNPushType::GCM) { trigger_error("GCM is deprecated. Please use FCM instead.", E_USER_DEPRECATED); } - if ( - !in_array( - $this->pushType, - [PNPushType::APNS, PNPushType::APNS2, PNPushType::MPNS, PNPushType::GCM, PNPushType::FCM] - ) - ) { + if (!in_array($this->pushType, PNPushType::all())) { throw new PubNubValidationException("Invalid push type"); } } @@ -81,14 +76,14 @@ protected function validatePushType() */ protected function validateDeviceId() { - if (!is_string($this->deviceId) || strlen($this->deviceId) === 0) { + if (!isset($this->deviceId) || empty($this->deviceId)) { throw new PubNubValidationException("Device ID is missing for push operation"); } } protected function validateTopic() { - if (($this->pushType == PNPushType::APNS2) && (!is_string($this->topic) || strlen($this->topic) === 0)) { + if (($this->pushType == PNPushType::APNS2) && (!isset($this->topic) || empty($this->topic))) { throw new PubNubValidationException("APNS2 topic is missing"); } } diff --git a/src/PubNub/Enums/PNPushType.php b/src/PubNub/Enums/PNPushType.php index a74e39cc..ee981c81 100644 --- a/src/PubNub/Enums/PNPushType.php +++ b/src/PubNub/Enums/PNPushType.php @@ -9,4 +9,15 @@ class PNPushType public const MPNS = "mpns"; public const GCM = "gcm"; public const FCM = "fcm"; + + public static function all() + { + return [ + self::APNS, + self::APNS2, + self::MPNS, + self::GCM, + self::FCM + ]; + } }