fix: move local packages inside repo so remote server can install them
This commit is contained in:
1 parent
50e5f3e381
commit
30b2fb3958
40 files changed
+1671
-2
No files matched your search
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PromoSeven\AzureMailer\Graph;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use PromoSeven\AzureMailer\Exceptions\GraphApiException;
|
||||
|
||||
class GraphClient
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TokenManager $tokenManager,
|
||||
private readonly array $config
|
||||
) {}
|
||||
|
||||
public function send(array $payload): void
|
||||
{
|
||||
$url = $this->endpoint();
|
||||
|
||||
$response = Http::withToken($this->tokenManager->getToken())
|
||||
->timeout($this->config['timeout'] ?? 30)
|
||||
->post($url, $payload);
|
||||
|
||||
if ($response->status() === 401) {
|
||||
Log::warning('azure-mailer: 401 received, retrying with fresh token');
|
||||
$this->tokenManager->invalidate();
|
||||
|
||||
$response = Http::withToken($this->tokenManager->getToken())
|
||||
->timeout($this->config['timeout'] ?? 30)
|
||||
->post($url, $payload);
|
||||
}
|
||||
|
||||
if (! $response->successful()) {
|
||||
$error = $response->json('error', []);
|
||||
throw GraphApiException::fromResponse(
|
||||
$error['code'] ?? (string) $response->status(),
|
||||
$error['message'] ?? 'Unknown error'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function endpoint(): string
|
||||
{
|
||||
$version = $this->config['graph_api_version'] ?? 'v1.0';
|
||||
$from = $this->config['from_address'];
|
||||
|
||||
return "https://graph.microsoft.com/{$version}/users/{$from}/sendMail";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PromoSeven\AzureMailer\Graph;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PromoSeven\AzureMailer\Exceptions\AuthenticationException;
|
||||
|
||||
class TokenManager
|
||||
{
|
||||
public function __construct(private readonly array $config) {}
|
||||
|
||||
public function getToken(): string
|
||||
{
|
||||
$key = $this->cacheKey();
|
||||
|
||||
if ($token = Cache::get($key)) {
|
||||
return $token;
|
||||
}
|
||||
|
||||
return $this->fetchAndCache($key);
|
||||
}
|
||||
|
||||
public function invalidate(): void
|
||||
{
|
||||
Cache::forget($this->cacheKey());
|
||||
}
|
||||
|
||||
private function fetchAndCache(string $key): string
|
||||
{
|
||||
$response = Http::asForm()->post(
|
||||
"https://login.microsoftonline.com/{$this->config['tenant_id']}/oauth2/v2.0/token",
|
||||
[
|
||||
'grant_type' => 'client_credentials',
|
||||
'client_id' => $this->config['client_id'],
|
||||
'client_secret' => $this->config['client_secret'],
|
||||
'scope' => 'https://graph.microsoft.com/.default',
|
||||
]
|
||||
);
|
||||
|
||||
if (! $response->successful()) {
|
||||
$body = $response->json();
|
||||
throw AuthenticationException::fromResponse(
|
||||
$body['error'] ?? 'unknown_error',
|
||||
$body['error_description'] ?? 'No description provided'
|
||||
);
|
||||
}
|
||||
|
||||
$body = $response->json();
|
||||
$ttl = max(1, ($body['expires_in'] ?? 3600) - 60);
|
||||
|
||||
Cache::put($key, $body['access_token'], $ttl);
|
||||
|
||||
return $body['access_token'];
|
||||
}
|
||||
|
||||
private function cacheKey(): string
|
||||
{
|
||||
return 'azure_mailer_token_' . $this->config['client_id'];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user