Failed Conditions
Push — master ( 398dce...01f535 )
by Adrien
02:21
created

MessageQueuer::renderMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 21
ccs 0
cts 17
cp 0
crap 2
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Service;
6
7
use Doctrine\ORM\EntityManager;
8
use Ecodev\Felix\Model\User;
9
use Laminas\View\Model\ViewModel;
10
use Laminas\View\Renderer\RendererInterface;
11
12
/**
13
 * Service to queue new message for pre-defined purposes
14
 */
15
class MessageQueuer
16
{
17
    /**
18
     * @var EntityManager
19
     */
20
    protected $entityManager;
21
22
    /**
23
     * @var string
24
     */
25
    private $hostname;
26
27
    /**
28
     * @var RendererInterface
29
     */
30
    private $viewRenderer;
31
32
    public function __construct(EntityManager $entityManager, RendererInterface $viewRenderer, string $hostname)
33
    {
34
        $this->entityManager = $entityManager;
35
        $this->hostname = $hostname;
36
        $this->viewRenderer = $viewRenderer;
37
    }
38
39
    /**
40
     * Render a message by templating
41
     *
42
     * @param null|User $user
43
     * @param string $email
44
     * @param string $subject
45
     * @param string $type
46
     * @param array $mailParams
47
     *
48
     * @return string
49
     */
50
    protected function renderMessage(?User $user, string $email, string $subject, string $type, array $mailParams): string
51
    {
52
        // First render the view
53
        $serverUrl = 'https://' . $this->hostname;
54
        $model = new ViewModel($mailParams);
55
        $model->setTemplate(str_replace('_', '-', $type));
56
        $model->setVariable('email', $email);
57
        $model->setVariable('user', $user);
58
        $model->setVariable('serverUrl', $serverUrl);
59
        $partialContent = $this->viewRenderer->render($model);
60
61
        // Then inject it into layout
62
        $layoutModel = new ViewModel([$model->captureTo() => $partialContent]);
63
        $layoutModel->setTemplate('layout');
64
        $layoutModel->setVariable('subject', $subject);
65
        $layoutModel->setVariable('user', $user);
66
        $layoutModel->setVariable('serverUrl', $serverUrl);
67
        $layoutModel->setVariable('hostname', $this->hostname);
68
        $content = $this->viewRenderer->render($layoutModel);
69
70
        return $content;
71
    }
72
}
73