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