Skip to content

Commit

Permalink
Apns: Use upstream enum for priority
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Dec 5, 2024
1 parent 13b16a6 commit e541e0c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 77 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions decomposer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "/"
}
Expand Down
15 changes: 5 additions & 10 deletions src/Lunr/Vortex/APNS/APNSPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Lunr\Vortex\APNS;

use ReflectionClass;
use ApnsPHP\Message\Priority;

/**
* Apple Push Notification Service Payload Generator.
Expand All @@ -31,7 +31,7 @@ public function __construct()
{
$this->elements = [];

$this->elements['priority'] = APNSPriority::HIGH;
$this->elements['priority'] = Priority::Immediately;
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
33 changes: 0 additions & 33 deletions src/Lunr/Vortex/APNS/APNSPriority.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down
23 changes: 5 additions & 18 deletions src/Lunr/Vortex/APNS/Tests/APNSPayloadSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Lunr\Vortex\APNS\Tests;

use ApnsPHP\Message\Priority;

/**
* This class contains tests for the setters of the APNSPayload class.
*
Expand Down Expand Up @@ -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']);
}

/**
Expand All @@ -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));
}

/**
Expand Down
13 changes: 4 additions & 9 deletions src/Lunr/Vortex/FCM/FCMApnsPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

namespace Lunr\Vortex\FCM;

use Lunr\Vortex\APNS\APNSPriority;
use ReflectionClass;

/**
* Firebase Cloud Messaging Push Notification APNS Payload Generator.
*
Expand Down Expand Up @@ -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'] = array_pop($matches)->value;

Check failure on line 118 in src/Lunr/Vortex/FCM/FCMApnsPayload.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Property Lunr\Vortex\FCM\FCMApnsPayload::$elements (array{headers?: array<string, string>, payload?: array<string, mixed>, fcm_options?: array{analytics_label?: string, image?: string}}) does not accept array{headers: non-empty-array<string, 1|5|string>, payload?: array<string, mixed>, fcm_options?: array{analytics_label?: string, image?: string}}.
}

return $this;
Expand Down
36 changes: 36 additions & 0 deletions src/Lunr/Vortex/FCM/FCMApnsPriority.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file contains priority types for Firebase Cloud Messaging Notifications to APNS.
*
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Vortex\FCM;

/**
* Firebase Cloud Messaging APNS Priority Types.
*/
enum FCMApnsPriority: int
{

/**
* Prioritize the device’s power considerations over all other factors for delivery,
* and prevent awakening the device
*/
case Low = 1;

/**
* Send the notification based on power considerations on the user’s device
*/
case Default = 5;

/**
* Send the notification immediately
*/
case High = 10;

}

?>
29 changes: 26 additions & 3 deletions src/Lunr/Vortex/FCM/Tests/FCMApnsPayloadSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down

0 comments on commit e541e0c

Please sign in to comment.