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, ], ); } }