Passed
Push — master ( 9f25c4...68bc04 )
by Adrien
02:16
created

MessageRenderer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 50
ccs 20
cts 20
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 21 1
A __construct() 0 4 1
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
    /**
17
     * @var RendererInterface
18
     */
19
    private $viewRenderer;
20
21
    /**
22
     * @var string
23
     */
24
    private $hostname;
25
26 1
    public function __construct(RendererInterface $viewRenderer, string $hostname)
27
    {
28 1
        $this->viewRenderer = $viewRenderer;
29 1
        $this->hostname = $hostname;
30 1
    }
31
32
    /**
33
     * Render a message by templating
34
     *
35
     * @param null|User $user
36
     * @param string $email
37
     * @param string $subject
38
     * @param string $type
39
     * @param array $mailParams
40
     *
41
     * @return string
42
     */
43 1
    public function render(?User $user, string $email, string $subject, string $type, array $mailParams): string
44
    {
45
        // First render the view
46 1
        $serverUrl = 'https://' . $this->hostname;
47 1
        $model = new ViewModel($mailParams);
48 1
        $model->setTemplate(str_replace('_', '-', $type));
49 1
        $model->setVariable('email', $email);
50 1
        $model->setVariable('user', $user);
51 1
        $model->setVariable('serverUrl', $serverUrl);
52 1
        $partialContent = $this->viewRenderer->render($model);
53
54
        // Then inject it into layout
55 1
        $layoutModel = new ViewModel([$model->captureTo() => $partialContent]);
56 1
        $layoutModel->setTemplate('layout');
57 1
        $layoutModel->setVariable('subject', $subject);
58 1
        $layoutModel->setVariable('user', $user);
59 1
        $layoutModel->setVariable('serverUrl', $serverUrl);
60 1
        $layoutModel->setVariable('hostname', $this->hostname);
61 1
        $content = $this->viewRenderer->render($layoutModel);
62
63 1
        return $content;
64
    }
65
}
66