1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\EmailEngineBundle\Repository; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException; |
7
|
|
|
use SfCod\EmailEngineBundle\Template\TemplateInterface; |
8
|
|
|
use Twig_Environment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class TwigFileRepository |
12
|
|
|
* |
13
|
|
|
* @author Virchenko Maksim <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @package SfCod\EmailEngineBundle\Repository |
16
|
|
|
*/ |
17
|
|
|
class TwigFileRepository implements RepositoryInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Twig template |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $template; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Twig_Environment |
28
|
|
|
*/ |
29
|
|
|
protected $twig; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* TwigFileRepository constructor. |
33
|
|
|
* |
34
|
|
|
* @param Twig_Environment $twig |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Twig_Environment $twig) |
37
|
|
|
{ |
38
|
|
|
$this->twig = $twig; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get subject template |
43
|
|
|
* |
44
|
|
|
* @param array $data |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
* |
48
|
|
|
* @throws \Throwable |
49
|
|
|
*/ |
50
|
|
|
public function getSubjectTemplate(array $data): string |
51
|
|
|
{ |
52
|
|
|
return $this->renderBlock('subject', $data); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get body template |
57
|
|
|
* |
58
|
|
|
* @param array $data |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
* |
62
|
|
|
* @throws \Throwable |
63
|
|
|
*/ |
64
|
|
|
public function getBodyTemplate(array $data): string |
65
|
|
|
{ |
66
|
|
|
return $this->renderBlock('content', $data); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get sender name template |
71
|
|
|
* |
72
|
|
|
* @param array $data |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
* |
76
|
|
|
* @throws \Throwable |
77
|
|
|
*/ |
78
|
|
|
public function getSenderNameTemplate(array $data): string |
79
|
|
|
{ |
80
|
|
|
if ($this->template->hasBlock('sender_name')) { |
|
|
|
|
81
|
|
|
return $this->renderBlock('sender_name', $data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return getenv('SENDER_NAME'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get sender email template |
89
|
|
|
* |
90
|
|
|
* @param array $data |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
* |
94
|
|
|
* @throws \Throwable |
95
|
|
|
*/ |
96
|
|
|
public function getSenderEmailTemplate(array $data): string |
97
|
|
|
{ |
98
|
|
|
if ($this->template->hasBlock('sender_email')) { |
|
|
|
|
99
|
|
|
return $this->renderBlock('sender_email', $data); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return getenv('SENDER_EMAIL'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Render twig template |
107
|
|
|
* |
108
|
|
|
* @param string $block |
109
|
|
|
* @param array $data |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
* |
113
|
|
|
* @throws \Throwable |
114
|
|
|
*/ |
115
|
|
|
protected function renderBlock(string $block, array $data): string |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
|
|
return $this->template->renderBlock($block, $data); |
|
|
|
|
119
|
|
|
} catch (\Throwable $e) { |
120
|
|
|
throw new RepositoryUnavailableException($e->getMessage()); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Load template file |
126
|
|
|
* |
127
|
|
|
* @param string $directory |
128
|
|
|
* |
129
|
|
|
* @return \Twig_TemplateWrapper |
130
|
|
|
* |
131
|
|
|
* @throws RepositoryUnavailableException |
132
|
|
|
*/ |
133
|
|
|
protected function loadTemplate(string $directory) |
134
|
|
|
{ |
135
|
|
|
try { |
136
|
|
|
$this->twig->setLoader(new \Twig_Loader_Chain([ |
137
|
|
|
$this->twig->getLoader(), |
138
|
|
|
new \Twig_Loader_Filesystem('Data', $directory), |
139
|
|
|
])); |
140
|
|
|
|
141
|
|
|
return $this->twig->load('template.html.twig'); |
142
|
|
|
} catch (\Throwable $e) { |
143
|
|
|
throw new RepositoryUnavailableException($e->getMessage()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Repository initialize |
149
|
|
|
* |
150
|
|
|
* @param TemplateInterface $template |
151
|
|
|
* @param array $arguments |
152
|
|
|
* |
153
|
|
|
* @throws RepositoryUnavailableException |
154
|
|
|
* @throws \ReflectionException |
155
|
|
|
*/ |
156
|
|
|
public function connect(TemplateInterface $template, array $arguments = []) |
157
|
|
|
{ |
158
|
|
|
$filePath = (new ReflectionClass(get_class($template)))->getFileName(); |
159
|
|
|
$directory = dirname($filePath); |
160
|
|
|
|
161
|
|
|
$this->template = $this->loadTemplate($directory); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.