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.
90 lines
2.5 KiB
90 lines
2.5 KiB
<?php |
|
|
|
namespace App\Services; |
|
|
|
use App\Models\EmailTemplate; |
|
use RuntimeException; |
|
|
|
class EmailTemplateService |
|
{ |
|
public function all(): array |
|
{ |
|
return EmailTemplate::query()->orderBy('name')->get()->all(); |
|
} |
|
|
|
public function get(string $slug): EmailTemplate |
|
{ |
|
$template = EmailTemplate::query()->where('slug', $slug)->first(); |
|
|
|
if ($template === null) { |
|
throw new RuntimeException("Email template [{$slug}] not found."); |
|
} |
|
|
|
return $template; |
|
} |
|
|
|
public function update(string $slug, string $subject, string $bodyHtml): EmailTemplate |
|
{ |
|
$template = $this->get($slug); |
|
$template->update([ |
|
'subject' => $subject, |
|
'body_html' => $bodyHtml, |
|
]); |
|
|
|
return $template->fresh(); |
|
} |
|
|
|
public function render(string $slug, array $variables): array |
|
{ |
|
$template = $this->get($slug); |
|
|
|
$defaults = [ |
|
'app_name' => config('app.name'), |
|
'from_email' => config('mail.from.address'), |
|
]; |
|
|
|
$vars = array_merge($defaults, $variables); |
|
|
|
return [ |
|
'subject' => $this->replacePlaceholders($template->subject, $vars), |
|
'html' => $this->wrapHtml($this->replacePlaceholders($template->body_html, $vars)), |
|
]; |
|
} |
|
|
|
public function sampleVariables(string $slug): array |
|
{ |
|
$base = [ |
|
'username' => 'preview.user', |
|
'display_name' => 'Preview User', |
|
]; |
|
|
|
return match ($slug) { |
|
'password_reset' => array_merge($base, [ |
|
'reset_url' => url('/password/reset/preview-token-not-valid'), |
|
'expires_minutes' => '60', |
|
]), |
|
'password_changed' => array_merge($base, [ |
|
'changed_at' => now()->format('Y-m-d H:i:s').' WIB', |
|
'changed_by' => 'admin (preview)', |
|
]), |
|
default => $base, |
|
}; |
|
} |
|
|
|
private function replacePlaceholders(string $text, array $variables): string |
|
{ |
|
foreach ($variables as $key => $value) { |
|
$text = str_replace('{' . $key . '}', (string) $value, $text); |
|
} |
|
|
|
return $text; |
|
} |
|
|
|
private function wrapHtml(string $body): string |
|
{ |
|
return '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"></head>' |
|
. '<body style="font-family:Arial,sans-serif;line-height:1.6;color:#333;max-width:600px;margin:0 auto;padding:20px;">' |
|
. $body |
|
. '</body></html>'; |
|
} |
|
}
|
|
|