You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.4 KiB
80 lines
2.4 KiB
|
1 month ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services\Sms;
|
||
|
|
|
||
|
|
use App\Models\AudienceRegistration;
|
||
|
|
use App\Support\SmsConfig;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
|
||
|
|
class AudienceRegistrationSmsNotifier
|
||
|
|
{
|
||
|
|
public function __construct(private readonly SubmailSmsClient $client)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
public function notify(AudienceRegistration $registration): void
|
||
|
|
{
|
||
|
|
if (! (bool) config('sms.enabled')) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if ((string) config('sms.driver') !== 'submail') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$mobile = trim((string) $registration->phone);
|
||
|
|
if (preg_match('/^1[3-9]\d{9}$/', $mobile) !== 1) {
|
||
|
|
Log::warning('sms.audience_registration skipped_invalid_mobile', [
|
||
|
|
'registration_id' => $registration->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
SmsConfig::assertReadyForRealSending();
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
Log::error('sms.audience_registration config_incomplete', [
|
||
|
|
'registration_id' => $registration->id,
|
||
|
|
'error' => $e->getMessage(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$registration->loadMissing('session');
|
||
|
|
$session = $registration->session;
|
||
|
|
$project = trim((string) config('sms.submail.audience_template_id'));
|
||
|
|
if ($project === '') {
|
||
|
|
Log::error('sms.audience_registration missing_template');
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$result = $this->client->sendTemplate($mobile, $project, [
|
||
|
|
'session' => $this->clip((string) ($session?->name ?? ''), 40),
|
||
|
|
'time' => $this->clip((string) ($session?->event_date_text ?? ''), 40),
|
||
|
|
'address' => $this->clip((string) ($session?->address ?? ''), 40),
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (! $result->success) {
|
||
|
|
Log::error('sms.audience_registration failed', [
|
||
|
|
'registration_id' => $registration->id,
|
||
|
|
'mobile_mask' => substr($mobile, 0, 3).'****'.substr($mobile, -4),
|
||
|
|
'provider_code' => $result->providerCode,
|
||
|
|
'provider_message' => $result->providerMessage,
|
||
|
|
'provider_request_id' => $result->requestId,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private function clip(string $value, int $max): string
|
||
|
|
{
|
||
|
|
$value = trim($value);
|
||
|
|
if (mb_strlen($value) <= $max) {
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
return mb_substr($value, 0, $max);
|
||
|
|
}
|
||
|
|
}
|