This commit is contained in:
baodev0
2026-10-05 09:30:24 +07:00
parent a1aeb88061
commit 1544629a1f
53 changed files with 1292 additions and 963 deletions
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace App\Support;
final class EmailRecipients
{
/**
* @return array<int, string>
*/
public static function parse(?string $value): array
{
if (!$value) {
return [];
}
return collect(preg_split('/[,;]+/', $value) ?: [])
->map(fn ($email) => trim((string) $email))
->filter(fn ($email) => $email !== '' && filter_var($email, FILTER_VALIDATE_EMAIL))
->unique()
->values()
->all();
}
}
+214
View File
@@ -0,0 +1,214 @@
<?php
namespace App\Support;
final class EmailScenarioRegistry
{
public const LAYOUT = 'LAYOUT';
public const TRANSACTIONAL = 'TRANSACTIONAL';
public const BUSINESS = 'BUSINESS';
public const INTERNAL = 'INTERNAL';
public const MARKETING = 'MARKETING';
public const CATEGORIES = [
self::LAYOUT => 'Bố cục chung',
self::TRANSACTIONAL => 'Transactional',
self::BUSINESS => 'Business',
self::INTERNAL => 'Internal',
self::MARKETING => 'Marketing',
];
private const SYSTEM_PERMISSIONS = [
'edit_content' => true,
'delete' => false,
'change_key' => false,
'disable' => false,
];
private const CUSTOM_PERMISSIONS = [
'edit_content' => true,
'delete' => true,
'change_key' => true,
'disable' => true,
];
private const LAYOUT_SLOT_TOKEN = '{{$content}}';
public const SCENARIOS = [
'layout' => [
'label' => 'Bố cục Header/Footer (mặc định)', 'category' => self::LAYOUT, 'group' => 'LAYOUT',
'required_variables' => ['content'],
],
'verify_account_welcome' => [
'label' => 'Mã xác thực tài khoản', 'category' => self::TRANSACTIONAL, 'group' => 'AUTH',
'required_variables' => ['verify_code'],
],
'customer_password_reset_link' => [
'label' => 'Link đặt lại mật khẩu khách hàng', 'category' => self::TRANSACTIONAL, 'group' => 'AUTH',
'required_variables' => ['reset_url'],
],
'password_reset_admin' => [
'label' => 'Admin cấp lại mật khẩu', 'category' => self::TRANSACTIONAL, 'group' => 'SECURITY',
'required_variables' => ['password'],
],
'customer_welcome' => [
'label' => 'Chào mừng khách hàng mới', 'category' => self::TRANSACTIONAL, 'group' => 'ACCOUNT',
'required_variables' => [],
],
'vm_credentials' => [
'label' => 'Khởi tạo VPS (thông tin truy cập)', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['service_host', 'service_username', 'service_password'],
],
'hosting_information' => [
'label' => 'Thông tin truy cập Hosting', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['service_host', 'service_username', 'service_password'],
],
'hosting_account_created' => [
'label' => 'Hosting đã khởi tạo', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['domain_name'],
],
'vm_suspended' => [
'label' => 'VPS bị tạm ngưng', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['vm_name'],
],
'service_expiry' => [
'label' => 'Dịch vụ sắp hết hạn', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['product_name', 'service_due_exp'],
],
'service_renewal_reminder' => [
'label' => 'Nhắc gia hạn dịch vụ', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['product_name', 'expires_at'],
],
'vm_creation_failed' => [
'label' => 'Khởi tạo VPS thất bại', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['order_num'],
],
'vm_rebuild_failed' => [
'label' => 'Rebuild VPS thất bại', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['vm_name'],
],
'vm_password_changed' => [
'label' => 'Đổi mật khẩu VPS', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['vm_name', 'new_password_row'],
],
'snapshot_action' => [
'label' => 'Thao tác Snapshot VPS', 'category' => self::BUSINESS, 'group' => 'SERVICE',
'required_variables' => ['vm_name', 'snapshot_name'],
],
'invoice_notification' => [
'label' => 'Thông báo hoá đơn / gia hạn cước', 'category' => self::BUSINESS, 'group' => 'BILLING',
'required_variables' => ['invoice_number', 'total'],
],
'invoice_payment_reminder' => [
'label' => 'Nhắc thanh toán hoá đơn', 'category' => self::BUSINESS, 'group' => 'BILLING',
'required_variables' => ['invoice_number', 'outstanding'],
],
'customer_deposit_success' => [
'label' => 'Nạp tiền thành công', 'category' => self::BUSINESS, 'group' => 'BILLING',
'required_variables' => ['transaction_code', 'amount_formatted'],
],
'manual_deposit_status' => [
'label' => 'Trạng thái nạp tiền thủ công', 'category' => self::BUSINESS, 'group' => 'BILLING',
'required_variables' => ['transaction_code', 'status_label'],
],
'order_created' => [
'label' => 'Xác nhận đơn hàng', 'category' => self::BUSINESS, 'group' => 'ORDER',
'required_variables' => ['order_num'], 'permissions' => ['disable' => true],
],
'domain_order_received' => [
'label' => 'Đơn đăng ký tên miền đã ghi nhận', 'category' => self::BUSINESS, 'group' => 'ORDER',
'required_variables' => ['domain_name', 'order_num'],
],
'domain_order_approved' => [
'label' => 'Tên miền được duyệt', 'category' => self::BUSINESS, 'group' => 'ORDER',
'required_variables' => ['domain_name'], 'permissions' => ['disable' => true],
],
'domain_order_rejected' => [
'label' => 'Tên miền bị từ chối', 'category' => self::BUSINESS, 'group' => 'ORDER',
'required_variables' => ['domain_name', 'reason'], 'permissions' => ['disable' => true],
],
'ticket_created' => [
'label' => 'Ticket hỗ trợ mới', 'category' => self::BUSINESS, 'group' => 'SUPPORT',
'required_variables' => ['ticket_num'],
],
'admin_order_notification' => [
'label' => '[Admin] Đơn hàng mới', 'category' => self::INTERNAL, 'group' => 'ORDER',
'required_variables' => ['order_num'],
],
'admin_domain_order_new' => [
'label' => '[Admin] Đơn đăng ký tên miền mới', 'category' => self::INTERNAL, 'group' => 'ORDER',
'required_variables' => ['domain_name', 'order_num'],
],
'admin_hosting_account_created' => [
'label' => '[Admin] Hosting account mới', 'category' => self::INTERNAL, 'group' => 'SERVICE',
'required_variables' => ['domain_name'],
],
'admin_trial_registration_request' => [
'label' => '[Admin] Yêu cầu dùng thử VPS', 'category' => self::INTERNAL, 'group' => 'SERVICE',
'required_variables' => ['customer_email'],
],
'admin_manual_deposit_pending' => [
'label' => '[Admin] Yêu cầu nạp tiền chờ duyệt', 'category' => self::INTERNAL, 'group' => 'BILLING',
'required_variables' => ['transaction_code', 'amount_formatted'],
],
'admin_ticket_notification' => [
'label' => '[Admin] Ticket hỗ trợ mới', 'category' => self::INTERNAL, 'group' => 'SUPPORT',
'required_variables' => ['ticket_num'],
],
];
public static function find(?string ...$identifiers): ?string
{
foreach ($identifiers as $identifier) {
if ($identifier !== null && isset(self::SCENARIOS[$identifier])) {
return $identifier;
}
}
return null;
}
/**
* @return array{registered: bool, scenario: string|null, label: string|null, category: string, group: string|null, permissions: array<string, bool>, required_variables: array<int, string>}
*/
public static function describe(?string ...$identifiers): array
{
$scenario = self::find(...$identifiers);
$definition = $scenario ? self::SCENARIOS[$scenario] : null;
return [
'registered' => $definition !== null,
'scenario' => $scenario,
'label' => $definition['label'] ?? null,
'category' => $definition['category'] ?? self::MARKETING,
'group' => $definition['group'] ?? null,
'permissions' => $definition
? [...self::SYSTEM_PERMISSIONS, ...($definition['permissions'] ?? [])]
: self::CUSTOM_PERMISSIONS,
'required_variables' => $definition['required_variables'] ?? [],
];
}
/**
* @param array<int, string> $required
* @return array<int, string>
*/
public static function missingVariables(array $required, ?string $subject, ?string $message): array
{
$content = $subject . "\n" . $message;
$used = MailPlaceholders::extract($content);
return array_values(array_filter(
$required,
fn (string $key) => $key === 'content'
? !str_contains((string) $message, self::LAYOUT_SLOT_TOKEN)
: !in_array($key, $used, true),
));
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace App\Support;
use App\Models\Setting;
final class MailGlobals
{
public const LABELS = [
'company_name' => 'Tên công ty',
'app_name' => 'Tên hệ thống',
'company_logo' => 'Logo công ty (URL)',
'support_email' => 'Email hỗ trợ',
'support_phone' => 'Hotline hỗ trợ',
'frontend_url' => 'Địa chỉ portal khách hàng',
'login_url' => 'Link đăng nhập',
'current_year' => 'Năm hiện tại',
'today' => 'Ngày hiện tại',
];
public static function frontendUrl(): string
{
return rtrim((string) (config('app.frontend_url') ?: config('app.url')), '/');
}
/**
* @return array<string, string>
*/
public static function values(): array
{
try {
$setting = json_decode((string) Setting::where('key', 'setting')->value('value'), true) ?: [];
} catch (\Throwable) {
$setting = [];
}
$companyName = $setting['company'] ?? config('app.name', 'The Gioi So');
$frontendUrl = self::frontendUrl();
return [
'company_name' => $companyName,
'app_name' => $companyName,
'company_logo' => $setting['logo'] ?? '',
'support_email' => $setting['email'] ?? config('mail.support_email', 'support@thegioiso.com'),
'support_phone' => $setting['hotline'] ?? $setting['phone'] ?? config('mail.support_phone', '1900.xxxx'),
'frontend_url' => $frontendUrl,
'login_url' => $frontendUrl . '/dang-nhap',
'current_year' => now()->format('Y'),
'today' => now()->format('d/m/Y'),
];
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
namespace App\Support;
use App\Models\Customer;
final class MailPlaceholders
{
public const PATTERN = '/\{\{\s*\$?([a-zA-Z_][a-zA-Z0-9_]*)\s*\}\}/';
private const LAYOUT_SLOT = 'content';
public const RECIPIENT_FIELDS = [
'client_name' => 'name',
'customer_name' => 'name',
'full_name' => 'name',
'fullname' => 'name',
'name' => 'name',
'customer_email' => 'email',
'email' => 'email',
'customer_phone' => 'phone',
'phone' => 'phone',
];
/**
* @return array<int, string>
*/
public static function extract(?string ...$contents): array
{
preg_match_all(self::PATTERN, implode("\n", array_map('strval', $contents)), $matches);
return array_values(array_diff(array_unique($matches[1]), [self::LAYOUT_SLOT]));
}
/**
* @param array<string, string|null> $values
*/
public static function fill(string $content, array $values, bool $escapeHtml = true): string
{
return preg_replace_callback(self::PATTERN, function (array $match) use ($values, $escapeHtml) {
if ($match[1] === self::LAYOUT_SLOT) {
return $match[0];
}
$value = (string) ($values[$match[1]] ?? '');
return $escapeHtml ? nl2br(htmlspecialchars($value, ENT_QUOTES, 'UTF-8')) : $value;
}, $content) ?? $content;
}
/**
* @return array<string, string>
*/
public static function recipientValues(?Customer $customer, string $email): array
{
$fields = [
'name' => $customer?->fullname ?: $email,
'email' => $email,
'phone' => (string) ($customer?->phone ?? ''),
];
return array_map(fn (string $field) => $fields[$field], self::RECIPIENT_FIELDS);
}
}