Skip to content

Commit

Permalink
Message: Make priority an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Jul 12, 2024
1 parent 1ca2a01 commit bc13732
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 39 deletions.
25 changes: 8 additions & 17 deletions ApnsPHP/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ApnsPHP;

use ApnsPHP\Message\Exception;
use ApnsPHP\Message\Priority;

/**
* The Push Notification Message.
Expand All @@ -33,12 +34,6 @@ class Message
*/
protected const APPLE_RESERVED_NAMESPACE = 'aps';

/**
* Supported notification priorities.
* @var int[]
*/
protected const APNS_PRIORITIES = [ 1, 5, 10 ];

/**
* Supported push types.
* @var string[]
Expand Down Expand Up @@ -148,9 +143,9 @@ class Message

/**
* The priority of the remote notification.
* @var 1|5|10|null
* @var Priority|null
*/
protected ?int $priority = null;
protected ?Priority $priority = null;

/**
* Push type
Expand Down Expand Up @@ -703,25 +698,21 @@ public function getTopic(): string
}

/**
* Set the priority of the remote notification, which is 5 (low) or 10 (high).
* Set the priority of the remote notification.
*
* @param 1|5|10 $priority The priority of the remote notification.
* @param Priority $priority The priority of the remote notification.
*/
public function setPriority(int $priority): void
public function setPriority(Priority $priority): void
{
if (!in_array($priority, self::APNS_PRIORITIES)) {
throw new Exception('Invalid priority');
}

$this->priority = $priority;
}

/**
* Get the priority of the remote notification.
*
* @return 1|5|10|null The priority of the remote notification.
* @return Priority|null The priority of the remote notification.
*/
public function getPriority(): ?int
public function getPriority(): ?Priority
{
return $this->priority;
}
Expand Down
32 changes: 32 additions & 0 deletions ApnsPHP/Message/Priority.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* APNS Push Priority
*
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: BSD-2-Clause
*/

namespace ApnsPHP\Message;

/**
* APNS Push Priority.
*/
enum Priority: int
{
/**
* Send notification immediately. This is the default.
*/
case Immediately = 10;

/**
* Send the notification based on power considerations on the user's device.
*/
case ConsiderPowerUsage = 5;

/**
* Prioritize the device's power considerations over all other factors for delivery,
* and prevent awakening the device.
*/
case PrioritizePowerUsage = 1;
}
2 changes: 1 addition & 1 deletion ApnsPHP/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ private function httpSend(Message $message, &$reply): bool
$headers[] = sprintf('apns-expiration: %s', $message->getExpiry());
}
if (!empty($message->getPriority())) {
$headers[] = sprintf('apns-priority: %s', $message->getPriority());
$headers[] = sprintf('apns-priority: %s', $message->getPriority()->value);
}
if (!empty($message->getCollapseId())) {
$headers[] = sprintf('apns-collapse-id: %s', $message->getCollapseId());
Expand Down
6 changes: 4 additions & 2 deletions ApnsPHP/Tests/MessageGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace ApnsPHP\Tests;

use ApnsPHP\Message\Priority;

/**
* This class contains tests for the getter functions
*
Expand Down Expand Up @@ -191,11 +193,11 @@ public function testGetCollapseId(): void
*/
public function testGetPriority(): void
{
$this->set_reflection_property_value('priority', 5);
$this->set_reflection_property_value('priority', Priority::ConsiderPowerUsage);

$value = $this->class->getPriority();

$this->assertSame(5, $value);
$this->assertSame(Priority::ConsiderPowerUsage, $value);
}

/**
Expand Down
25 changes: 7 additions & 18 deletions ApnsPHP/Tests/MessageSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace ApnsPHP\Tests;

use ApnsPHP\Message\Priority;

/**
* This class contains tests for the setter functions
*
Expand Down Expand Up @@ -54,9 +56,9 @@ public function validCustomIdentifierProvider(): array
public function validPriorityProvider(): array
{
$data = [];
$data[] = [ 1 ];
$data[] = [ 5 ];
$data[] = [ 10 ];
$data[] = [ Priority::PrioritizePowerUsage ];
$data[] = [ Priority::ConsiderPowerUsage ];
$data[] = [ Priority::Immediately ];

return $data;
}
Expand Down Expand Up @@ -387,31 +389,18 @@ public function testSetInvalidCustomIdentifier(): void
/**
* Test that setPriority() sets a message priority.
*
* @param int $priority Priority value
* @param Priority $priority Priority value
*
* @dataProvider validPriorityProvider
* @covers \ApnsPHP\Message::setPriority
*/
public function testSetValidPriority(int $priority): void
public function testSetValidPriority(Priority $priority): void
{
$this->class->setPriority($priority);

$this->assertPropertySame('priority', $priority);
}

/**
* Test that setPriority() throws an exception when trying to set an invalid priority.
*
* @covers \ApnsPHP\Message::setPriority
*/
public function testSetInvalidPriority(): void
{
$this->expectException('ApnsPHP\Message\Exception');
$this->expectExceptionMessage('Invalid priority');

$this->class->setPriority(2);
}

/**
* Test that setPushType() sets a push type.
*
Expand Down
4 changes: 3 additions & 1 deletion ApnsPHP/Tests/PushHttpSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace ApnsPHP\Tests;

use ApnsPHP\Message\Priority;

/**
* This class contains tests for the httpSend function
*
Expand All @@ -33,7 +35,7 @@ private function setHttpHeaders(): void

$this->message->expects($this->exactly(2))
->method('getPriority')
->will($this->returnValue(5));
->willReturn(Priority::ConsiderPowerUsage);

$this->message->expects($this->exactly(2))
->method('getCollapseId')
Expand Down

0 comments on commit bc13732

Please sign in to comment.