@@ -27,13 +27,8 @@ class WebPush
27
27
/** @var array Key is push server type and value is the API key */
28
28
protected $ apiKeys ;
29
29
30
- /** @var array Array of array of Notifications by server type */
31
- private $ notificationsByServerType ;
32
-
33
- /** @var array Array of not standard endpoint sources */
34
- private $ urlByServerType = array (
35
- 'GCM ' => self ::GCM_URL ,
36
- );
30
+ /** @var array Array of array of Notifications */
31
+ private $ notifications ;
37
32
38
33
/** @var array Default options : TTL, urgency, topic */
39
34
private $ defaultOptions ;
@@ -87,9 +82,7 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
87
82
$ payload = Encryption::padPayload ($ payload , $ this ->automaticPadding );
88
83
}
89
84
90
- // sort notification by server type
91
- $ type = $ this ->sortEndpoint ($ endpoint );
92
- $ this ->notificationsByServerType [$ type ][] = new Notification ($ endpoint , $ payload , $ userPublicKey , $ userAuthToken , $ options );
85
+ $ this ->notifications [] = new Notification ($ endpoint , $ payload , $ userPublicKey , $ userAuthToken , $ options );
93
86
94
87
if ($ flush ) {
95
88
$ res = $ this ->flush ();
@@ -121,22 +114,12 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
121
114
*/
122
115
public function flush ()
123
116
{
124
- if (empty ($ this ->notificationsByServerType )) {
117
+ if (empty ($ this ->notifications )) {
125
118
return false ;
126
119
}
127
120
128
- // if GCM is present, we should check for the API key
129
- if (array_key_exists ('GCM ' , $ this ->notificationsByServerType )) {
130
- if (empty ($ this ->apiKeys ['GCM ' ])) {
131
- throw new \ErrorException ('No GCM API Key specified. ' );
132
- }
133
- }
134
-
135
121
// for each endpoint server type
136
- $ responses = array ();
137
- foreach ($ this ->notificationsByServerType as $ serverType => $ notifications ) {
138
- $ responses = array_merge ($ responses , $ this ->prepareAndSend ($ notifications , $ serverType ));
139
- }
122
+ $ responses = $ this ->prepareAndSend ($ this ->notifications );
140
123
141
124
// if multi curl, flush
142
125
if ($ this ->browser ->getClient () instanceof MultiCurl) {
@@ -148,20 +131,22 @@ public function flush()
148
131
/** @var Response|null $response */
149
132
$ return = array ();
150
133
$ completeSuccess = true ;
151
- foreach ($ responses as $ response ) {
134
+ foreach ($ responses as $ i => $ response ) {
152
135
if (!isset ($ response )) {
153
136
$ return [] = array (
154
137
'success ' => false ,
138
+ 'endpoint ' => $ this ->notifications [$ i ]->getEndpoint (),
155
139
);
156
140
157
141
$ completeSuccess = false ;
158
142
} elseif (!$ response ->isSuccessful ()) {
159
143
$ return [] = array (
160
144
'success ' => false ,
145
+ 'endpoint ' => $ this ->notifications [$ i ]->getEndpoint (),
161
146
'statusCode ' => $ response ->getStatusCode (),
162
147
'headers ' => $ response ->getHeaders (),
163
148
'content ' => $ response ->getContent (),
164
- 'expired ' => in_array ($ response ->getStatusCode (), array (404 , 410 )),
149
+ 'expired ' => in_array ($ response ->getStatusCode (), array (400 , 404 , 410 )),
165
150
);
166
151
167
152
$ completeSuccess = false ;
@@ -173,12 +158,12 @@ public function flush()
173
158
}
174
159
175
160
// reset queue
176
- $ this ->notificationsByServerType = null ;
161
+ $ this ->notifications = null ;
177
162
178
163
return $ completeSuccess ? true : $ return ;
179
164
}
180
165
181
- private function prepareAndSend (array $ notifications, $ serverType )
166
+ private function prepareAndSend (array $ notifications )
182
167
{
183
168
$ responses = array ();
184
169
/** @var Notification $notification */
@@ -219,8 +204,12 @@ private function prepareAndSend(array $notifications, $serverType)
219
204
$ headers ['Topic ' ] = $ options ['topic ' ];
220
205
}
221
206
222
- if ($ serverType === 'GCM ' ) {
223
- $ headers ['Authorization ' ] = 'key= ' .$ this ->apiKeys ['GCM ' ];
207
+ if (substr ($ endpoint , 0 , strlen (self ::GCM_URL )) === self ::GCM_URL ) {
208
+ if (array_key_exists ('GCM ' , $ this ->apiKeys )) {
209
+ $ headers ['Authorization ' ] = 'key= ' .$ this ->apiKeys ['GCM ' ];
210
+ } else {
211
+ throw new \ErrorException ('No GCM API Key specified. ' );
212
+ }
224
213
}
225
214
226
215
$ responses [] = $ this ->sendRequest ($ endpoint , $ headers , $ content );
@@ -247,22 +236,6 @@ private function sendRequest($url, array $headers, $content)
247
236
return $ response ;
248
237
}
249
238
250
- /**
251
- * @param string $endpoint
252
- *
253
- * @return string
254
- */
255
- private function sortEndpoint ($ endpoint )
256
- {
257
- foreach ($ this ->urlByServerType as $ type => $ url ) {
258
- if (substr ($ endpoint , 0 , strlen ($ url )) === $ url ) {
259
- return $ type ;
260
- }
261
- }
262
-
263
- return 'standard ' ;
264
- }
265
-
266
239
/**
267
240
* @return Browser
268
241
*/
0 commit comments