Skip to content

Commit

Permalink
Improve readability and fix error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Jun 11, 2024
1 parent fa2b942 commit d3d7729
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
15 changes: 5 additions & 10 deletions src/PubNub/Endpoints/Push/PushEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand All @@ -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");
}
}
Expand Down
19 changes: 7 additions & 12 deletions src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/PubNub/Enums/PNPushType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}
}
2 changes: 1 addition & 1 deletion tests/integrational/ModifyPushChannelsForDeviceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public function testMissingChannelsAdd()
$listAdd = new AddChannelsToPushExposed($this->pubnub);

$listAdd->pushType(PNPushType::MPNS)
->deviceId("")
->deviceId("Example")
->sync();
}

Expand Down

0 comments on commit d3d7729

Please sign in to comment.