2026-05-19 11:16:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Social;
|
|
|
|
|
|
|
|
|
|
use App\Enums\SocialAccount\Platform;
|
|
|
|
|
use App\Exceptions\PlatformUnavailableException;
|
|
|
|
|
use App\Exceptions\TokenExpiredException;
|
|
|
|
|
use Closure;
|
|
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
|
|
|
use Illuminate\Http\Client\Response;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalizes the failure modes of an OAuth token-refresh request:
|
|
|
|
|
*
|
|
|
|
|
* - ConnectionException (timeout / DNS / refused) → PlatformUnavailableException
|
|
|
|
|
* - HTTP 5xx → PlatformUnavailableException
|
|
|
|
|
* - HTTP 4xx → TokenExpiredException
|
|
|
|
|
*
|
|
|
|
|
* Callers configure the actual HTTP call through the closure passed to
|
|
|
|
|
* `send()`, so platform-specific quirks (form vs JSON body, auth headers,
|
|
|
|
|
* basic auth, etc.) stay where they belong — in the per-platform refresh
|
|
|
|
|
* method — while the failure semantics are uniform across providers.
|
|
|
|
|
*/
|
|
|
|
|
class TokenRefreshClient
|
|
|
|
|
{
|
|
|
|
|
public function __construct(public readonly Platform $platform) {}
|
|
|
|
|
|
|
|
|
|
public static function for(Platform $platform): self
|
|
|
|
|
{
|
|
|
|
|
return new self($platform);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Closure():Response $request
|
|
|
|
|
*
|
|
|
|
|
* @throws PlatformUnavailableException
|
|
|
|
|
* @throws TokenExpiredException
|
|
|
|
|
*/
|
|
|
|
|
public function send(Closure $request): Response
|
|
|
|
|
{
|
|
|
|
|
$name = $this->platform->label();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$response = $request();
|
|
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
|
throw new PlatformUnavailableException("{$name} API unreachable: {$e->getMessage()}");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 12:18:58 +00:00
|
|
|
if ($response->serverError() || $response->status() === 429) {
|
2026-05-19 11:16:43 +00:00
|
|
|
throw new PlatformUnavailableException(
|
|
|
|
|
"{$name} API returned {$response->status()} during token refresh",
|
|
|
|
|
$response->status(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($response->failed()) {
|
|
|
|
|
Log::error("TokenRefreshClient: {$name} token refresh failed", [
|
2026-05-19 12:21:43 +00:00
|
|
|
'body' => TokenRedactor::redact($response->body()),
|
2026-05-19 11:16:43 +00:00
|
|
|
]);
|
fix(social): publish flow honors PlatformUnavailable too
The previous commits in this PR closed the loophole on the hourly /
daily token-refresh jobs. The same loophole remained on the publish
path: every per-platform publisher (LinkedIn, X, YouTube, TikTok,
Threads, Instagram, Pinterest, Bluesky and their Analytics siblings)
has its own refreshToken() called before publishing a scheduled post,
and all of those treated any non-2xx as TokenExpired — including 5xx.
Result before this commit: a Bluesky outage that coincided with a
scheduled publish would mark the account as expired and fail the post.
Changes:
- Route every refreshToken() in the 16 publisher / analytics classes
through TokenRefreshClient::for(Platform::X)->send(...).
- TokenRefreshClient now also fills platformErrorCode from the HTTP
status and pulls error_description / error.message from the JSON
body, preserving the richer info LinkedIn / X / TikTok / Pinterest /
Threads used to put on their TokenExpiredException.
- PublishToSocialPlatform catches PlatformUnavailableException
explicitly: the post is marked failed (category: platform_unavailable,
with http_status in error_context) but the account stays Connected.
No retry inside this job — the scheduler reattempts the next run.
Test added: publish flow does NOT mark account expired when the
publisher throws PlatformUnavailable. Full suite: 1569 passing.
2026-05-19 12:01:02 +00:00
|
|
|
|
|
|
|
|
$body = $response->json();
|
|
|
|
|
$message = data_get($body, 'error_description')
|
|
|
|
|
?? data_get($body, 'error.message')
|
|
|
|
|
?? "Failed to refresh {$name} token";
|
|
|
|
|
|
|
|
|
|
throw new TokenExpiredException($message, platformErrorCode: (string) $response->status());
|
2026-05-19 11:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
}
|