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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Modules\Shared\Sms\SmsService;
|
||||
|
||||
$config = [
|
||||
'enabled' => true,
|
||||
'sms_poh' => [
|
||||
'server' => 'https://sms.test/send',
|
||||
'token' => 'test-token',
|
||||
'sender' => 'FamousLY4',
|
||||
],
|
||||
];
|
||||
|
||||
test('send posts to the configured server with a bearer token and returns true on success', function () use ($config) {
|
||||
Http::fake(['sms.test/*' => Http::response(['status' => 'ok'])]);
|
||||
|
||||
$result = (new SmsService($config))->send('+959111222333', 'Your driver is here.');
|
||||
|
||||
expect($result)->toBeTrue();
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://sms.test/send'
|
||||
&& $request->hasHeader('Authorization', 'Bearer test-token')
|
||||
&& $request['to'] === '+959111222333'
|
||||
&& $request['message'] === 'Your driver is here.'
|
||||
&& $request['from'] === 'FamousLY4';
|
||||
});
|
||||
});
|
||||
|
||||
test('send returns false and does not call the gateway when disabled', function () use ($config) {
|
||||
Http::fake();
|
||||
$config['enabled'] = false;
|
||||
|
||||
$result = (new SmsService($config))->send('+959111222333', 'Your driver is here.');
|
||||
|
||||
expect($result)->toBeFalse();
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
|
||||
test('send returns false on a non-successful gateway response', function () use ($config) {
|
||||
Http::fake(['sms.test/*' => Http::response(['error' => 'invalid'], 422)]);
|
||||
|
||||
$result = (new SmsService($config))->send('+959111222333', 'Your driver is here.');
|
||||
|
||||
expect($result)->toBeFalse();
|
||||
});
|
||||
|
||||
test('send uses an explicit from over the configured sender', function () use ($config) {
|
||||
Http::fake(['sms.test/*' => Http::response(['status' => 'ok'])]);
|
||||
|
||||
(new SmsService($config))->send('+959111222333', 'Hello', 'OtherSender');
|
||||
|
||||
Http::assertSent(fn ($request) => $request['from'] === 'OtherSender');
|
||||
});
|
||||
Reference in New Issue
Block a user