|
3 | 3 |
|
4 | 4 | namespace Nipwaayoni\SnsHandler\Listeners; |
5 | 5 |
|
6 | | -use Illuminate\Support\Facades\Http; |
| 6 | +use Http\Discovery\HttpClientDiscovery; |
| 7 | +use Http\Discovery\Psr17FactoryDiscovery; |
7 | 8 | use Illuminate\Support\Facades\Log; |
8 | 9 | use Nipwaayoni\SnsHandler\Events\SnsConfirmationRequestReceived; |
9 | 10 | use Nipwaayoni\SnsHandler\SnsConfirmSubscriptionException; |
| 11 | +use Nipwaayoni\SnsHandler\SnsException; |
| 12 | +use Nipwaayoni\SnsHandler\SnsMessage; |
| 13 | +use Psr\Http\Client\ClientExceptionInterface; |
| 14 | +use Psr\Http\Client\ClientInterface; |
| 15 | +use Psr\Http\Message\RequestFactoryInterface; |
| 16 | +use Psr\Http\Message\ResponseInterface; |
10 | 17 |
|
11 | 18 | class SnsConfirmationRequestListener |
12 | 19 | { |
| 20 | + /** |
| 21 | + * @var ClientInterface |
| 22 | + */ |
| 23 | + private $client; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var RequestFactoryInterface |
| 27 | + */ |
| 28 | + private $requestFactory; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param ClientInterface|null $client |
| 32 | + * @param RequestFactoryInterface|null $requestFactory |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + ClientInterface $client = null, |
| 36 | + RequestFactoryInterface $requestFactory = null |
| 37 | + ) { |
| 38 | + $this->client = $client ?? HttpClientDiscovery::find(); |
| 39 | + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); |
| 40 | + } |
| 41 | + |
13 | 42 | public function handle(SnsConfirmationRequestReceived $event) |
14 | 43 | { |
15 | 44 | $message = $event->message(); |
16 | | - //TODO Make this work with Laravel 6, as the Http facade was introduced in Laravel 7 |
17 | | - $response = Http::get($message->subscribeUrl()); |
18 | | - if ($response->successful()) { |
19 | | - $info = sprintf('Subscription confirmation for %s succeeded with status %s', $message->topicArn(), $response->status()); |
20 | | - Log::info($info); |
21 | | - return; |
| 45 | + |
| 46 | + $response = $this->getResponse($message); |
| 47 | + |
| 48 | + Log::info(sprintf('Subscription confirmation for %s succeeded with status %s', $message->topicArn(), $response->getStatusCode())); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param SnsMessage $message |
| 53 | + * @return ResponseInterface |
| 54 | + * @throws SnsConfirmSubscriptionException |
| 55 | + * @throws SnsException |
| 56 | + */ |
| 57 | + private function getResponse(SnsMessage $message): ResponseInterface |
| 58 | + { |
| 59 | + try { |
| 60 | + return $this->client->sendRequest( |
| 61 | + $this->requestFactory->createRequest('GET', $message->subscribeUrl()) |
| 62 | + ); |
| 63 | + } catch (ClientExceptionInterface $e) { |
| 64 | + throw new SnsConfirmSubscriptionException( |
| 65 | + sprintf('Subscription confirmation for %s failed with status %s', $message->topicArn(), $e->getCode()) |
| 66 | + ); |
22 | 67 | } |
23 | | - $error = sprintf('Subscription confirmation for %s failed with status %s', $message->topicArn(), $response->status()); |
24 | | - Log::error($error); |
25 | | - throw new SnsConfirmSubscriptionException($error); |
26 | 68 | } |
27 | 69 | } |
0 commit comments