Skip to content

Commit 58439c5

Browse files
committed
rm sorting, add 'endpoint' key in the (bad) results array, add 400 to expired codes
fix #36
1 parent f507a86 commit 58439c5

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ The `expired` key can be useful to clean your database of expired endpoints.
133133
$res = array(
134134
array( // first notification
135135
'success' => false,
136+
'endpoint' => $theEndpointToDeleteInYourDatabaseIfExpired
136137
'statusCode' => $responseStatusCode,
137138
'headers' => $responseHeaders,
138139
'content' => $responseContent, // you may have more infos here

src/WebPush.php

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ class WebPush
2727
/** @var array Key is push server type and value is the API key */
2828
protected $apiKeys;
2929

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;
3732

3833
/** @var array Default options : TTL, urgency, topic */
3934
private $defaultOptions;
@@ -87,9 +82,7 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
8782
$payload = Encryption::padPayload($payload, $this->automaticPadding);
8883
}
8984

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);
9386

9487
if ($flush) {
9588
$res = $this->flush();
@@ -121,22 +114,12 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
121114
*/
122115
public function flush()
123116
{
124-
if (empty($this->notificationsByServerType)) {
117+
if (empty($this->notifications)) {
125118
return false;
126119
}
127120

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-
135121
// 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);
140123

141124
// if multi curl, flush
142125
if ($this->browser->getClient() instanceof MultiCurl) {
@@ -148,20 +131,22 @@ public function flush()
148131
/** @var Response|null $response */
149132
$return = array();
150133
$completeSuccess = true;
151-
foreach ($responses as $response) {
134+
foreach ($responses as $i => $response) {
152135
if (!isset($response)) {
153136
$return[] = array(
154137
'success' => false,
138+
'endpoint' => $this->notifications[$i]->getEndpoint(),
155139
);
156140

157141
$completeSuccess = false;
158142
} elseif (!$response->isSuccessful()) {
159143
$return[] = array(
160144
'success' => false,
145+
'endpoint' => $this->notifications[$i]->getEndpoint(),
161146
'statusCode' => $response->getStatusCode(),
162147
'headers' => $response->getHeaders(),
163148
'content' => $response->getContent(),
164-
'expired' => in_array($response->getStatusCode(), array(404, 410)),
149+
'expired' => in_array($response->getStatusCode(), array(400, 404, 410)),
165150
);
166151

167152
$completeSuccess = false;
@@ -173,12 +158,12 @@ public function flush()
173158
}
174159

175160
// reset queue
176-
$this->notificationsByServerType = null;
161+
$this->notifications = null;
177162

178163
return $completeSuccess ? true : $return;
179164
}
180165

181-
private function prepareAndSend(array $notifications, $serverType)
166+
private function prepareAndSend(array $notifications)
182167
{
183168
$responses = array();
184169
/** @var Notification $notification */
@@ -219,8 +204,12 @@ private function prepareAndSend(array $notifications, $serverType)
219204
$headers['Topic'] = $options['topic'];
220205
}
221206

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+
}
224213
}
225214

226215
$responses[] = $this->sendRequest($endpoint, $headers, $content);
@@ -247,22 +236,6 @@ private function sendRequest($url, array $headers, $content)
247236
return $response;
248237
}
249238

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-
266239
/**
267240
* @return Browser
268241
*/

tests/WebPushTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,8 @@ public function testSendGCMNotificationWithWrongGCMApiKey()
136136
$this->assertEquals(400, $res['statusCode']);
137137

138138
$this->assertArrayHasKey('headers', $res);
139+
140+
$this->assertArrayHasKey('endpoint', $res);
141+
$this->assertEquals(self::$endpoints['GCM'], $res['endpoint']);
139142
}
140143
}

0 commit comments

Comments
 (0)