Skip to content

Commit 761adf3

Browse files
authored
fix: Exception type mismatch in rejected callback (#410)
Fixes #409
1 parent 469b4d9 commit 761adf3

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/WebPush.php

+22-16
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use Base64Url\Base64Url;
1717
use GuzzleHttp\Client;
1818
use GuzzleHttp\Pool;
19+
use GuzzleHttp\Exception\ConnectException;
1920
use GuzzleHttp\Exception\RequestException;
2021
use GuzzleHttp\Psr7\Request;
22+
use Psr\Http\Message\RequestInterface;
2123
use Psr\Http\Message\ResponseInterface;
2224

2325
class WebPush
@@ -152,17 +154,11 @@ public function flush(?int $batchSize = null): \Generator
152154
foreach ($requests as $request) {
153155
$promises[] = $this->client->sendAsync($request)
154156
->then(function ($response) use ($request) {
155-
/** @var ResponseInterface $response * */
157+
/** @var ResponseInterface $response **/
156158
return new MessageSentReport($request, $response);
157159
})
158160
->otherwise(function ($reason) {
159-
/** @var RequestException $reason **/
160-
if (method_exists($reason, 'getResponse')) {
161-
$response = $reason->getResponse();
162-
} else {
163-
$response = null;
164-
}
165-
return new MessageSentReport($reason->getRequest(), $response, false, $reason->getMessage());
161+
return $this->createRejectedReport($reason);
166162
});
167163
}
168164

@@ -205,17 +201,12 @@ public function flushPooled($callback, ?int $batchSize = null, ?int $requestConc
205201
$pool = new Pool($this->client, $batch, [
206202
'requestConcurrency' => $requestConcurrency,
207203
'fulfilled' => function (ResponseInterface $response, int $index) use ($callback, $batch) {
208-
/** @var \Psr\Http\Message\RequestInterface $request **/
204+
/** @var RequestInterface $request **/
209205
$request = $batch[$index];
210206
$callback(new MessageSentReport($request, $response));
211207
},
212-
'rejected' => function (RequestException $reason) use ($callback) {
213-
if (method_exists($reason, 'getResponse')) {
214-
$response = $reason->getResponse();
215-
} else {
216-
$response = null;
217-
}
218-
$callback(new MessageSentReport($reason->getRequest(), $response, false, $reason->getMessage()));
208+
'rejected' => function ($reason) use ($callback) {
209+
$callback($this->createRejectedReport($reason));
219210
},
220211
]);
221212

@@ -228,6 +219,21 @@ public function flushPooled($callback, ?int $batchSize = null, ?int $requestConc
228219
}
229220
}
230221

222+
/**
223+
* @param RequestException|ConnectException $reason
224+
* @return MessageSentReport
225+
*/
226+
protected function createRejectedReport($reason): MessageSentReport
227+
{
228+
if ($reason instanceof RequestException) {
229+
$response = $reason->getResponse();
230+
} else {
231+
$response = null;
232+
}
233+
234+
return new MessageSentReport($reason->getRequest(), $response, false, $reason->getMessage());
235+
}
236+
231237
/**
232238
* @throws \ErrorException|\Random\RandomException
233239
*/

0 commit comments

Comments
 (0)