Skip to content

Commit 94c7677

Browse files
authored
[BC] Rename sendNotification and add sendOneNotification (#218)
* [BC] Rename sendNotification and add sendOneNotification * Fix doc Phpdoc
1 parent 92f4e2b commit 94c7677

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ $webPush = new WebPush();
7171

7272
// send multiple notifications with payload
7373
foreach ($notifications as $notification) {
74-
$webPush->sendNotification(
74+
$webPush->queueNotification(
7575
$notification['subscription'],
7676
$notification['payload'] // optional (defaults null)
7777
);
@@ -93,12 +93,11 @@ foreach ($webPush->flush() as $report) {
9393

9494
/**
9595
* send one notification and flush directly
96-
* @var \Generator<MessageSentReport> $sent
96+
* @var MessageSentReport $report
9797
*/
98-
$sent = $webPush->sendNotification(
98+
$report = $webPush->sendOneNotification(
9999
$notifications[0]['subscription'],
100-
$notifications[0]['payload'], // optional (defaults null)
101-
true // optional (defaults false)
100+
$notifications[0]['payload'] // optional (defaults null)
102101
);
103102
```
104103

@@ -132,7 +131,7 @@ $auth = [
132131
];
133132

134133
$webPush = new WebPush($auth);
135-
$webPush->sendNotification(...);
134+
$webPush->queueNotification(...);
136135
```
137136

138137
In order to generate the uncompressed public and secret key, encoded in Base64, enter the following in your Linux bash:
@@ -184,7 +183,7 @@ $webPush = new WebPush([], $defaultOptions);
184183
$webPush->setDefaultOptions($defaultOptions);
185184

186185
// or for one notification
187-
$webPush->sendNotification($subscription, $payload, $flush, ['TTL' => 5000]);
186+
$webPush->sendOneNotification($subscription, $payload, ['TTL' => 5000]);
188187
```
189188

190189
#### TTL
@@ -208,8 +207,9 @@ it as a parameter : `$webPush->flush($batchSize)`.
208207

209208
### Server errors
210209
You can see what the browser vendor's server sends back in case it encountered an error (push subscription expiration, wrong parameters...).
211-
`sendNotification()` (with `$flush` as `true`) and `flush()` **always** returns a [`\Generator`](http://php.net/manual/en/language.generators.php) with [`MessageSentReport`](https://github.com/web-push-libs/web-push-php/blob/master/src/MessageSentReport.php) objects, even if you just send one notification.
212-
To loop through the results, just pass it into `foreach`. You can also use [`iterator_to_array`](http://php.net/manual/en/function.iterator-to-array.php) to check the contents while debugging.
210+
211+
* `sendOneNotification()` returns a [`MessageSentReport`](https://github.com/web-push-libs/web-push-php/blob/master/src/MessageSentReport.php)
212+
* `flush()` returns a [`\Generator`](http://php.net/manual/en/language.generators.php) with [`MessageSentReport`](https://github.com/web-push-libs/web-push-php/blob/master/src/MessageSentReport.php) objects. To loop through the results, just pass it into `foreach`. You can also use [`iterator_to_array`](http://php.net/manual/en/function.iterator-to-array.php) to check the contents while debugging.
213213

214214
```php
215215
<?php
@@ -346,7 +346,7 @@ require __DIR__ . '/path/to/vendor/autoload.php';
346346
```
347347

348348
### I must use PHP 5.4 or 5.5. What can I do?
349-
You won't be able to send any payload, so you'll only be able to use `sendNotification($subscription)`.
349+
You won't be able to send any payload, so you'll only be able to use `sendOneNotification($subscription)` or `queueNotification($subscription)`.
350350
Install the library with `composer` using `--ignore-platform-reqs`.
351351
The workaround for getting the payload is to fetch it in the service worker ([example](https://github.com/Minishlink/physbook/blob/2ed8b9a8a217446c9747e9191a50d6312651125d/web/service-worker.js#L75)).
352352

src/WebPush.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,16 @@ public function __construct(array $auth = [], array $defaultOptions = [], ?int $
102102
}
103103

104104
/**
105-
* Send a notification.
105+
* Queue a notification. Will be sent when flush() is called.
106106
*
107107
* @param SubscriptionInterface $subscription
108-
* @param string|null $payload If you want to send an array, json_encode it
109-
* @param bool $flush If you want to flush directly (usually when you send only one notification)
108+
* @param string|null $payload If you want to send an array or object, json_encode it
110109
* @param array $options Array with several options tied to this notification. If not set, will use the default options that you can set in the WebPush object
111110
* @param array $auth Use this auth details instead of what you provided when creating WebPush
112111
*
113-
* @return \Generator|MessageSentReport[]|true Return an array of information if $flush is set to true and the queued requests has failed.
114-
* Else return true
115-
*
116112
* @throws \ErrorException
117113
*/
118-
public function sendNotification(SubscriptionInterface $subscription, ?string $payload = null, bool $flush = false, array $options = [], array $auth = [])
114+
public function queueNotification(SubscriptionInterface $subscription, ?string $payload = null, array $options = [], array $auth = []): void
119115
{
120116
if (isset($payload)) {
121117
if (Utils::safeStrlen($payload) > Encryption::MAX_PAYLOAD_LENGTH) {
@@ -135,8 +131,20 @@ public function sendNotification(SubscriptionInterface $subscription, ?string $p
135131
}
136132

137133
$this->notifications[] = new Notification($subscription, $payload, $options, $auth);
134+
}
138135

139-
return false !== $flush ? $this->flush() : true;
136+
/**
137+
* @param SubscriptionInterface $subscription
138+
* @param string|null $payload If you want to send an array or object, json_encode it
139+
* @param array $options Array with several options tied to this notification. If not set, will use the default options that you can set in the WebPush object
140+
* @param array $auth Use this auth details instead of what you provided when creating WebPush
141+
* @return MessageSentReport
142+
* @throws \ErrorException
143+
*/
144+
public function sendOneNotification(SubscriptionInterface $subscription, ?string $payload = null, array $options = [], array $auth = []): MessageSentReport
145+
{
146+
$this->queueNotification($subscription, $payload, $options, $auth);
147+
return $this->flush()->current();
140148
}
141149

142150
/**

tests/PushServiceTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,9 @@ protected function createClosureTest($browserId, $browserVersion, $options)
177177
$subscription = new Subscription($endpoint, $p256dh, $auth, $contentEncoding);
178178

179179
try {
180-
$sendResp = $this->webPush->sendNotification($subscription, $payload, true);
181-
$this->assertInstanceOf(\Generator::class, $sendResp);
182-
183-
/** @var \Minishlink\WebPush\MessageSentReport $report */
184-
foreach ($sendResp as $report) {
185-
$this->assertTrue($report->isSuccess());
186-
}
180+
$report = $this->webPush->sendOneNotification($subscription, $payload);
181+
$this->assertInstanceOf(\Minishlink\WebPush\MessageSentReport::class, $report);
182+
$this->assertTrue($report->isSuccess());
187183

188184
$dataString = json_encode([
189185
'testSuiteId' => self::$testSuiteId,

tests/WebPushTest.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,10 @@ public function notificationProvider(): array
9393
* @param string $payload
9494
* @throws ErrorException
9595
*/
96-
public function testSendNotification($subscription, $payload)
96+
public function testSendOneNotification($subscription, $payload)
9797
{
98-
$reports = $this->webPush->sendNotification($subscription, $payload, true);
99-
100-
foreach ($reports as $report) {
101-
$this->assertTrue($report->isSuccess());
102-
}
98+
$report = $this->webPush->sendOneNotification($subscription, $payload);
99+
$this->assertTrue($report->isSuccess());
103100
}
104101

105102
/**
@@ -114,7 +111,7 @@ public function testSendNotificationBatch()
114111
$notifications = array_fill(0, $total, $notifications[0]);
115112

116113
foreach ($notifications as $notification) {
117-
$this->webPush->sendNotification($notification[0], $notification[1]);
114+
$this->webPush->queueNotification($notification[0], $notification[1]);
118115
}
119116

120117
$reports = $this->webPush->flush($batchSize);
@@ -127,16 +124,15 @@ public function testSendNotificationBatch()
127124
/**
128125
* @throws ErrorException
129126
*/
130-
public function testSendNotificationWithTooBigPayload()
127+
public function testSendOneNotificationWithTooBigPayload()
131128
{
132129
$this->expectException('ErrorException');
133130
$this->expectExceptionMessage('Size of payload must not be greater than 4078 octets.');
134131

135132
$subscription = new Subscription(self::$endpoints['standard'], self::$keys['standard']);
136-
$this->webPush->sendNotification(
133+
$this->webPush->sendOneNotification(
137134
$subscription,
138-
str_repeat('test', 1020),
139-
true
135+
str_repeat('test', 1020)
140136
);
141137
}
142138

@@ -146,14 +142,14 @@ public function testSendNotificationWithTooBigPayload()
146142
public function testFlush() {
147143
$subscription = new Subscription(self::$endpoints['standard']);
148144

149-
$this->webPush->sendNotification($subscription);
150-
$this->assertNotEmpty(iterator_to_array($this->webPush->flush()));
145+
$report = $this->webPush->sendOneNotification($subscription);
146+
$this->assertFalse($report->isSuccess()); // it doesn't have VAPID
151147

152148
// queue has been reset
153149
$this->assertEmpty(iterator_to_array($this->webPush->flush()));
154150

155-
$this->webPush->sendNotification($subscription);
156-
$this->assertNotEmpty(iterator_to_array($this->webPush->flush()));
151+
$report = $this->webPush->sendOneNotification($subscription);
152+
$this->assertFalse($report->isSuccess()); // it doesn't have VAPID
157153

158154
$nonExistantSubscription = Subscription::create([
159155
'endpoint' => 'https://fcm.googleapis.com/fcm/send/fCd2-8nXJhU:APA91bGi2uaqFXGft4qdolwyRUcUPCL1XV_jWy1tpCRqnu4sk7ojUpC5gnq1PTncbCdMq9RCVQIIFIU9BjzScvjrDqpsI7J-K_3xYW8xo1xSNCfge1RvJ6Xs8RGL_Sw7JtbCyG1_EVgWDc22on1r_jozD8vsFbB0Fg',
@@ -163,9 +159,9 @@ public function testFlush() {
163159
]);
164160

165161
// test multiple requests
166-
$this->webPush->sendNotification($nonExistantSubscription, json_encode(['test' => 1]));
167-
$this->webPush->sendNotification($nonExistantSubscription, json_encode(['test' => 2]));
168-
$this->webPush->sendNotification($nonExistantSubscription, json_encode(['test' => 3]));
162+
$this->webPush->queueNotification($nonExistantSubscription, json_encode(['test' => 1]));
163+
$this->webPush->queueNotification($nonExistantSubscription, json_encode(['test' => 2]));
164+
$this->webPush->queueNotification($nonExistantSubscription, json_encode(['test' => 3]));
169165

170166
/** @var \Minishlink\WebPush\MessageSentReport $report */
171167
foreach ($this->webPush->flush() as $report) {
@@ -187,10 +183,10 @@ public function testFlushEmpty(): void {
187183
public function testCount(): void {
188184
$subscription = new Subscription(self::$endpoints['standard']);
189185

190-
$this->webPush->sendNotification($subscription);
191-
$this->webPush->sendNotification($subscription);
192-
$this->webPush->sendNotification($subscription);
193-
$this->webPush->sendNotification($subscription);
186+
$this->webPush->queueNotification($subscription);
187+
$this->webPush->queueNotification($subscription);
188+
$this->webPush->queueNotification($subscription);
189+
$this->webPush->queueNotification($subscription);
194190

195191
$this->assertEquals(4, $this->webPush->countPendingNotifications());
196192
}

0 commit comments

Comments
 (0)