| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Services; |
| 6 |
|
| 7 |
/** |
| 8 |
* Renders email preview HTML for any bundled template key (core settings + Pro default bodies). |
| 9 |
*/ |
| 10 |
final class EmailTemplatePreviewService |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @return array{subject: string, body: string} |
| 14 |
*/ |
| 15 |
public static function render( |
| 16 |
string $templateKey, |
| 17 |
string $subjectTpl, |
| 18 |
string $bodyTpl, |
| 19 |
?int $tripId = null |
| 20 |
): array { |
| 21 |
$key = sanitize_key($templateKey); |
| 22 |
if ($key === '' || !self::isPreviewable($key)) { |
| 23 |
throw new \InvalidArgumentException(__('Unknown email template.', 'yatra')); |
| 24 |
} |
| 25 |
|
| 26 |
$samples = EmailTemplateSampleData::forTemplateKey($key, $tripId); |
| 27 |
$coreType = TransactionalEmailTemplateService::coreTemplateKeyToType($key); |
| 28 |
|
| 29 |
if ($coreType !== null) { |
| 30 |
return TransactionalEmailTemplateService::renderWithStringTemplates( |
| 31 |
$coreType, |
| 32 |
$subjectTpl, |
| 33 |
$bodyTpl, |
| 34 |
$samples |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
$defaults = EmailTemplateDefaults::proSystemTemplate($key); |
| 39 |
if ($defaults === null) { |
| 40 |
throw new \InvalidArgumentException(__('Unknown email template.', 'yatra')); |
| 41 |
} |
| 42 |
|
| 43 |
$subjectOut = $subjectTpl !== '' |
| 44 |
? TransactionalEmailTemplateService::parseMergeTags($subjectTpl, $samples) |
| 45 |
: TransactionalEmailTemplateService::parseMergeTags($defaults['subject'], $samples); |
| 46 |
|
| 47 |
$bodyRaw = $bodyTpl !== '' ? $bodyTpl : $defaults['body']; |
| 48 |
$bodyOut = TransactionalEmailTemplateService::parseMergeTags($bodyRaw, $samples); |
| 49 |
|
| 50 |
return [ |
| 51 |
'subject' => $subjectOut, |
| 52 |
'body' => $bodyOut, |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
public static function isPreviewable(string $templateKey): bool |
| 57 |
{ |
| 58 |
return EmailTemplateSampleData::isPreviewableTemplateKey(sanitize_key($templateKey)); |
| 59 |
} |
| 60 |
} |
| 61 |
|