From d3d77292b60820747962520498a310559fcbd856 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 +++++---------- .../Endpoints/Push/RemoveChannelsFromPush.php | 19 +++++++------------ src/PubNub/Enums/PNPushType.php | 11 +++++++++++ .../ModifyPushChannelsForDeviceTest.php | 2 +- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/PubNub/Endpoints/Push/PushEndpoint.php b/src/PubNub/Endpoints/Push/PushEndpoint.php index 394f4421..44aa85cf 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/Endpoints/Push/RemoveChannelsFromPush.php b/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php index 99c2433f..0a793592 100644 --- a/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php +++ b/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php @@ -34,22 +34,17 @@ public function channels(string | array $channels): static protected function validateParams() { $this->validateSubscribeKey(); + $this->validateChannel(); + $this->validateDeviceId(); + $this->validatePushType(); + $this->validateTopic(); + } + protected function validateChannel() + { if (!is_array($this->channels) || count($this->channels) === 0) { throw new PubNubValidationException("Channel missing"); } - - if (!is_string($this->deviceId) || strlen($this->deviceId) === 0) { - throw new PubNubValidationException("Device ID is missing for push operation"); - } - - if ($this->pushType === null || strlen($this->pushType) === 0) { - throw new PubNubValidationException("Push Type is missing"); - } - - if (($this->pushType == PNPushType::APNS2) && (!is_string($this->topic) || strlen($this->topic) === 0)) { - 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 + ]; + } } diff --git a/tests/integrational/ModifyPushChannelsForDeviceTest.php b/tests/integrational/ModifyPushChannelsForDeviceTest.php index 7d74f3b1..59c1eea0 100644 --- a/tests/integrational/ModifyPushChannelsForDeviceTest.php +++ b/tests/integrational/ModifyPushChannelsForDeviceTest.php @@ -347,7 +347,7 @@ public function testMissingChannelsAdd() $listAdd = new AddChannelsToPushExposed($this->pubnub); $listAdd->pushType(PNPushType::MPNS) - ->deviceId("") + ->deviceId("Example") ->sync(); }