diff --git a/ApnsPHP/Message.php b/ApnsPHP/Message.php index fb590787..799ca0bb 100644 --- a/ApnsPHP/Message.php +++ b/ApnsPHP/Message.php @@ -11,6 +11,7 @@ use ApnsPHP\Message\Exception; use ApnsPHP\Message\Priority; +use ApnsPHP\Message\PushType; /** * The Push Notification Message. @@ -34,21 +35,6 @@ class Message */ protected const APPLE_RESERVED_NAMESPACE = 'aps'; - /** - * Supported push types. - * @var string[] - */ - protected const APNS_PUSH_TYPES = [ - 'alert', - 'background', - 'location', - 'voip', - 'complication', - 'fileprovider', - 'mdm', - 'liveactivity', - ]; - /** * If the JSON payload is longer than maximum allowed size, shorts message text. * @var bool|null @@ -149,9 +135,9 @@ class Message /** * Push type - * @var 'alert'|'background'|'location'|'voip'|'complication'|'fileprovider'|'mdm'|'liveactivity'|null + * @var PushType|null */ - private ?string $pushType = null; + private ?PushType $pushType = null; /** * Constructor. @@ -740,23 +726,19 @@ public function getCollapseId(): string /** * Set the push type. * - * @param 'alert'|'background'|'location'|'voip'|'complication'|'fileprovider'|'mdm'|'liveactivity' $pushType + * @param PushType $pushType */ - public function setPushType(string $pushType): void + public function setPushType(PushType $pushType): void { - if (!in_array($pushType, self::APNS_PUSH_TYPES)) { - throw new Exception('Invalid push type'); - } - $this->pushType = $pushType; } /** * Get the push type. * - * @return 'alert'|'background'|'location'|'voip'|'complication'|'fileprovider'|'mdm'|'liveactivity'|null Push type + * @return PushType|null Push type */ - public function getPushType(): ?string + public function getPushType(): ?PushType { return $this->pushType; } diff --git a/ApnsPHP/Message/PushType.php b/ApnsPHP/Message/PushType.php new file mode 100644 index 00000000..ffdf3c77 --- /dev/null +++ b/ApnsPHP/Message/PushType.php @@ -0,0 +1,73 @@ +getCustomIdentifier()); } if ($message->getPushType() !== null) { - $headers[] = sprintf('apns-push-type: %s', $message->getPushType()); + $headers[] = sprintf('apns-push-type: %s', $message->getPushType()->value); } if (!empty($this->providerToken)) { $headers[] = sprintf('Authorization: Bearer %s', $this->providerToken); diff --git a/ApnsPHP/Tests/MessageGetTest.php b/ApnsPHP/Tests/MessageGetTest.php index 2a769e36..5a3f0fd6 100644 --- a/ApnsPHP/Tests/MessageGetTest.php +++ b/ApnsPHP/Tests/MessageGetTest.php @@ -10,6 +10,7 @@ namespace ApnsPHP\Tests; use ApnsPHP\Message\Priority; +use ApnsPHP\Message\PushType; /** * This class contains tests for the getter functions @@ -207,11 +208,11 @@ public function testGetPriority(): void */ public function testGetPushType(): void { - $this->set_reflection_property_value('pushType', 'alert'); + $this->set_reflection_property_value('pushType', PushType::Alert); $value = $this->class->getPushType(); - $this->assertSame('alert', $value); + $this->assertSame(PushType::Alert, $value); } /** diff --git a/ApnsPHP/Tests/MessageSetTest.php b/ApnsPHP/Tests/MessageSetTest.php index e2a43abe..61f7b193 100644 --- a/ApnsPHP/Tests/MessageSetTest.php +++ b/ApnsPHP/Tests/MessageSetTest.php @@ -10,6 +10,7 @@ namespace ApnsPHP\Tests; use ApnsPHP\Message\Priority; +use ApnsPHP\Message\PushType; /** * This class contains tests for the setter functions @@ -71,14 +72,15 @@ public function validPriorityProvider(): array public function validPushTypeProvider(): array { $data = []; - $data[] = [ 'alert' ]; - $data[] = [ 'background' ]; - $data[] = [ 'location' ]; - $data[] = [ 'voip' ]; - $data[] = [ 'complication' ]; - $data[] = [ 'fileprovider' ]; - $data[] = [ 'mdm' ]; - $data[] = [ 'liveactivity' ]; + $data[] = [ PushType::Alert ]; + $data[] = [ PushType::Background ]; + $data[] = [ PushType::Location ]; + $data[] = [ PushType::Voip ]; + $data[] = [ PushType::Complication ]; + $data[] = [ PushType::FileProvider ]; + $data[] = [ PushType::Mdm ]; + $data[] = [ PushType::LiveActivity ]; + $data[] = [ PushType::PushToTalk ]; return $data; } @@ -404,28 +406,15 @@ public function testSetValidPriority(Priority $priority): void /** * Test that setPushType() sets a push type. * - * @param string $type Push type value + * @param PushType $type Push type value * * @dataProvider validPushTypeProvider * @covers \ApnsPHP\Message::setPushType */ - public function testSetValidPushType(string $type): void + public function testSetValidPushType(PushType $type): void { $this->class->setPushType($type); $this->assertPropertySame('pushType', $type); } - - /** - * Test that setPushType() throws an exception when trying to set an invalid push type. - * - * @covers \ApnsPHP\Message::setPushType - */ - public function testSetInvalidPushType(): void - { - $this->expectException('ApnsPHP\Message\Exception'); - $this->expectExceptionMessage('Invalid push type'); - - $this->class->setPushType('news'); - } } diff --git a/ApnsPHP/Tests/PushHttpSendTest.php b/ApnsPHP/Tests/PushHttpSendTest.php index d8910375..10877433 100644 --- a/ApnsPHP/Tests/PushHttpSendTest.php +++ b/ApnsPHP/Tests/PushHttpSendTest.php @@ -11,6 +11,7 @@ namespace ApnsPHP\Tests; use ApnsPHP\Message\Priority; +use ApnsPHP\Message\PushType; /** * This class contains tests for the httpSend function @@ -47,7 +48,7 @@ private function setHttpHeaders(): void $this->message->expects($this->exactly(2)) ->method('getPushType') - ->will($this->returnValue('alert')); + ->will($this->returnValue(PushType::Alert)); $this->set_reflection_property_value('providerToken', 'jwt'); }