Ecodev /
felix
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Ecodev\Felix\Service; |
||
| 6 | |||
| 7 | use Ecodev\Felix\Model\User; |
||
| 8 | use Laminas\View\Model\ViewModel; |
||
| 9 | use Laminas\View\Renderer\RendererInterface; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Service to render message to HTML. |
||
| 13 | */ |
||
| 14 | final class MessageRenderer |
||
| 15 | { |
||
| 16 | 1 | public function __construct(private readonly RendererInterface $viewRenderer, private string $hostname) |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 17 | { |
||
| 18 | 1 | } |
|
| 19 | |||
| 20 | /** |
||
| 21 | * Render a message by templating. |
||
| 22 | */ |
||
| 23 | 1 | public function render(?User $user, string $email, string $subject, string $type, array $mailParams, array $layoutParams = [], ?string $hostname = null): string |
|
| 24 | { |
||
| 25 | // Override hostname if given |
||
| 26 | 1 | $hostname ??= $this->hostname; |
|
| 27 | |||
| 28 | // First render the view |
||
| 29 | 1 | $serverUrl = 'https://' . $hostname; |
|
| 30 | 1 | $model = new ViewModel($mailParams); |
|
| 31 | 1 | $model->setTemplate(str_replace('_', '-', $type)); |
|
| 32 | 1 | $model->setVariable('email', $email); |
|
| 33 | 1 | $model->setVariable('user', $user); |
|
| 34 | 1 | $model->setVariable('serverUrl', $serverUrl); |
|
| 35 | 1 | $partialContent = $this->viewRenderer->render($model); |
|
| 36 | |||
| 37 | // Then inject it into layout |
||
| 38 | 1 | $layoutModel = new ViewModel($layoutParams); |
|
| 39 | 1 | $layoutModel->setTemplate('layout'); |
|
| 40 | 1 | $layoutModel->setVariable($model->captureTo(), $partialContent); |
|
| 41 | 1 | $layoutModel->setVariable('subject', $subject); |
|
| 42 | 1 | $layoutModel->setVariable('user', $user); |
|
| 43 | 1 | $layoutModel->setVariable('serverUrl', $serverUrl); |
|
| 44 | 1 | $layoutModel->setVariable('hostname', $hostname); |
|
| 45 | 1 | $content = $this->viewRenderer->render($layoutModel); |
|
| 46 | |||
| 47 | 1 | return $content; |
|
| 48 | } |
||
| 49 | } |
||
| 50 |