Skip to content

Commit 4537e1d

Browse files
committed
change API to take into account user auth token
1 parent 34491d0 commit 4537e1d

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

phpunit.dist.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<env name="STANDARD_ENDPOINT" value="" />
1919
<env name="GCM_ENDPOINT" value="" />
2020
<env name="USER_PUBLIC_KEY" value="" />
21+
<env name="USER_AUTH_TOKEN" value="" />
2122
<env name="GCM_API_KEY" value="" />
2223
</php>
2324
</phpunit>

src/Notification.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ class Notification
1616
/** @var string */
1717
private $endpoint;
1818

19-
public function __construct($endpoint, $payload, $userPublicKey)
19+
/** @var string */
20+
private $payload;
21+
22+
/** @var string */
23+
private $userPublicKey;
24+
25+
/** @var string */
26+
private $userAuthToken;
27+
28+
public function __construct($endpoint, $payload, $userPublicKey, $userAuthToken)
2029
{
2130
$this->endpoint = $endpoint;
2231
$this->payload = $payload;
2332
$this->userPublicKey = $userPublicKey;
33+
$this->userAuthToken = $userAuthToken;
2434
}
2535

2636
/**
@@ -46,4 +56,12 @@ public function getUserPublicKey()
4656
{
4757
return $this->userPublicKey;
4858
}
59+
60+
/**
61+
* @return null|string
62+
*/
63+
public function getUserAuthToken()
64+
{
65+
return $this->userAuthToken;
66+
}
4967
}

src/WebPush.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,25 @@ public function __construct(array $apiKeys = array(), $TTL = 2419200, $timeout =
6666
/**
6767
* Send a notification.
6868
*
69-
* @param string $endpoint
69+
* @param string $endpoint
7070
* @param string|null $payload If you want to send an array, json_encode it.
7171
* @param string|null $userPublicKey
72-
* @param bool $flush If you want to flush directly (usually when you send only one notification)
72+
* @param string|null $userAuthToken
73+
* @param bool $flush If you want to flush directly (usually when you send only one notification)
7374
*
74-
* @return bool|array Return an array of information if $flush is set to true and the queued requests has failed.
75+
* @return array|bool Return an array of information if $flush is set to true and the queued requests has failed.
7576
* Else return true.
7677
* @throws \ErrorException
7778
*/
78-
public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $flush = false)
79+
public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $userAuthToken = null, $flush = false)
7980
{
81+
if (isset($userAuthToken) && is_bool($userAuthToken)) {
82+
throw new \ErrorException('The API has changed: sendNotification now takes the optional user auth token as parameter.');
83+
}
84+
8085
// sort notification by server type
8186
$type = $this->sortEndpoint($endpoint);
82-
$this->notificationsByServerType[$type][] = new Notification($endpoint, $payload, $userPublicKey);
87+
$this->notificationsByServerType[$type][] = new Notification($endpoint, $payload, $userPublicKey, $userAuthToken);
8388

8489
if ($flush) {
8590
$res = $this->flush();
@@ -182,7 +187,7 @@ private function sendToStandardEndpoints(array $notifications)
182187
$userPublicKey = $notification->getUserPublicKey();
183188

184189
if (isset($payload) && isset($userPublicKey) && ($this->payloadEncryptionSupport || $this->nativePayloadEncryptionSupport)) {
185-
$encrypted = Encryption::encrypt($payload, $userPublicKey, null, $this->nativePayloadEncryptionSupport);
190+
$encrypted = Encryption::encrypt($payload, $userPublicKey, $notification->getUserAuthToken(), $this->nativePayloadEncryptionSupport);
186191

187192
$headers = array(
188193
'Content-Length' => strlen($encrypted['cipherText']),

tests/WebPushTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class WebPushTest extends PHPUnit_Framework_TestCase
1515
{
1616
private $endpoints;
1717
private $keys;
18+
private $tokens;
1819

1920
/** @var WebPush WebPush with correct api keys */
2021
private $webPush;
@@ -31,12 +32,16 @@ public function setUp()
3132
'GCM' => getenv('GCM_API_KEY'),
3233
);
3334

35+
$this->tokens = array(
36+
'standard' => getenv('USER_AUTH_TOKEN'),
37+
);
38+
3439
$this->webPush = new WebPush($this->keys);
3540
}
3641

3742
public function testSendNotification()
3843
{
39-
$res = $this->webPush->sendNotification($this->endpoints['standard'], null, null, true);
44+
$res = $this->webPush->sendNotification($this->endpoints['standard'], null, null, null, true);
4045

4146
$this->assertEquals($res, true);
4247
}
@@ -47,12 +52,37 @@ public function testSendNotificationWithPayload()
4752
$this->endpoints['standard'],
4853
'test',
4954
$this->keys['standard'],
55+
$this->tokens['standard'],
5056
true
5157
);
5258

5359
$this->assertTrue($res);
5460
}
5561

62+
public function testSendNotificationWithPayloadWithoutAuthToken()
63+
{
64+
$res = $this->webPush->sendNotification(
65+
$this->endpoints['standard'],
66+
'test',
67+
$this->keys['standard'],
68+
null,
69+
true
70+
);
71+
72+
$this->assertTrue($res);
73+
}
74+
75+
public function testSendNotificationWithOldAPI()
76+
{
77+
$this->setExpectedException('ErrorException', 'The API has changed: sendNotification now takes the optional user auth token as parameter.');
78+
$this->webPush->sendNotification(
79+
$this->endpoints['standard'],
80+
'test',
81+
$this->keys['standard'],
82+
true
83+
);
84+
}
85+
5686
public function testSendNotifications()
5787
{
5888
foreach($this->endpoints as $endpoint) {
@@ -81,14 +111,14 @@ public function testSendGCMNotificationWithoutGCMApiKey()
81111
$webPush = new WebPush();
82112

83113
$this->setExpectedException('ErrorException', 'No GCM API Key specified.');
84-
$webPush->sendNotification($this->endpoints['GCM'], null, null, true);
114+
$webPush->sendNotification($this->endpoints['GCM'], null, null, null, true);
85115
}
86116

87117
public function testSendGCMNotificationWithWrongGCMApiKey()
88118
{
89119
$webPush = new WebPush(array('GCM' => 'bar'));
90120

91-
$res = $webPush->sendNotification($this->endpoints['GCM'], null, null, true);
121+
$res = $webPush->sendNotification($this->endpoints['GCM'], null, null, null, true);
92122

93123
$this->assertTrue(is_array($res)); // there has been an error
94124
$this->assertArrayHasKey('success', $res);

0 commit comments

Comments
 (0)