|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014 Roave, LLC. |
|
4
|
|
|
* All rights reserved. |
|
5
|
|
|
* |
|
6
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
7
|
|
|
* modification, are permitted provided that the following conditions |
|
8
|
|
|
* are met: |
|
9
|
|
|
* |
|
10
|
|
|
* * Redistributions of source code must retain the above copyright |
|
11
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
12
|
|
|
* |
|
13
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
14
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
15
|
|
|
* the documentation and/or other materials provided with the |
|
16
|
|
|
* distribution. |
|
17
|
|
|
* |
|
18
|
|
|
* * Neither the names of the copyright holders nor the names of the |
|
19
|
|
|
* contributors may be used to endorse or promote products derived |
|
20
|
|
|
* from this software without specific prior written permission. |
|
21
|
|
|
* |
|
22
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
23
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
24
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
25
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
26
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
27
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
28
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
29
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
30
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
31
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
32
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
33
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
34
|
|
|
* |
|
35
|
|
|
* @author Antoine Hedgecock |
|
36
|
|
|
* |
|
37
|
|
|
* @copyright 2014 Roave, LLC |
|
38
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
namespace Roave\EmailTemplates\Service; |
|
42
|
|
|
|
|
43
|
|
|
use DateTime; |
|
44
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
45
|
|
|
use Roave\EmailTemplates\Entity\TemplateEntity; |
|
46
|
|
|
use Roave\EmailTemplates\Hydrator\TemplateHydrator; |
|
47
|
|
|
use Roave\EmailTemplates\InputFilter\TemplateInputFilter; |
|
48
|
|
|
use Roave\EmailTemplates\Options\TemplateServiceOptions; |
|
49
|
|
|
use Roave\EmailTemplates\Repository\TemplateRepositoryInterface; |
|
50
|
|
|
use Roave\EmailTemplates\Service\Template\Engine\EngineInterface; |
|
51
|
|
|
use Roave\EmailTemplates\Service\Template\EnginePluginManager; |
|
52
|
|
|
use Zend\EventManager\EventManagerAwareTrait; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Class TemplateEntity |
|
56
|
|
|
*/ |
|
57
|
|
|
class TemplateService implements TemplateServiceInterface |
|
58
|
|
|
{ |
|
59
|
|
|
use EventManagerAwareTrait; |
|
60
|
|
|
|
|
61
|
|
|
const EVENT_RENDER = 'render'; |
|
62
|
|
|
const EVENT_CREATE = 'create'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var ObjectManager |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $objectManager; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var TemplateRepositoryInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $repository; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var TemplateInputFilter |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $inputFilter; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var TemplateHydrator |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $hydrator; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var Template\EnginePluginManager |
|
86
|
|
|
*/ |
|
87
|
|
|
private $engineManager; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var TemplateServiceOptions |
|
91
|
|
|
*/ |
|
92
|
|
|
private $options; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param ObjectManager $objectManager |
|
96
|
|
|
* @param TemplateRepositoryInterface $repository |
|
97
|
|
|
* @param TemplateInputFilter $inputFilter |
|
98
|
|
|
* @param TemplateHydrator $hydrator |
|
99
|
|
|
* @param EnginePluginManager $engineManager |
|
100
|
|
|
* @param TemplateServiceOptions|null $options |
|
101
|
|
|
*/ |
|
102
|
|
|
public function __construct( |
|
103
|
|
|
ObjectManager $objectManager, |
|
104
|
|
|
TemplateRepositoryInterface $repository, |
|
105
|
|
|
TemplateInputFilter $inputFilter, |
|
106
|
|
|
TemplateHydrator $hydrator, |
|
107
|
|
|
EnginePluginManager $engineManager = null, |
|
108
|
|
|
TemplateServiceOptions $options = null |
|
109
|
|
|
) { |
|
110
|
|
|
$this->objectManager = $objectManager; |
|
111
|
|
|
$this->repository = $repository; |
|
112
|
|
|
$this->inputFilter = $inputFilter; |
|
113
|
|
|
$this->hydrator = $hydrator; |
|
114
|
|
|
$this->options = $options ?: new TemplateServiceOptions(); |
|
115
|
|
|
$this->engineManager = $engineManager ?: new EnginePluginManager(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Render a template |
|
120
|
|
|
* |
|
121
|
|
|
* @triggers render |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $templateId |
|
124
|
|
|
* @param string $locale |
|
125
|
|
|
* @param array|\Traversable $parameters |
|
126
|
|
|
* |
|
127
|
|
|
* @return string[] |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function render($templateId, $locale, array $parameters = array()) |
|
130
|
|
|
{ |
|
131
|
3 |
|
$template = $this->repository->getByIdAndLocale($templateId, $locale); |
|
132
|
3 |
|
$mergedParameters = array_merge($this->options->getPredefinedParams(), $parameters); |
|
133
|
|
|
|
|
134
|
3 |
|
if (! $template) { |
|
135
|
1 |
|
$template = $this->create($templateId, $locale, $mergedParameters); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
3 |
|
$this->getEventManager() |
|
139
|
3 |
|
->trigger(static::EVENT_RENDER, $this, ['template' => $template, 'parameters' => $mergedParameters]); |
|
140
|
|
|
|
|
141
|
|
|
/** @var EngineInterface $engine */ |
|
142
|
3 |
|
$engine = $this->engineManager->get($this->options->getEngine()); |
|
143
|
|
|
|
|
144
|
|
|
return [ |
|
145
|
3 |
|
$engine->render($template->getSubject(), $mergedParameters), |
|
146
|
3 |
|
$engine->render($template->getHtmlBody(), $mergedParameters), |
|
147
|
3 |
|
$engine->render($template->getTextBody(), $mergedParameters) |
|
148
|
|
|
]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Create a new template |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $templateId |
|
155
|
|
|
* @param string $locale |
|
156
|
|
|
* @param array $parameters |
|
157
|
|
|
* |
|
158
|
|
|
* @return TemplateEntity |
|
159
|
|
|
*/ |
|
160
|
1 |
|
protected function create($templateId, $locale, array $parameters) |
|
161
|
|
|
{ |
|
162
|
1 |
|
$template = new TemplateEntity(); |
|
163
|
1 |
|
$template->setId($templateId); |
|
164
|
1 |
|
$template->setLocale($locale); |
|
165
|
1 |
|
$template->setParameters($parameters); |
|
166
|
1 |
|
$template->setSubject($this->options->getDefaultSubject()); |
|
167
|
1 |
|
$template->setTextBody(sprintf($this->options->getDefaultBody(), $templateId, $locale)); |
|
168
|
1 |
|
$template->setHtmlBody(sprintf($this->options->getDefaultBody(), $templateId, $locale)); |
|
169
|
1 |
|
$template->setCreatedAt(new DateTime()); |
|
170
|
1 |
|
$template->setUpdatedAt(new DateTime()); |
|
171
|
|
|
|
|
172
|
1 |
|
$this->getEventManager() |
|
173
|
1 |
|
->trigger(static::EVENT_CREATE, $this, ['template' => $template]); |
|
174
|
|
|
|
|
175
|
1 |
|
$this->objectManager->persist($template); |
|
176
|
1 |
|
$this->objectManager->flush(); |
|
177
|
|
|
|
|
178
|
1 |
|
return $template; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Save a template entity |
|
183
|
|
|
* |
|
184
|
|
|
* @param array $data |
|
185
|
|
|
* @param TemplateEntity $template |
|
186
|
|
|
* |
|
187
|
|
|
* @throws Exception\FailedDataValidationException |
|
188
|
|
|
*/ |
|
189
|
2 |
|
public function update(array $data, TemplateEntity $template) |
|
190
|
|
|
{ |
|
191
|
|
|
// Allow for delta updates |
|
192
|
2 |
|
$data = array_merge($this->hydrator->extract($template), $data); |
|
193
|
|
|
|
|
194
|
2 |
|
$this->inputFilter->setData($data); |
|
195
|
2 |
|
if (! $this->inputFilter->isValid()) { |
|
196
|
1 |
|
throw Exception\FailedDataValidationException::create($this->inputFilter->getMessages()); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
1 |
|
$values = $this->inputFilter->getValues(); |
|
200
|
|
|
|
|
201
|
1 |
|
$template->setUpdatedAt(new DateTime()); |
|
202
|
|
|
|
|
203
|
1 |
|
$this->hydrator->hydrate($values, $template); |
|
204
|
1 |
|
$this->objectManager->flush(); |
|
205
|
1 |
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|