update
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Mail\Concerns\BuildsTemplateEnvelope;
|
||||
use App\Models\Template;
|
||||
use App\Services\Email\EmailTemplateRenderer;
|
||||
use App\Services\Email\EmailTemplateService;
|
||||
use App\Support\MailGlobals;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
|
||||
abstract class TemplateMailable extends Mailable
|
||||
{
|
||||
use BuildsTemplateEnvelope;
|
||||
|
||||
private ?array $rendered = null;
|
||||
|
||||
abstract protected function scenario(): string;
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
abstract protected function variables(): array;
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return $this->templateEnvelope($this->rendered()['subject'], $this->template()->mailConfig());
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(htmlString: $this->rendered()['html']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{subject: string, html: string}
|
||||
*/
|
||||
private function rendered(): array
|
||||
{
|
||||
return $this->rendered ??= app(EmailTemplateRenderer::class)->render(
|
||||
$this->template(),
|
||||
[...MailGlobals::values(), ...$this->variables()],
|
||||
);
|
||||
}
|
||||
|
||||
private function template(): Template
|
||||
{
|
||||
return app(EmailTemplateService::class)->loadTemplate($this->scenario());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user