@@ -29,7 +29,7 @@ class WebPush
29
29
/** @var array Array of array of Notifications */
30
30
private $ notifications ;
31
31
32
- /** @var array Default options : TTL, urgency, topic */
32
+ /** @var array Default options : TTL, urgency, topic, batchSize */
33
33
private $ defaultOptions ;
34
34
35
35
/** @var int Automatic padding of payloads, if disabled, trade security for bandwidth */
@@ -39,7 +39,7 @@ class WebPush
39
39
* WebPush constructor.
40
40
*
41
41
* @param array $auth Some servers needs authentication
42
- * @param array $defaultOptions TTL, urgency, topic
42
+ * @param array $defaultOptions TTL, urgency, topic, batchSize
43
43
* @param int|null $timeout Timeout of POST request
44
44
* @param array $clientOptions
45
45
*/
@@ -113,54 +113,60 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
113
113
/**
114
114
* Flush notifications. Triggers the requests.
115
115
*
116
+ * @param int $batchSize Defaults the value defined in defaultOptions during instanciation (which defaults to 1000).
116
117
* @return array|bool If there are no errors, return true.
117
118
* If there were no notifications in the queue, return false.
118
- * Else return an array of information for each notification sent (success, statusCode, headers, content)
119
- *
120
- * @throws \ErrorException
119
+ * Else return an array of information for each notification sent (success, statusCode, headers, content)
121
120
*/
122
- public function flush ()
121
+ public function flush ($ batchSize = null )
123
122
{
124
123
if (empty ($ this ->notifications )) {
125
124
return false ;
126
125
}
127
126
128
- // for each endpoint server type
129
- $ requests = $ this ->prepare ($ this ->notifications );
130
- $ promises = [];
131
- foreach ($ requests as $ request ) {
132
- $ promises [] = $ this ->client ->sendAsync ($ request );
127
+ if (!isset ($ batchSize )) {
128
+ $ batchSize = $ this ->defaultOptions ['batchSize ' ];
133
129
}
134
- $ results = Promise \settle ($ promises )->wait ();
135
130
131
+ $ batches = array_chunk ($ this ->notifications , $ batchSize );
136
132
$ return = array ();
137
133
$ completeSuccess = true ;
138
- foreach ($ results as $ result ) {
139
- if ($ result ['state ' ] === "rejected " ) {
140
- /** @var RequestException $reason **/
141
- $ reason = $ result ['reason ' ];
142
-
143
- $ error = array (
144
- 'success ' => false ,
145
- 'endpoint ' => "" .$ reason ->getRequest ()->getUri (),
146
- 'message ' => $ reason ->getMessage (),
147
- );
148
-
149
- $ response = $ reason ->getResponse ();
150
- if ($ response !== null ) {
151
- $ statusCode = $ response ->getStatusCode ();
152
- $ error ['statusCode ' ] = $ statusCode ;
153
- $ error ['expired ' ] = in_array ($ statusCode , array (400 , 404 , 410 ));
154
- $ error ['content ' ] = $ response ->getBody ();
155
- $ error ['headers ' ] = $ response ->getHeaders ();
134
+ foreach ($ batches as $ batch ) {
135
+ // for each endpoint server type
136
+ $ requests = $ this ->prepare ($ batch );
137
+ $ promises = [];
138
+ foreach ($ requests as $ request ) {
139
+ $ promises [] = $ this ->client ->sendAsync ($ request );
140
+ }
141
+ $ results = Promise \settle ($ promises )->wait ();
142
+
143
+ foreach ($ results as $ result ) {
144
+ if ($ result ['state ' ] === "rejected " ) {
145
+ /** @var RequestException $reason **/
146
+ $ reason = $ result ['reason ' ];
147
+
148
+ $ error = array (
149
+ 'success ' => false ,
150
+ 'endpoint ' => "" .$ reason ->getRequest ()->getUri (),
151
+ 'message ' => $ reason ->getMessage (),
152
+ );
153
+
154
+ $ response = $ reason ->getResponse ();
155
+ if ($ response !== null ) {
156
+ $ statusCode = $ response ->getStatusCode ();
157
+ $ error ['statusCode ' ] = $ statusCode ;
158
+ $ error ['expired ' ] = in_array ($ statusCode , array (400 , 404 , 410 ));
159
+ $ error ['content ' ] = $ response ->getBody ();
160
+ $ error ['headers ' ] = $ response ->getHeaders ();
161
+ }
162
+
163
+ $ return [] = $ error ;
164
+ $ completeSuccess = false ;
165
+ } else {
166
+ $ return [] = array (
167
+ 'success ' => true ,
168
+ );
156
169
}
157
-
158
- $ return [] = $ error ;
159
- $ completeSuccess = false ;
160
- } else {
161
- $ return [] = array (
162
- 'success ' => true ,
163
- );
164
170
}
165
171
}
166
172
@@ -293,12 +299,13 @@ public function getDefaultOptions()
293
299
}
294
300
295
301
/**
296
- * @param array $defaultOptions Keys 'TTL' (Time To Live, defaults 4 weeks), 'urgency', and 'topic'
302
+ * @param array $defaultOptions Keys 'TTL' (Time To Live, defaults 4 weeks), 'urgency', 'topic', 'batchSize '
297
303
*/
298
304
public function setDefaultOptions (array $ defaultOptions )
299
305
{
300
306
$ this ->defaultOptions ['TTL ' ] = array_key_exists ('TTL ' , $ defaultOptions ) ? $ defaultOptions ['TTL ' ] : 2419200 ;
301
307
$ this ->defaultOptions ['urgency ' ] = array_key_exists ('urgency ' , $ defaultOptions ) ? $ defaultOptions ['urgency ' ] : null ;
302
308
$ this ->defaultOptions ['topic ' ] = array_key_exists ('topic ' , $ defaultOptions ) ? $ defaultOptions ['topic ' ] : null ;
309
+ $ this ->defaultOptions ['batchSize ' ] = array_key_exists ('batchSize ' , $ defaultOptions ) ? $ defaultOptions ['batchSize ' ] : 1000 ;
303
310
}
304
311
}
0 commit comments