Completed
Push — master ( 5523da...313d5d )
by max
12:42
created

Template   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 97
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 3
A getSubject() 0 4 1
A getBody() 0 10 1
A getId() 0 4 1
A getLayoutId() 0 4 1
1
<?php
2
3
namespace T4web\Mail;
4
5
use Zend\View\Renderer\RendererInterface;
6
use Zend\View\Model\ViewModel;
7
8
class Template
9
{
10
    const LAYOUT_DEFAULT = 1;
11
    const FEEDBACK_ANSWER = 1;
12
13
    /**
14
     * @var int
15
     */
16
    private $id;
17
18
    /**
19
     * @var string
20
     */
21
    private $subject;
22
23
    /**
24
     * @var string
25
     */
26
    private $template;
27
28
    /**
29
     * @var ViewModel
30
     */
31
    private $layout;
32
33
    /**
34
     * @var int
35
     */
36
    private $layoutId;
37
38
    /**
39
     * @var RendererInterface
40
     */
41
    private $renderer;
42
43
    public function __construct(
44
        $templateId,
45
        array $templateConfig,
46
        RendererInterface $renderer,
47
        ViewModel $layout
48
    ) {
49
        if (!isset($templateConfig['subject'])) {
50
            throw new Exception\TemplateException("Template subject not configured.");
51
        }
52
53
        $this->subject = $templateConfig['subject'];
54
55
        if (!isset($templateConfig['template'])) {
56
            throw new Exception\TemplateException("Template template not configured.");
57
        }
58
59
        $this->id = $templateId;
60
        $this->template = $templateConfig['template'];
61
        $this->layout = $layout;
62
        $this->layoutId = $templateConfig['layout'];
63
        $this->renderer = $renderer;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getSubject()
70
    {
71
        return $this->subject;
72
    }
73
74
    /**
75
     * @param array $data
76
     * @return string
77
     */
78
    public function getBody(array $data = [])
79
    {
80
        $viewContent = new ViewModel($data);
81
        $viewContent->setTemplate($this->template);
82
        $content = $this->renderer->render($viewContent);
83
84
        $this->layout->setVariable('content', $content);
85
86
        return $this->renderer->render($this->layout);
87
    }
88
89
    /**
90
     * @return int
91
     */
92
    public function getId()
93
    {
94
        return $this->id;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getLayoutId()
101
    {
102
        return $this->layoutId;
103
    }
104
}