|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SfCod\EmailEngineBundle\Mailer; |
|
4
|
|
|
|
|
5
|
|
|
use SfCod\EmailEngineBundle\Template\ParametersAwareInterface; |
|
6
|
|
|
use SfCod\EmailEngineBundle\Template\TemplateInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Email templates manager |
|
10
|
|
|
* |
|
11
|
|
|
* Class TemplateManager |
|
12
|
|
|
* |
|
13
|
|
|
* @author Virchenko Maksim <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* @package SfCod\EmailEngineBundle\Mailer |
|
16
|
|
|
*/ |
|
17
|
|
|
class TemplateManager |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* List of templates |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $templates = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* TemplateManager constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param TemplateInterface[] $templates |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(array $templates) |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($templates as $template) { |
|
34
|
|
|
$this->templates[$template::getSlug()] = $template; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* List all available templates |
|
40
|
|
|
* |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
public function listTemplates(): array |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->templates; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* List all available templates for choice using names |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
public function listTemplatesChoice(): array |
|
54
|
|
|
{ |
|
55
|
|
|
$templates = []; |
|
56
|
|
|
|
|
57
|
|
|
foreach ($this->templates as $slug => $template) { |
|
58
|
|
|
$templates[$slug] = $this->getTemplateName($slug); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $templates; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get template |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $slug |
|
68
|
|
|
* |
|
69
|
|
|
* @return string|null |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getTemplateClass(string $slug): string |
|
72
|
|
|
{ |
|
73
|
|
|
if (isset($this->templates[$slug])) { |
|
74
|
|
|
return $this->templates[$slug]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get email template name |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $slug |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getTemplateName(string $slug): string |
|
88
|
|
|
{ |
|
89
|
|
|
if (isset($this->templates[$slug])) { |
|
90
|
|
|
/** @var TemplateInterface $template */ |
|
91
|
|
|
$template = $this->templates[$slug]; |
|
92
|
|
|
|
|
93
|
|
|
return $template::getName(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $slug; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get template description |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $slug |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getTemplateDescription(string $slug): string |
|
107
|
|
|
{ |
|
108
|
|
|
/** @var TemplateInterface $template */ |
|
109
|
|
|
$template = $this->templates[$slug]; |
|
110
|
|
|
|
|
111
|
|
|
return $template::getDescription(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get template parameters |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $slug |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getTemplateParameters(string $slug): string |
|
122
|
|
|
{ |
|
123
|
|
|
/** @var TemplateInterface|ParametersAwareInterface $template */ |
|
124
|
|
|
$template = $this->templates[$slug]; |
|
125
|
|
|
|
|
126
|
|
|
if (in_array(ParametersAwareInterface::class, class_implements($template)) && false === empty($template::listParameters())) { |
|
127
|
|
|
$params = 'Available parameters are:'; |
|
128
|
|
|
|
|
129
|
|
|
foreach ($template::listParameters() as $parameter) { |
|
130
|
|
|
$params .= sprintf('<br/>- <b>{{%s}}</b>: %s;', $parameter::getName(), $parameter::getDescription()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $params; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return 'No parameters available.'; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|