59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?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\AutoEmailGate;
|
|
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']);
|
|
}
|
|
|
|
public function buildViewData()
|
|
{
|
|
return [...parent::buildViewData(), AutoEmailGate::SCENARIO_DATA_KEY => $this->scenario()];
|
|
}
|
|
|
|
/**
|
|
* @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());
|
|
}
|
|
}
|