Skip to content

Commit

Permalink
Allow client subscription structure to be directly passed to create (#…
Browse files Browse the repository at this point in the history
…191)

this addresses #179
  • Loading branch information
maglnet authored and Minishlink committed Nov 29, 2018
1 parent b3ed289 commit cfde4bd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ $notifications = [
'contentEncoding' => 'aesgcm', // one of PushManager.supportedContentEncodings
]),
'payload' => '{msg:"test"}',
],
], [
'subscription' => Subscription::create([ // this is the structure for the working draft from october 2018 (https://www.w3.org/TR/2018/WD-push-api-20181026/)
"endpoint" => "https://example.com/other/endpoint/of/another/vendor/abcdef...",
"keys" => [
'p256dh' => '(stringOf88Chars)',
'auth' => '(stringOf24Chars)'
],
]),
'payload' => '{msg:"Hello World!"}',
],
];

$webPush = new WebPush();
Expand Down
9 changes: 9 additions & 0 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public static function create(array $associativeArray): Subscription {
);
}

if (array_key_exists('keys', $associativeArray) && is_array($associativeArray['keys'])) {
return new self(
$associativeArray['endpoint'],
$associativeArray['keys']['p256dh'] ?? null,
$associativeArray['keys']['auth'] ?? null,
$associativeArray['contentEncoding'] ?? "aesgcm"
);
}

return new self(
$associativeArray['endpoint']
);
Expand Down
14 changes: 14 additions & 0 deletions tests/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,18 @@ public function testConstructFull()
$this->assertEquals("authToken", $subscription->getAuthToken());
$this->assertEquals("aes128gcm", $subscription->getContentEncoding());
}

public function testCreatePartialWithNewStructure()
{
$subscription = Subscription::create([
"endpoint" => "http://toto.com",
"keys" => [
'p256dh' => 'publicKey',
'auth' => 'authToken'
]
]);
$this->assertEquals("http://toto.com", $subscription->getEndpoint());
$this->assertEquals("publicKey", $subscription->getPublicKey());
$this->assertEquals("authToken", $subscription->getAuthToken());
}
}

0 comments on commit cfde4bd

Please sign in to comment.