update email template
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Email;
|
||||
|
||||
use App\Jobs\Mail\SendTemplateEmailJob;
|
||||
use App\Models\Template;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* Entry point chung để gửi email theo scenario. Load Template (có cache),
|
||||
* render qua EmailTemplateRenderer, gửi qua queue (SendTemplateEmailJob).
|
||||
*/
|
||||
class EmailTemplateService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EmailTemplateRenderer $renderer,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param string|array<int, string> $to
|
||||
*/
|
||||
public function send(string $scenario, string|array $to, array $data = []): void
|
||||
{
|
||||
SendTemplateEmailJob::dispatch($scenario, is_array($to) ? $to : [$to], $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render ngay (không gửi) — dùng cho preview trong Email Builder.
|
||||
*
|
||||
* @return array{subject: string, html: string}
|
||||
*/
|
||||
public function preview(string $scenario, array $data = []): array
|
||||
{
|
||||
return $this->renderer->render($this->loadTemplate($scenario), $data);
|
||||
}
|
||||
|
||||
public function loadTemplate(string $scenario): Template
|
||||
{
|
||||
return Cache::remember(
|
||||
Template::CACHE_PREFIX . $scenario,
|
||||
3600,
|
||||
fn () => Template::where('scenario', $scenario)->where('status', 1)->firstOrFail(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user