Skip to content

Commit cfde4bd

Browse files
maglnetMinishlink
authored andcommitted
Allow client subscription structure to be directly passed to create (web-push-libs#191)
this addresses web-push-libs#179
1 parent b3ed289 commit cfde4bd

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ $notifications = [
5656
'contentEncoding' => 'aesgcm', // one of PushManager.supportedContentEncodings
5757
]),
5858
'payload' => '{msg:"test"}',
59-
],
59+
], [
60+
'subscription' => Subscription::create([ // this is the structure for the working draft from october 2018 (https://www.w3.org/TR/2018/WD-push-api-20181026/)
61+
"endpoint" => "https://example.com/other/endpoint/of/another/vendor/abcdef...",
62+
"keys" => [
63+
'p256dh' => '(stringOf88Chars)',
64+
'auth' => '(stringOf24Chars)'
65+
],
66+
]),
67+
'payload' => '{msg:"Hello World!"}',
68+
],
6069
];
6170

6271
$webPush = new WebPush();

src/Subscription.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ public static function create(array $associativeArray): Subscription {
7373
);
7474
}
7575

76+
if (array_key_exists('keys', $associativeArray) && is_array($associativeArray['keys'])) {
77+
return new self(
78+
$associativeArray['endpoint'],
79+
$associativeArray['keys']['p256dh'] ?? null,
80+
$associativeArray['keys']['auth'] ?? null,
81+
$associativeArray['contentEncoding'] ?? "aesgcm"
82+
);
83+
}
84+
7685
return new self(
7786
$associativeArray['endpoint']
7887
);

tests/SubscriptionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,18 @@ public function testConstructFull()
7272
$this->assertEquals("authToken", $subscription->getAuthToken());
7373
$this->assertEquals("aes128gcm", $subscription->getContentEncoding());
7474
}
75+
76+
public function testCreatePartialWithNewStructure()
77+
{
78+
$subscription = Subscription::create([
79+
"endpoint" => "http://toto.com",
80+
"keys" => [
81+
'p256dh' => 'publicKey',
82+
'auth' => 'authToken'
83+
]
84+
]);
85+
$this->assertEquals("http://toto.com", $subscription->getEndpoint());
86+
$this->assertEquals("publicKey", $subscription->getPublicKey());
87+
$this->assertEquals("authToken", $subscription->getAuthToken());
88+
}
7589
}

0 commit comments

Comments
 (0)