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.
130 lines
4.0 KiB
130 lines
4.0 KiB
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\SmsVerification;
|
|
use RuntimeException;
|
|
|
|
class SmsConfig
|
|
{
|
|
/** @return array<string, string> */
|
|
public static function realSendingRequiredEnv(): array
|
|
{
|
|
return match (self::driver()) {
|
|
'submail' => self::submailRequiredEnv(),
|
|
default => self::tencentCloudRequiredEnv(),
|
|
};
|
|
}
|
|
|
|
/** @return array<string, string> */
|
|
public static function tencentCloudRequiredEnv(): array
|
|
{
|
|
return [
|
|
'sms.tencentcloud.endpoint' => 'TENCENT_SMS_ENDPOINT',
|
|
'sms.tencentcloud.sdk_app_id' => 'TENCENT_SMS_SDK_APP_ID',
|
|
'sms.tencentcloud.app_key' => 'TENCENT_SMS_APP_KEY',
|
|
'sms.tencentcloud.sign_name' => 'TENCENT_SMS_SIGN_NAME',
|
|
'sms.tencentcloud.template_id' => 'TENCENT_SMS_TEMPLATE_ID',
|
|
];
|
|
}
|
|
|
|
/** @return array<string, string> */
|
|
public static function submailRequiredEnv(): array
|
|
{
|
|
return [
|
|
'sms.submail.endpoint' => 'SUBMAIL_SMS_ENDPOINT',
|
|
'sms.submail.app_id' => 'SUBMAIL_APP_ID',
|
|
'sms.submail.app_key' => 'SUBMAIL_APP_KEY',
|
|
'sms.submail.sign_name' => 'SUBMAIL_SMS_SIGN_NAME',
|
|
'sms.submail.login_template_id' => 'SUBMAIL_LOGIN_TEMPLATE_ID',
|
|
];
|
|
}
|
|
|
|
public static function driver(): string
|
|
{
|
|
$driver = trim((string) config('sms.driver', 'submail'));
|
|
|
|
return $driver !== '' ? $driver : 'submail';
|
|
}
|
|
|
|
public static function activeProvider(): string
|
|
{
|
|
if (! (bool) config('sms.enabled')) {
|
|
return SmsVerification::PROVIDER_DISABLED;
|
|
}
|
|
|
|
return self::driver() === 'tencentcloud'
|
|
? SmsVerification::PROVIDER_TENCENTCLOUD
|
|
: SmsVerification::PROVIDER_SUBMAIL;
|
|
}
|
|
|
|
public static function activeLoginTemplateId(): string
|
|
{
|
|
return self::driver() === 'tencentcloud'
|
|
? (string) config('sms.tencentcloud.template_id')
|
|
: (string) config('sms.submail.login_template_id');
|
|
}
|
|
|
|
public static function activeSignName(): string
|
|
{
|
|
return self::driver() === 'tencentcloud'
|
|
? (string) config('sms.tencentcloud.sign_name')
|
|
: (string) config('sms.submail.sign_name');
|
|
}
|
|
|
|
public static function assertReadyForRealSending(): void
|
|
{
|
|
if (! (bool) config('sms.enabled')) {
|
|
return;
|
|
}
|
|
|
|
$driver = self::driver();
|
|
if ($driver === 'submail') {
|
|
self::assertKeysPresent(self::submailRequiredEnv(), 'Submail SMS configuration is incomplete.');
|
|
|
|
return;
|
|
}
|
|
|
|
if ($driver !== 'tencentcloud') {
|
|
throw new RuntimeException('SMS_ENABLED=true requires SMS_DRIVER=submail or tencentcloud.');
|
|
}
|
|
|
|
$missing = [];
|
|
foreach (self::tencentCloudRequiredEnv() as $configKey => $envName) {
|
|
$value = config($configKey);
|
|
if ($value === null || trim((string) $value) === '') {
|
|
$missing[] = $envName;
|
|
}
|
|
}
|
|
|
|
if ((int) config('sms.tencentcloud.template_param_count') !== 2) {
|
|
$missing[] = 'TENCENT_SMS_TEMPLATE_PARAM_COUNT must be 2';
|
|
}
|
|
|
|
if ($missing !== []) {
|
|
throw new RuntimeException(
|
|
'SMS_ENABLED=true but Tencent Cloud SMS configuration is incomplete. '
|
|
.'The legacy application API requires SDKAppID and App Key. Missing or invalid: '
|
|
.implode(', ', $missing)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string> $map
|
|
*/
|
|
private static function assertKeysPresent(array $map, string $prefix): void
|
|
{
|
|
$missing = [];
|
|
foreach ($map as $configKey => $envName) {
|
|
$value = config($configKey);
|
|
if ($value === null || trim((string) $value) === '') {
|
|
$missing[] = $envName;
|
|
}
|
|
}
|
|
if ($missing !== []) {
|
|
throw new RuntimeException($prefix.' Missing or invalid: '.implode(', ', $missing));
|
|
}
|
|
}
|
|
}
|