Failed Conditions
Pull Request — master (#11)
by Adrien
15:48 queued 12:33
created

MessageRenderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 25
ccs 18
cts 18
cp 1
crap 1
rs 9.7
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
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 16 at column 49
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