trypost/app/Services/Social/Concerns/HasSocialHttpClient.php
Paulo Castellano cf80e1fcae refactor(social): single source of truth for token redaction
Same shape of bug as the refreshToken duplication: the regex that strips
access_token / Bearer headers from logged HTTP bodies existed in three
near-identical copies (HasSocialHttpClient trait, TokenRefreshClient,
SocialPublishException), drifting subtly — SocialPublishException was
missing the JSON "token" pattern.

Extracts a TokenRedactor::redact(string) helper and routes all callers
through it. Adding a new token format now means one regex in one file.
2026-05-19 09:21:43 -03:00

44 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Social\Concerns;
use App\Models\PostPlatform;
use App\Services\Social\TokenRedactor;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
trait HasSocialHttpClient
{
protected function validateContentLength(PostPlatform $postPlatform): void
{
$content = $postPlatform->post->content ?? '';
if ($postPlatform->platform->contentOverflow($content) === 0) {
return;
}
$maxLength = $postPlatform->platform->maxContentLength();
$contentLength = mb_strlen($content);
throw new \Exception(
"Content exceeds {$postPlatform->platform->label()} limit of {$maxLength} characters ({$contentLength} provided)."
);
}
protected function socialHttp(): PendingRequest
{
return Http::retry(
times: 3,
sleepMilliseconds: 5000,
when: fn ($exception, $request) => $exception->response?->status() === 429,
throw: false,
)->timeout(120);
}
protected function redactResponseBody(string $body): string
{
return TokenRedactor::redact($body);
}
}