update
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services\Customer;
|
||||
|
||||
use App\Support\EmailRecipients;
|
||||
use App\Mail\Customer\ManualCustomerEmail;
|
||||
use App\Mail\Customer\PasswordResetByAdmin;
|
||||
use App\Models\Customer;
|
||||
@@ -93,9 +94,9 @@ class CustomerMailService
|
||||
|
||||
abort_if($subject === '' || $message === '', 422, 'Subject và content không được để trống.');
|
||||
|
||||
$to = $this->parseEmailRecipients($data['to'] ?? null) ?: [$customer->email];
|
||||
$cc = $this->parseEmailRecipients($data['cc'] ?? null);
|
||||
$bcc = $this->parseEmailRecipients($data['bcc'] ?? null);
|
||||
$to = EmailRecipients::parse($data['to'] ?? null) ?: [$customer->email];
|
||||
$cc = EmailRecipients::parse($data['cc'] ?? null);
|
||||
$bcc = EmailRecipients::parse($data['bcc'] ?? null);
|
||||
|
||||
abort_if($to === [], 422, 'Không có địa chỉ email nhận hợp lệ.');
|
||||
|
||||
@@ -129,18 +130,4 @@ class CustomerMailService
|
||||
'customer_id' => $customer->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function parseEmailRecipients(?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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Services\Email;
|
||||
|
||||
use App\Exceptions\InvalidTemplateVariableException;
|
||||
use App\Models\Template;
|
||||
use App\Support\MailPlaceholders;
|
||||
|
||||
/**
|
||||
* Render Template subject/message: chỉ thay thế placeholder dạng {{key}} đã khai báo
|
||||
@@ -11,7 +12,7 @@ use App\Models\Template;
|
||||
*/
|
||||
class EmailTemplateRenderer
|
||||
{
|
||||
private const PLACEHOLDER_PATTERN = '/\{\{\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\}\}/';
|
||||
private const PLACEHOLDER_PATTERN = MailPlaceholders::PATTERN;
|
||||
|
||||
/**
|
||||
* @return array{subject: string, html: string}
|
||||
@@ -31,7 +32,7 @@ class EmailTemplateRenderer
|
||||
|
||||
private function assertKnownVariables(string $scenario, string $content, array $variables): void
|
||||
{
|
||||
$used = $this->extractPlaceholders($content);
|
||||
$used = MailPlaceholders::extract($content);
|
||||
$invalid = array_values(array_diff($used, array_keys($variables)));
|
||||
|
||||
if ($invalid !== []) {
|
||||
@@ -39,17 +40,13 @@ class EmailTemplateRenderer
|
||||
}
|
||||
}
|
||||
|
||||
private function extractPlaceholders(string $content): array
|
||||
{
|
||||
preg_match_all(self::PLACEHOLDER_PATTERN, $content, $matches);
|
||||
|
||||
return array_values(array_unique($matches[1]));
|
||||
}
|
||||
|
||||
private function replace(string $content, array $variables, array $data): string
|
||||
{
|
||||
return preg_replace_callback(self::PLACEHOLDER_PATTERN, function (array $match) use ($variables, $data) {
|
||||
$key = $match[1];
|
||||
if (!isset($variables[$key]) && !array_key_exists($key, $data)) {
|
||||
return $match[0];
|
||||
}
|
||||
$definition = $variables[$key] ?? [];
|
||||
$value = $data[$key] ?? $definition['example'] ?? '';
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Email;
|
||||
|
||||
use App\Models\Customer;
|
||||
use App\Models\MailCampaign;
|
||||
use App\Models\Template;
|
||||
use App\Support\MailGlobals;
|
||||
use App\Support\MailPlaceholders;
|
||||
|
||||
final class MailCampaignRenderer
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $values
|
||||
* @return array{subject: string, html: string}
|
||||
*/
|
||||
public function render(MailCampaign $campaign, array $values): array
|
||||
{
|
||||
$subject = MailPlaceholders::fill((string) $campaign->subject, $values, escapeHtml: false);
|
||||
$content = MailPlaceholders::fill((string) $campaign->message, $values);
|
||||
|
||||
return [
|
||||
'subject' => $subject,
|
||||
'html' => $campaign->template_id ? $content : Template::renderWithLayout($subject, $content),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function recipientValues(MailCampaign $campaign, ?Customer $customer, string $email): array
|
||||
{
|
||||
return [...MailGlobals::values(), ...MailPlaceholders::recipientValues($customer, $email), ...$this->adminValues($campaign)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function sampleValues(MailCampaign $campaign): array
|
||||
{
|
||||
$samples = array_map(fn (string $key) => "[{$key}]", array_combine(
|
||||
array_keys(MailPlaceholders::RECIPIENT_FIELDS),
|
||||
array_keys(MailPlaceholders::RECIPIENT_FIELDS),
|
||||
));
|
||||
|
||||
return [...MailGlobals::values(), ...$samples, ...$this->adminValues($campaign)];
|
||||
}
|
||||
|
||||
private function adminValues(MailCampaign $campaign): array
|
||||
{
|
||||
return array_filter($campaign->variables ?? [], fn ($value) => $value !== null && $value !== '');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Email;
|
||||
|
||||
use App\Models\Template;
|
||||
use App\Support\EmailScenarioRegistry;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
final class TemplateGuard
|
||||
{
|
||||
public function assertCanSave(Template $template): void
|
||||
{
|
||||
if ($template->exists) {
|
||||
$this->assertKeepsRegisteredIdentity($template);
|
||||
}
|
||||
|
||||
$definition = EmailScenarioRegistry::describe($template->key, $template->scenario);
|
||||
if (!$definition['registered']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$duplicate = Template::query()
|
||||
->whereKeyNot($template->id)
|
||||
->where(fn ($q) => $q->where('key', $definition['scenario'])->orWhere('scenario', $definition['scenario']))
|
||||
->exists();
|
||||
if ($duplicate) {
|
||||
$this->reject("Scenario \"{$definition['scenario']}\" đã có template, không tạo thêm được.");
|
||||
}
|
||||
|
||||
if (!$definition['permissions']['disable'] && (int) $template->status !== 1) {
|
||||
$this->reject('Template hệ thống này không được tắt: code đang gửi email theo template này.');
|
||||
}
|
||||
|
||||
$missing = EmailScenarioRegistry::missingVariables($definition['required_variables'], $template->subject, $template->message);
|
||||
if ($missing !== []) {
|
||||
$this->reject(
|
||||
'Template thiếu biến bắt buộc: ' . implode(', ', array_map(fn ($key) => '{{$' . $key . '}}', $missing)),
|
||||
['missing_variables' => $missing],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function assertCanDelete(Template $template): void
|
||||
{
|
||||
$definition = EmailScenarioRegistry::describe($template->key, $template->scenario);
|
||||
|
||||
if (!$definition['permissions']['delete']) {
|
||||
$this->reject('Template hệ thống không được xoá: code đang gửi email theo template này.');
|
||||
}
|
||||
}
|
||||
|
||||
private function assertKeepsRegisteredIdentity(Template $template): void
|
||||
{
|
||||
$before = EmailScenarioRegistry::describe($template->getOriginal('key'), $template->getOriginal('scenario'));
|
||||
|
||||
if ($before['registered'] && !$before['permissions']['change_key'] && $template->isDirty(['key', 'scenario'])) {
|
||||
$this->reject("Không được đổi key/scenario của template hệ thống \"{$before['scenario']}\".");
|
||||
}
|
||||
}
|
||||
|
||||
private function reject(string $message, array $extra = []): never
|
||||
{
|
||||
throw new HttpResponseException(response()->json(['error' => true, 'message' => $message, ...$extra], 422));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user