1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pixelart Swiftmailer manipulator bundle. |
5
|
|
|
* |
6
|
|
|
* (c) pixelart GmbH |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Pixelart\Bundle\SwiftmailerManipulatorBundle\Swift\Plugins; |
13
|
|
|
|
14
|
|
|
use Swift_Events_SendEvent; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Allows manipulations of messages on-the-fly. |
19
|
|
|
*/ |
20
|
|
|
final class ManipulatorPlugin implements \Swift_Events_SendListener |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var EngineInterface |
24
|
|
|
*/ |
25
|
|
|
private $templating; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $prependSubject; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $prependBodyTemplate; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string The original subject before manipulations |
39
|
|
|
*/ |
40
|
|
|
private $originalSubject; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string The original body before manipulations |
44
|
|
|
*/ |
45
|
|
|
private $originalBody; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Swift_Mime_Message The message that was last replaced |
49
|
|
|
*/ |
50
|
|
|
private $lastMessage; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param EngineInterface $templating |
54
|
|
|
* @param string $prependSubject |
55
|
|
|
* @param string $prependBodyTemplate |
56
|
|
|
*/ |
57
|
10 |
|
public function __construct(EngineInterface $templating, $prependSubject, $prependBodyTemplate) |
58
|
|
|
{ |
59
|
10 |
|
$this->templating = $templating; |
60
|
10 |
|
$this->prependSubject = $prependSubject; |
61
|
10 |
|
$this->prependBodyTemplate = $prependBodyTemplate; |
62
|
10 |
|
} |
63
|
|
|
|
64
|
10 |
|
public function beforeSendPerformed(\Swift_Events_SendEvent $event) |
65
|
|
|
{ |
66
|
10 |
|
$message = $event->getMessage(); |
67
|
10 |
|
$this->restoreMessage($message); |
68
|
|
|
|
69
|
10 |
|
$this->prependSubject($message); |
70
|
10 |
|
$this->prependBodyWithTemplate($message); |
71
|
|
|
|
72
|
10 |
|
$this->lastMessage = $message; |
73
|
10 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Swift_Events_SendEvent $event |
77
|
|
|
*/ |
78
|
10 |
|
public function sendPerformed(Swift_Events_SendEvent $event) |
79
|
|
|
{ |
80
|
10 |
|
$this->restoreMessage($event->getMessage()); |
81
|
10 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \Swift_Mime_Message $message |
85
|
|
|
*/ |
86
|
10 |
|
private function prependSubject(\Swift_Mime_Message $message) |
87
|
|
|
{ |
88
|
10 |
|
if (null !== $this->prependSubject) { |
89
|
5 |
|
$this->originalSubject = $message->getSubject(); |
90
|
|
|
|
91
|
5 |
|
$message->setSubject(sprintf( |
92
|
5 |
|
'%s %s', |
93
|
5 |
|
$this->prependSubject, |
94
|
5 |
|
$this->originalSubject |
95
|
3 |
|
)); |
96
|
3 |
|
} |
97
|
10 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param \Swift_Mime_Message $message |
101
|
|
|
*/ |
102
|
10 |
|
private function prependBodyWithTemplate(\Swift_Mime_Message $message) |
103
|
|
|
{ |
104
|
10 |
|
if (null !== $this->prependBodyTemplate) { |
105
|
5 |
|
$this->originalBody = $message->getBody(); |
106
|
|
|
|
107
|
5 |
|
$message->setBody(sprintf( |
108
|
5 |
|
"%s\n\n---------------------------------------------------------------------------\n\n%s", |
109
|
5 |
|
$this->templating->render($this->prependBodyTemplate), |
110
|
5 |
|
$this->originalBody |
111
|
3 |
|
)); |
112
|
3 |
|
} |
113
|
10 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Restore a changed message back to its original state. |
117
|
|
|
* |
118
|
|
|
* @param \Swift_Mime_Message $message |
119
|
|
|
*/ |
120
|
10 |
|
private function restoreMessage(\Swift_Mime_Message $message) |
121
|
|
|
{ |
122
|
10 |
|
if ($this->lastMessage === $message) { |
123
|
10 |
|
if (null !== $this->originalSubject) { |
124
|
5 |
|
$message->setSubject($this->originalSubject); |
125
|
5 |
|
$this->originalSubject = null; |
126
|
3 |
|
} |
127
|
|
|
|
128
|
10 |
|
if (null !== $this->originalBody) { |
129
|
5 |
|
$message->setBody($this->originalBody); |
130
|
5 |
|
$this->originalBody = null; |
131
|
3 |
|
} |
132
|
|
|
|
133
|
10 |
|
$this->lastMessage = null; |
134
|
6 |
|
} |
135
|
10 |
|
} |
136
|
|
|
} |
137
|
|
|
|