11
11
12
12
namespace Minishlink \WebPush ;
13
13
14
- use Buzz \Browser ;
15
- use Buzz \Client \AbstractClient ;
16
- use Buzz \Client \MultiCurl ;
17
- use Buzz \Exception \RequestException ;
18
- use Buzz \Message \Response ;
14
+ use GuzzleHttp \Client ;
15
+ use GuzzleHttp \Exception \RequestException ;
16
+ use GuzzleHttp \Psr7 \Request ;
17
+ use GuzzleHttp \Promise ;
19
18
20
19
class WebPush
21
20
{
22
21
const GCM_URL = 'https://android.googleapis.com/gcm/send ' ;
23
22
24
- /** @var Browser */
25
- protected $ browser ;
23
+ /** @var Client */
24
+ protected $ client ;
26
25
27
26
/** @var array */
28
27
protected $ auth ;
@@ -45,9 +44,9 @@ class WebPush
45
44
* @param array $auth Some servers needs authentication
46
45
* @param array $defaultOptions TTL, urgency, topic
47
46
* @param int|null $timeout Timeout of POST request
48
- * @param AbstractClient|null $client
47
+ * @param array $clientOptions
49
48
*/
50
- public function __construct (array $ auth = array (), $ defaultOptions = array (), $ timeout = 30 , AbstractClient $ client = null )
49
+ public function __construct (array $ auth = array (), $ defaultOptions = array (), $ timeout = 30 , $ clientOptions = array () )
51
50
{
52
51
if (array_key_exists ('VAPID ' , $ auth )) {
53
52
$ auth ['VAPID ' ] = VAPID ::validate ($ auth ['VAPID ' ]);
@@ -57,9 +56,10 @@ public function __construct(array $auth = array(), $defaultOptions = array(), $t
57
56
58
57
$ this ->setDefaultOptions ($ defaultOptions );
59
58
60
- $ client = isset ($ client ) ? $ client : new MultiCurl ();
61
- $ client ->setTimeout ($ timeout );
62
- $ this ->browser = new Browser ($ client );
59
+ if (!array_key_exists ('timeout ' , $ clientOptions ) && isset ($ timeout )) {
60
+ $ clientOptions ['timeout ' ] = $ timeout ;
61
+ }
62
+ $ this ->client = new Client ($ clientOptions );
63
63
64
64
$ this ->nativePayloadEncryptionSupport = version_compare (phpversion (), '7.1 ' , '>= ' );
65
65
}
@@ -131,36 +131,36 @@ public function flush()
131
131
}
132
132
133
133
// for each endpoint server type
134
- $ responses = $ this ->prepareAndSend ($ this ->notifications );
135
-
136
- // if multi curl, flush
137
- if ($ this ->browser ->getClient () instanceof MultiCurl) {
138
- /** @var MultiCurl $multiCurl */
139
- $ multiCurl = $ this ->browser ->getClient ();
140
- $ multiCurl ->flush ();
134
+ $ requests = $ this ->prepare ($ this ->notifications );
135
+ $ promises = [];
136
+ foreach ($ requests as $ request ) {
137
+ $ promises [] = $ this ->client ->sendAsync ($ request );
141
138
}
139
+ $ results = Promise \settle ($ promises )->wait ();
142
140
143
- /** @var Response|null $response */
144
141
$ return = array ();
145
142
$ completeSuccess = true ;
146
- foreach ($ responses as $ i => $ response ) {
147
- if (!isset ($ response )) {
148
- $ return [] = array (
149
- 'success ' => false ,
150
- 'endpoint ' => $ this ->notifications [$ i ]->getEndpoint (),
151
- );
143
+ foreach ($ results as $ result ) {
144
+ if ($ result ['state ' ] === "rejected " ) {
145
+ /** @var RequestException $reason **/
146
+ $ reason = $ result ['reason ' ];
152
147
153
- $ completeSuccess = false ;
154
- } elseif (!$ response ->isSuccessful ()) {
155
- $ return [] = array (
148
+ $ error = array (
156
149
'success ' => false ,
157
- 'endpoint ' => $ this ->notifications [$ i ]->getEndpoint (),
158
- 'statusCode ' => $ response ->getStatusCode (),
159
- 'headers ' => $ response ->getHeaders (),
160
- 'content ' => $ response ->getContent (),
161
- 'expired ' => in_array ($ response ->getStatusCode (), array (400 , 404 , 410 )),
150
+ 'endpoint ' => "" .$ reason ->getRequest ()->getUri (),
151
+ 'message ' => $ reason ->getMessage (),
162
152
);
163
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
164
$ completeSuccess = false ;
165
165
} else {
166
166
$ return [] = array (
@@ -175,9 +175,9 @@ public function flush()
175
175
return $ completeSuccess ? true : $ return ;
176
176
}
177
177
178
- private function prepareAndSend (array $ notifications )
178
+ private function prepare (array $ notifications )
179
179
{
180
- $ responses = array ();
180
+ $ requests = array ();
181
181
/** @var Notification $notification */
182
182
foreach ($ notifications as $ notification ) {
183
183
$ endpoint = $ notification ->getEndpoint ();
@@ -247,48 +247,10 @@ private function prepareAndSend(array $notifications)
247
247
}
248
248
}
249
249
250
- $ responses [] = $ this -> sendRequest ( $ endpoint , $ headers , $ content );
250
+ $ requests [] = new Request ( ' POST ' , $ endpoint , $ headers , $ content );
251
251
}
252
252
253
- return $ responses ;
254
- }
255
-
256
- /**
257
- * @param string $url
258
- * @param array $headers
259
- * @param string $content
260
- *
261
- * @return \Buzz\Message\MessageInterface|null
262
- */
263
- private function sendRequest ($ url , array $ headers , $ content )
264
- {
265
- try {
266
- $ response = $ this ->browser ->post ($ url , $ headers , $ content );
267
- } catch (RequestException $ e ) {
268
- $ response = null ;
269
- }
270
-
271
- return $ response ;
272
- }
273
-
274
- /**
275
- * @return Browser
276
- */
277
- public function getBrowser ()
278
- {
279
- return $ this ->browser ;
280
- }
281
-
282
- /**
283
- * @param Browser $browser
284
- *
285
- * @return WebPush
286
- */
287
- public function setBrowser ($ browser )
288
- {
289
- $ this ->browser = $ browser ;
290
-
291
- return $ this ;
253
+ return $ requests ;
292
254
}
293
255
294
256
/**
0 commit comments