-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
ConnectionRepository.php
41 lines (34 loc) · 1.42 KB
/
ConnectionRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace Georgeboot\LaravelEchoApiGateway;
use Aws\ApiGatewayManagementApi\ApiGatewayManagementApiClient;
use Aws\ApiGatewayManagementApi\Exception\ApiGatewayManagementApiException;
use GuzzleHttp\Exception\ClientException;
class ConnectionRepository
{
protected ApiGatewayManagementApiClient $apiGatewayManagementApiClient;
public function __construct(
protected SubscriptionRepository $subscriptionRepository,
array $config
) {
$this->apiGatewayManagementApiClient = new ApiGatewayManagementApiClient(array_merge($config['connection'], [
'version' => '2018-11-29',
'endpoint' => "https://{$config['api']['id']}.execute-api.{$config['connection']['region']}.amazonaws.com/{$config['api']['stage']}/",
]));
}
public function sendMessage(string $connectionId, string $data): void
{
try {
$this->apiGatewayManagementApiClient->postToConnection([
'ConnectionId' => $connectionId,
'Data' => $data,
]);
} catch (ClientException $guzzleClientException) {
// GoneException: The connection with the provided id no longer exists.
if ($guzzleClientException->getResponse()->getStatusCode() === 410) {
$this->subscriptionRepository->clearConnection($connectionId);
return;
}
throw $guzzleClientException;
}
}
}