Package system sms

This commit is contained in:
Simon Pocrnjič
2025-10-26 12:57:09 +01:00
parent 266af6595e
commit 369af34ad4
29 changed files with 2639 additions and 330 deletions
+37 -10
View File
@@ -12,6 +12,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
class SendSmsJob implements ShouldQueue
{
@@ -49,16 +50,42 @@ public function handle(SmsService $sms): void
/** @var SmsSender|null $sender */
$sender = $this->senderId ? SmsSender::find($this->senderId) : null;
// Send and get log (handles queued->sent/failed transitions internally)
$log = $sms->sendRaw(
profile: $profile,
to: $this->to,
content: $this->content,
sender: $sender,
countryCode: $this->countryCode,
deliveryReport: $this->deliveryReport,
clientReference: $this->clientReference,
);
// Apply Redis throttle from config to avoid provider rate limits
$scope = config('services.sms.throttle.scope', 'global');
$provider = config('services.sms.throttle.provider_key', 'smsapi_si');
$allow = (int) config('services.sms.throttle.allow', 30);
$every = (int) config('services.sms.throttle.every', 60);
$jitter = (int) config('services.sms.throttle.jitter_seconds', 2);
$key = $scope === 'per_profile' && $profile ? "sms:{$provider}:{$profile->id}" : "sms:{$provider}";
$log = null;
try {
Redis::throttle($key)->allow($allow)->every($every)->then(function () use (&$log, $sms, $profile, $sender) {
// Send and get log (handles queued->sent/failed transitions internally)
$log = $sms->sendRaw(
profile: $profile,
to: $this->to,
content: $this->content,
sender: $sender,
countryCode: $this->countryCode,
deliveryReport: $this->deliveryReport,
clientReference: $this->clientReference,
);
}, function () use ($jitter) {
return $this->release(max(1, rand(1, $jitter)));
});
} catch (\Throwable $e) {
// Fallback if Redis is unavailable in test or local env
$log = $sms->sendRaw(
profile: $profile,
to: $this->to,
content: $this->content,
sender: $sender,
countryCode: $this->countryCode,
deliveryReport: $this->deliveryReport,
clientReference: $this->clientReference,
);
}
// If invoked from the case UI with a selected template, create an Activity
if ($this->templateId && $this->clientCaseId && $log) {
try {