diff --git a/composer.json b/composer.json index 61b217b..770c56c 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "license": "MIT", "require": { "psr/log": ">=1.1", - "m2mobi/apns-php": "~3.0", + "m2mobi/apns-php": "dev-master", "phpmailer/phpmailer": "~6.4", "rmccue/requests": "~2.0", "php": ">=8.1", diff --git a/decomposer.json b/decomposer.json index c60ac73..a6ae248 100644 --- a/decomposer.json +++ b/decomposer.json @@ -19,8 +19,8 @@ }, "ApnsPHP": { "url": "https://github.com/move-backend/apnsphp.git", - "version": "3.0.3", - "revision": "release/3.0.x", + "version": "4.0.0", + "revision": "master", "psr0": { "path": "/" } diff --git a/src/Lunr/Vortex/APNS/APNSPayload.php b/src/Lunr/Vortex/APNS/APNSPayload.php index e373002..d22b89b 100644 --- a/src/Lunr/Vortex/APNS/APNSPayload.php +++ b/src/Lunr/Vortex/APNS/APNSPayload.php @@ -10,7 +10,7 @@ namespace Lunr\Vortex\APNS; -use ReflectionClass; +use ApnsPHP\Message\Priority; /** * Apple Push Notification Service Payload Generator. @@ -31,7 +31,7 @@ public function __construct() { $this->elements = []; - $this->elements['priority'] = APNSPriority::HIGH; + $this->elements['priority'] = Priority::Immediately; } /** @@ -88,18 +88,13 @@ public function set_topic(string $topic): self /** * Mark the notification priority. * - * @param int $priority Notification priority value. + * @param Priority $priority Notification priority value. * * @return APNSPayload Self Reference */ - public function set_priority(int $priority): self + public function set_priority(Priority $priority): self { - $priority_class = new ReflectionClass('\Lunr\Vortex\APNS\APNSPriority'); - - if (in_array($priority, array_values($priority_class->getConstants()))) - { - $this->elements['priority'] = $priority; - } + $this->elements['priority'] = $priority; return $this; } diff --git a/src/Lunr/Vortex/APNS/APNSPriority.php b/src/Lunr/Vortex/APNS/APNSPriority.php deleted file mode 100644 index 3f40414..0000000 --- a/src/Lunr/Vortex/APNS/APNSPriority.php +++ /dev/null @@ -1,33 +0,0 @@ - diff --git a/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php b/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php index fe46f90..4b6e38b 100644 --- a/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php +++ b/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php @@ -12,6 +12,7 @@ use ApnsPHP\Exception as ApnsPHPException; use ApnsPHP\Message\Exception as MessageException; +use ApnsPHP\Message\Priority; use ApnsPHP\Push\Exception as PushException; use Lunr\Vortex\Email\EmailPayload; use Lunr\Vortex\FCM\FCMPayload; @@ -124,7 +125,7 @@ public function testPushConstructsCorrectPayload(): void 'mutable_content' => TRUE, 'content_available' => TRUE, 'topic' => 'com.company.app', - 'priority' => 5, + 'priority' => Priority::ConsiderPowerUsage, 'collapse_key' => 'key', 'identifier' => 'identifier', 'yo' => 'he', diff --git a/src/Lunr/Vortex/APNS/Tests/APNSPayloadSetTest.php b/src/Lunr/Vortex/APNS/Tests/APNSPayloadSetTest.php index 6916cd5..5663872 100644 --- a/src/Lunr/Vortex/APNS/Tests/APNSPayloadSetTest.php +++ b/src/Lunr/Vortex/APNS/Tests/APNSPayloadSetTest.php @@ -10,6 +10,8 @@ namespace Lunr\Vortex\APNS\Tests; +use ApnsPHP\Message\Priority; + /** * This class contains tests for the setters of the APNSPayload class. * @@ -311,27 +313,12 @@ public function testSetTopicReturnsSelfReference(): void */ public function testPriority(): void { - $this->class->set_priority(5); - - $value = $this->get_reflection_property_value('elements'); - - $this->assertArrayHasKey('priority', $value); - $this->assertEquals(5, $value['priority']); - } - - /** - * Test set_priority() works correctly. - * - * @covers \Lunr\Vortex\APNS\APNSPayload::set_priority - */ - public function testPriorityIgnoresWrongValues(): void - { - $this->class->set_priority(7); + $this->class->set_priority(Priority::ConsiderPowerUsage); $value = $this->get_reflection_property_value('elements'); $this->assertArrayHasKey('priority', $value); - $this->assertEquals(10, $value['priority']); + $this->assertEquals(Priority::ConsiderPowerUsage, $value['priority']); } /** @@ -341,7 +328,7 @@ public function testPriorityIgnoresWrongValues(): void */ public function testSetPriorityReturnsSelfReference(): void { - $this->assertSame($this->class, $this->class->set_priority(5)); + $this->assertSame($this->class, $this->class->set_priority(Priority::ConsiderPowerUsage)); } /** diff --git a/src/Lunr/Vortex/FCM/FCMApnsPayload.php b/src/Lunr/Vortex/FCM/FCMApnsPayload.php index 7f12777..490b2fd 100644 --- a/src/Lunr/Vortex/FCM/FCMApnsPayload.php +++ b/src/Lunr/Vortex/FCM/FCMApnsPayload.php @@ -9,9 +9,6 @@ namespace Lunr\Vortex\FCM; -use Lunr\Vortex\APNS\APNSPriority; -use ReflectionClass; - /** * Firebase Cloud Messaging Push Notification APNS Payload Generator. * @@ -114,13 +111,11 @@ public function set_mutable_content(bool $mutable): static */ public function set_priority(string $priority): static { - $priority = strtoupper($priority); - - $priority_class = new ReflectionClass(APNSPriority::class); - $priorities = $priority_class->getConstants(); - if (in_array($priority, array_keys($priorities))) + $priority = strtolower($priority); + $matches = array_filter(FCMApnsPriority::cases(), fn($case) => strtolower($case->name) === $priority); + if (count($matches) === 1) { - $this->elements['headers']['apns-priority'] = $priorities[$priority]; + $this->elements['headers']['apns-priority'] = (string) array_pop($matches)->value; } return $this; diff --git a/src/Lunr/Vortex/FCM/FCMApnsPriority.php b/src/Lunr/Vortex/FCM/FCMApnsPriority.php new file mode 100644 index 0000000..abf4d69 --- /dev/null +++ b/src/Lunr/Vortex/FCM/FCMApnsPriority.php @@ -0,0 +1,36 @@ + diff --git a/src/Lunr/Vortex/FCM/Tests/FCMApnsPayloadSetTest.php b/src/Lunr/Vortex/FCM/Tests/FCMApnsPayloadSetTest.php index 1ddcb34..f622e39 100644 --- a/src/Lunr/Vortex/FCM/Tests/FCMApnsPayloadSetTest.php +++ b/src/Lunr/Vortex/FCM/Tests/FCMApnsPayloadSetTest.php @@ -98,20 +98,43 @@ public function testSetMutableContentReturnsSelfReference(): void $this->assertSame($this->class, $this->class->set_mutable_content(TRUE)); } + /** + * Provide different priority options for testing. + * + * @return array + */ + public function priorityProvider(): array + { + $return = []; + + $return['lowercase'] = [ 'high', 10 ]; + $return['titlecase'] = [ 'High', 10 ]; + $return['uppercase'] = [ 'HIGH', 10 ]; + $return['default'] = [ 'default', 5 ]; + $return['low'] = [ 'low', 1 ]; + + return $return; + } + /** * Test set_priority() works correctly. * + * @dataProvider priorityProvider + * + * @param string $priority The priority to set + * @param int $expected The expected value + * * @covers \Lunr\Vortex\FCM\FCMApnsPayload::set_priority */ - public function testSetPriority(): void + public function testSetPriority(string $priority, int $expected): void { - $this->class->set_priority('high'); + $this->class->set_priority($priority); $value = $this->get_reflection_property_value('elements'); $this->assertArrayHasKey('headers', $value); $this->assertArrayHasKey('apns-priority', $value['headers']); - $this->assertEquals(10, $value['headers']['apns-priority']); + $this->assertEquals($expected, $value['headers']['apns-priority']); } /**