Files
Verde-Web/app/Services/Sms/SemaphoreSmsService.php
admin dc297d4cd7 feat(backend): complete Module 1 auth (register/login/OTP/reset)
Closes Module 1: 9 auth endpoints under /api/v1/auth, OTP via SMS
(Semaphore + log + fake drivers), role middleware, role + admin seeders,
27 feature tests passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 21:46:53 +08:00

52 lines
1.6 KiB
PHP

<?php
namespace App\Services\Sms;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class SemaphoreSmsService implements SmsService
{
public function __construct(
private readonly string $apiKey,
private readonly string $senderName,
private readonly string $endpoint = 'https://api.semaphore.co/api/v4/messages',
) {}
public function send(string $to, string $message): SmsResult
{
try {
$response = Http::asForm()
->timeout(10)
->post($this->endpoint, [
'apikey' => $this->apiKey,
'number' => $to,
'message' => $message,
'sendername' => $this->senderName,
]);
} catch (ConnectionException $e) {
Log::warning('Semaphore SMS connection failed', ['to' => $to, 'error' => $e->getMessage()]);
return SmsResult::failure('connection_failed');
}
if (! $response->successful()) {
Log::warning('Semaphore SMS HTTP error', [
'to' => $to,
'status' => $response->status(),
'body' => $response->body(),
]);
return SmsResult::failure('provider_error_'.$response->status());
}
$payload = $response->json();
$providerId = is_array($payload) && isset($payload[0]['message_id'])
? (string) $payload[0]['message_id']
: null;
return SmsResult::success($providerId);
}
}