|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Phant\EmailSender\Service; |
| 6 | + |
| 7 | +use Twig\Environment as Twig; |
| 8 | +use Twig\Loader\FilesystemLoader; |
| 9 | + |
| 10 | +class EmailBuilder |
| 11 | +{ |
| 12 | + public const TemplateEmail = 'layout.twig'; |
| 13 | + public const TemplateComponentCta = 'component/cta.twig'; |
| 14 | + public const TemplateComponentOtp = 'component/otp.twig'; |
| 15 | + public const TemplateComponentMeta = 'component/meta.twig'; |
| 16 | + |
| 17 | + protected Twig $twig; |
| 18 | + |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + $loader = new FilesystemLoader(__DIR__ . '/../Template/'); |
| 22 | + |
| 23 | + $this->twig = new Twig($loader, [ |
| 24 | + 'debug' => true, |
| 25 | + 'auto_reload' => true, |
| 26 | + ]); |
| 27 | + } |
| 28 | + |
| 29 | + public function build( |
| 30 | + string $bodyHtml, |
| 31 | + ?string $headerLogo, |
| 32 | + ?string $footerLogo, |
| 33 | + ?string $footerHtml |
| 34 | + ): string { |
| 35 | + return $this->twig->render( |
| 36 | + self::TemplateEmail, |
| 37 | + [ |
| 38 | + 'body_html' => $bodyHtml, |
| 39 | + 'header_logo' => $headerLogo, |
| 40 | + 'footer_logo' => $footerLogo, |
| 41 | + 'footer_html' => $footerHtml, |
| 42 | + ] |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + public function buildCta( |
| 47 | + string $url, |
| 48 | + string $label, |
| 49 | + ?string $backgroundColor = '#1E88E5', |
| 50 | + ?string $textColor = '#ffffff' |
| 51 | + ): string { |
| 52 | + return $this->twig->render( |
| 53 | + self::TemplateComponentCta, |
| 54 | + [ |
| 55 | + 'url' => $url, |
| 56 | + 'label' => $label, |
| 57 | + 'background_color' => $backgroundColor, |
| 58 | + 'text_color' => $textColor, |
| 59 | + ] |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function buildOtp( |
| 64 | + string $otp |
| 65 | + ): string { |
| 66 | + return $this->twig->render( |
| 67 | + self::TemplateComponentOtp, |
| 68 | + [ |
| 69 | + 'otp' => $otp, |
| 70 | + ] |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function buildMeta( |
| 75 | + array $metas |
| 76 | + ): string { |
| 77 | + return $this->twig->render( |
| 78 | + self::TemplateComponentMeta, |
| 79 | + [ |
| 80 | + 'metas' => $metas, |
| 81 | + ] |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments