You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Cloud/CloudProvisioningBillingCon...

62 lines
1.9 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\Configurations;
/** Administrator-controlled payment window for newly provisioned instances. */
final class CloudProvisioningBillingConfig
{
public const PAYMENT_WINDOW_MINUTES_KEY =
'cloud_provision_invoice_payment_window_minutes';
public const DEFAULT_PAYMENT_WINDOW_MINUTES = 30;
public const MIN_PAYMENT_WINDOW_MINUTES = 1;
public const MAX_PAYMENT_WINDOW_MINUTES = 10080;
public static function paymentWindowMinutes(): int
{
$stored = Configurations::where(
'name',
self::PAYMENT_WINDOW_MINUTES_KEY
)->value('value');
try {
return self::validatePaymentWindowMinutes(
$stored === null ? self::DEFAULT_PAYMENT_WINDOW_MINUTES : $stored
);
} catch (\InvalidArgumentException $error) {
throw new CloudError(
'新建实例账单支付期限配置无效,请联系管理员',
503
);
}
}
public static function paymentWindowSeconds(): int
{
return self::paymentWindowMinutes() * 60;
}
public static function validatePaymentWindowMinutes($value): int
{
if (is_array($value) || is_object($value)) {
throw new \InvalidArgumentException(
'新建实例账单支付期限必须是整数分钟'
);
}
$raw = trim((string) $value);
if (preg_match('/^[0-9]+$/D', $raw) !== 1) {
throw new \InvalidArgumentException(
'新建实例账单支付期限必须是整数分钟'
);
}
$minutes = (int) $raw;
if ($minutes < self::MIN_PAYMENT_WINDOW_MINUTES
|| $minutes > self::MAX_PAYMENT_WINDOW_MINUTES) {
throw new \InvalidArgumentException(
'新建实例账单支付期限必须为 1 至 10080 分钟'
);
}
return $minutes;
}
}