add sms sending feat
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Shared\Sms;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Thin wrapper around the sms_poh gateway (the only provider configured
|
||||
* today, config('services.sms')). No-ops when SMS is disabled so callers
|
||||
* (queued listeners) can call send() unconditionally in every environment.
|
||||
*/
|
||||
class SmsService
|
||||
{
|
||||
private readonly bool $enabled;
|
||||
|
||||
private readonly ?string $server;
|
||||
|
||||
private readonly ?string $token;
|
||||
|
||||
private readonly ?string $sender;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed>|null $config
|
||||
*/
|
||||
public function __construct(?array $config = null)
|
||||
{
|
||||
$config ??= (array) config('services.sms');
|
||||
$providerConfig = (array) ($config['sms_poh'] ?? []);
|
||||
|
||||
$this->enabled = (bool) ($config['enabled'] ?? false);
|
||||
$this->server = $providerConfig['server'] ?? null;
|
||||
$this->token = $providerConfig['token'] ?? null;
|
||||
$this->sender = $providerConfig['sender'] ?? null;
|
||||
}
|
||||
|
||||
public function send(string $to, string $message, ?string $from = null): bool
|
||||
{
|
||||
if (! $this->enabled || $this->server === null || $this->token === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::withToken($this->token)
|
||||
->post($this->server, [
|
||||
'to' => $to,
|
||||
'message' => $message,
|
||||
'from' => $from ?? $this->sender,
|
||||
]);
|
||||
|
||||
Log::notice('Send SMS Response : '.$to.' '.$response->body());
|
||||
|
||||
return $response->successful();
|
||||
} catch (ConnectionException $exception) {
|
||||
Log::error('Send SMS Error : '.$to.' '.$exception->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user