|
17 | 17 | use GuzzleHttp\Client;
|
18 | 18 | use GuzzleHttp\Exception\RequestException;
|
19 | 19 | use GuzzleHttp\Psr7\Request;
|
20 |
| -use GuzzleHttp\Promise; |
| 20 | +use Psr\Http\Message\ResponseInterface; |
21 | 21 |
|
22 | 22 | class WebPush
|
23 | 23 | {
|
@@ -121,93 +121,53 @@ public function sendNotification(Subscription $subscription, ?string $payload =
|
121 | 121 |
|
122 | 122 | $this->notifications[] = new Notification($subscription, $payload, $options, $auth);
|
123 | 123 |
|
124 |
| - if ($flush) { |
125 |
| - $res = $this->flush(); |
126 |
| - |
127 |
| - // if there has been a problem with at least one notification |
128 |
| - if (is_array($res)) { |
129 |
| - // if there was only one notification, return the information directly |
130 |
| - if (count($res) === 1) { |
131 |
| - return $res[0]; |
132 |
| - } |
133 |
| - |
134 |
| - return $res; |
135 |
| - } |
136 |
| - |
137 |
| - return true; |
138 |
| - } |
139 |
| - |
140 |
| - return true; |
| 124 | + return false !== $flush ? $this->flush() : true; |
141 | 125 | }
|
142 | 126 |
|
143 |
| - /** |
144 |
| - * Flush notifications. Triggers the requests. |
145 |
| - * |
146 |
| - * @param null|int $batchSize Defaults the value defined in defaultOptions during instantiation (which defaults to 1000). |
147 |
| - * |
148 |
| - * @return array|bool If there are no errors, return true. |
149 |
| - * If there were no notifications in the queue, return false. |
150 |
| - * Else return an array of information for each notification sent (success, statusCode, headers, content) |
151 |
| - * |
152 |
| - * @throws \ErrorException |
153 |
| - */ |
154 |
| - public function flush(?int $batchSize = null) |
| 127 | + /** |
| 128 | + * Flush notifications. Triggers the requests. |
| 129 | + * |
| 130 | + * @param null|int $batchSize Defaults the value defined in defaultOptions during instantiation (which defaults to 1000). |
| 131 | + * |
| 132 | + * @return iterable |
| 133 | + * @throws \ErrorException |
| 134 | + */ |
| 135 | + public function flush(?int $batchSize = null) : iterable |
155 | 136 | {
|
156 | 137 | if (empty($this->notifications)) {
|
157 |
| - return false; |
| 138 | + yield from []; |
158 | 139 | }
|
159 | 140 |
|
160 | 141 | if (null === $batchSize) {
|
161 | 142 | $batchSize = $this->defaultOptions['batchSize'];
|
162 | 143 | }
|
163 | 144 |
|
164 | 145 | $batches = array_chunk($this->notifications, $batchSize);
|
165 |
| - $return = []; |
166 |
| - $completeSuccess = true; |
167 |
| - foreach ($batches as $batch) { |
168 |
| - // for each endpoint server type |
169 |
| - $requests = $this->prepare($batch); |
170 |
| - $promises = []; |
171 |
| - foreach ($requests as $request) { |
172 |
| - $promises[] = $this->client->sendAsync($request); |
173 |
| - } |
174 |
| - $results = Promise\settle($promises)->wait(); |
175 |
| - |
176 |
| - foreach ($results as $result) { |
177 |
| - if ($result['state'] === "rejected") { |
178 |
| - /** @var RequestException $reason **/ |
179 |
| - $reason = $result['reason']; |
180 |
| - |
181 |
| - $error = [ |
182 |
| - 'success' => false, |
183 |
| - 'endpoint' => $reason->getRequest()->getUri(), |
184 |
| - 'message' => $reason->getMessage(), |
185 |
| - ]; |
186 |
| - |
187 |
| - $response = $reason->getResponse(); |
188 |
| - if ($response !== null) { |
189 |
| - $statusCode = $response->getStatusCode(); |
190 |
| - $error['statusCode'] = $statusCode; |
191 |
| - $error['reasonPhrase'] = $response->getReasonPhrase(); |
192 |
| - $error['expired'] = in_array($statusCode, [404, 410]); |
193 |
| - $error['content'] = $response->getBody(); |
194 |
| - $error['headers'] = $response->getHeaders(); |
195 |
| - } |
196 | 146 |
|
197 |
| - $return[] = $error; |
198 |
| - $completeSuccess = false; |
199 |
| - } else { |
200 |
| - $return[] = [ |
201 |
| - 'success' => true, |
202 |
| - ]; |
203 |
| - } |
204 |
| - } |
205 |
| - } |
206 |
| - |
207 |
| - // reset queue |
208 |
| - $this->notifications = null; |
| 147 | + // reset queue |
| 148 | + $this->notifications = []; |
209 | 149 |
|
210 |
| - return $completeSuccess ? true : $return; |
| 150 | + foreach ($batches as $batch) { |
| 151 | + // for each endpoint server type |
| 152 | + $requests = $this->prepare($batch); |
| 153 | + |
| 154 | + foreach ($requests as $request) { |
| 155 | + $result = null; |
| 156 | + |
| 157 | + $this->client->sendAsync($request) |
| 158 | + ->then(function ($response) use ($request, &$result) { |
| 159 | + /** @var ResponseInterface $response * */ |
| 160 | + $result = new MessageSentReport($request, $response); |
| 161 | + }) |
| 162 | + ->otherwise(function ($reason) use (&$result) { |
| 163 | + /** @var RequestException $reason **/ |
| 164 | + $result = new MessageSentReport($reason->getRequest(), $reason->getResponse(), false, $reason->getMessage()); |
| 165 | + }) |
| 166 | + ->wait(false); |
| 167 | + |
| 168 | + yield $result; |
| 169 | + } |
| 170 | + } |
211 | 171 | }
|
212 | 172 |
|
213 | 173 | /**
|
|
0 commit comments