Files
ladill-transfer/app/Services/Identity/PublicIdUserResolver.php
T
isaaccladandCursor ccffa1433d
Deploy Ladill Transfer / deploy (push) Successful in 28s
Route SMS voice uploads through Transfer with wallet billing.
Add a service API for the Ladill SMS platform to store voice recordings in Transfer and charge monthly storage from the customer wallet.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-20 02:23:18 +00:00

58 lines
1.6 KiB
PHP

<?php
namespace App\Services\Identity;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use RuntimeException;
/** Resolves a Ladill Transfer user mirror from a platform account public_id. */
class PublicIdUserResolver
{
public function resolve(string $publicId): User
{
$publicId = trim($publicId);
if ($publicId === '') {
throw new RuntimeException('User public_id is required.');
}
$existing = User::query()->where('public_id', $publicId)->first();
if ($existing) {
return $existing;
}
$base = rtrim((string) config('identity.api_url'), '/');
$key = (string) config('identity.api_key');
if ($base === '' || $key === '') {
throw new RuntimeException('Identity API is not configured.');
}
$res = Http::withToken($key)
->acceptJson()
->timeout(10)
->get("{$base}/identity/profile", ['user' => $publicId]);
if ($res->failed()) {
throw new RuntimeException('Could not resolve the Ladill account.');
}
$data = $res->json('data');
if (! is_array($data)) {
throw new RuntimeException('Could not resolve the Ladill account.');
}
$email = trim((string) ($data['email'] ?? ''));
if ($email === '') {
$email = 'sms+'.$publicId.'@accounts.ladill.local';
}
return User::updateOrCreate(
['public_id' => $publicId],
[
'name' => trim((string) ($data['name'] ?? '')) ?: 'Ladill user',
'email' => $email,
],
);
}
}