Skip to content

Commit

Permalink
Message: Make the push type an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Jul 12, 2024
1 parent bc13732 commit 20008a1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 52 deletions.
32 changes: 7 additions & 25 deletions ApnsPHP/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

/**
* The Push Notification Message.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
73 changes: 73 additions & 0 deletions ApnsPHP/Message/PushType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

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

namespace ApnsPHP\Message;

/**
* APNS Push Type.
*/
enum PushType: string
{
/**
* The push type for notifications that trigger a user interaction — for example, an alert, badge, or sound.
* If you set this push type, the apns-topic header field must use your app’s bundle ID as the topic.
*/
case Alert = 'alert';

/**
* The push type for notifications that deliver content in the background, and don’t trigger any user interactions.
* If you set this push type, the apns-topic header field must use your app’s bundle ID as the topic.
*/
case Background = 'background';

/**
* The push type for notifications that request a user’s location. If you set this push type, the apns-topic header
* field must use your app’s bundle ID with `.location-query` appended to the end.
*/
case Location = 'location';

/**
* The push type for notifications that provide information about an incoming Voice-over-IP (VoIP) call.
* If you set this push type, the apns-topic header field must use your app’s bundle ID with `.voip` appended
* to the end.
*/
case Voip = 'voip';

/**
* The push type for notifications that contain update information for a watchOS app’s complications.
* If you set this push type, the apns-topic header field must use your app’s bundle ID with `.complication`
* appended to the end.
*/
case Complication = 'complication';

/**
* The push type to signal changes to a File Provider extension. If you set this push type, the apns-topic header
* field must use your app’s bundle ID with `.pushkit.fileprovider` appended to the end.
*/
case FileProvider = 'fileprovider';

/**
* The push type for notifications that tell managed devices to contact the MDM server. If you set this push type,
* you must use the topic from the UID attribute in the subject of your MDM push certificate.
*/
case Mdm = 'mdm';

/**
* The push type to signal changes to a live activity session. If you set this push type, the apns-topic header
* field must use your app’s bundle ID with `.push-type.liveactivity` appended to the end.
*/
case LiveActivity = 'liveactivity';

/**
* The push type for notifications that provide information about updates to your application’s push to talk
* services. If you set this push type, the apns-topic header field must use your app’s bundle ID with `.voip-ptt`
* appended to the end.
*/
case PushToTalk = 'pushtotalk';
}
2 changes: 1 addition & 1 deletion ApnsPHP/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ private function httpSend(Message $message, &$reply): bool
$headers[] = sprintf('apns-id: %s', $message->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);
Expand Down
5 changes: 3 additions & 2 deletions ApnsPHP/Tests/MessageGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ApnsPHP\Tests;

use ApnsPHP\Message\Priority;
use ApnsPHP\Message\PushType;

/**
* This class contains tests for the getter functions
Expand Down Expand Up @@ -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);
}

/**
Expand Down
35 changes: 12 additions & 23 deletions ApnsPHP/Tests/MessageSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ApnsPHP\Tests;

use ApnsPHP\Message\Priority;
use ApnsPHP\Message\PushType;

/**
* This class contains tests for the setter functions
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
}
}
3 changes: 2 additions & 1 deletion ApnsPHP/Tests/PushHttpSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace ApnsPHP\Tests;

use ApnsPHP\Message\Priority;
use ApnsPHP\Message\PushType;

/**
* This class contains tests for the httpSend function
Expand Down Expand Up @@ -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');
}
Expand Down

0 comments on commit 20008a1

Please sign in to comment.