-
-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathGuzzleApiClientHandler.php
More file actions
99 lines (80 loc) · 2.93 KB
/
GuzzleApiClientHandler.php
File metadata and controls
99 lines (80 loc) · 2.93 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Auth\CreateActionLink;
use Beste\Json;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;
use InvalidArgumentException;
use Kreait\Firebase\Auth\CreateActionLink;
use Kreait\Firebase\Auth\ProjectAwareAuthResourceUrlBuilder;
use Kreait\Firebase\Auth\TenantAwareAuthResourceUrlBuilder;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use function array_filter;
use const JSON_FORCE_OBJECT;
/**
* @internal
*/
final readonly class GuzzleApiClientHandler
{
/**
* @param non-empty-string $projectId
*/
public function __construct(
private ClientInterface $client,
private string $projectId,
) {
}
public function handle(CreateActionLink $action): string
{
$request = $this->createRequest($action);
try {
$response = $this->client->send($request, ['http_errors' => false]);
} catch (ClientExceptionInterface $e) {
throw new FailedToCreateActionLink(
message: 'Failed to create action link: '.$e->getMessage(),
previous: $e
);
}
if ($response->getStatusCode() !== 200) {
throw FailedToCreateActionLink::withActionAndResponse($action, $response);
}
try {
$data = Json::decode((string) $response->getBody(), true);
} catch (InvalidArgumentException $e) {
throw new FailedToCreateActionLink(
message: 'Unable to parse the response data: '.$e->getMessage(),
previous: $e
);
}
$actionCode = $data['oobLink'] ?? null;
if (!is_scalar($actionCode)) {
throw new FailedToCreateActionLink('The response did not contain an action link');
}
return (string) $actionCode;
}
private function createRequest(CreateActionLink $action): RequestInterface
{
$data = [
'requestType' => $action->type(),
'email' => $action->email(),
'returnOobLink' => true,
...$action->settings()->toArray(),
];
$tenantId = $action->tenantId();
if (is_string($tenantId) && $tenantId !== '') {
$urlBuilder = TenantAwareAuthResourceUrlBuilder::forProjectAndTenant($this->projectId, $tenantId);
} else {
$urlBuilder = ProjectAwareAuthResourceUrlBuilder::forProject($this->projectId);
}
$url = $urlBuilder->getUrl('/accounts:sendOobCode');
$body = Utils::streamFor(Json::encode($data, JSON_FORCE_OBJECT));
$headers = array_filter([
'Content-Type' => 'application/json; charset=UTF-8',
'Content-Length' => (string) $body->getSize(),
'X-Firebase-Locale' => $action->locale(),
], fn(?string $value): bool => $value !== null);
return new Request('POST', $url, $headers, $body);
}
}