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 |
|
if (null !== $this->prependSubject) { |
70
|
5 |
|
$this->originalSubject = $message->getSubject(); |
71
|
|
|
|
72
|
5 |
|
$message->setSubject(sprintf( |
73
|
5 |
|
'%s %s', |
74
|
5 |
|
$this->prependSubject, |
75
|
5 |
|
$this->originalSubject |
76
|
3 |
|
)); |
77
|
3 |
|
} |
78
|
|
|
|
79
|
10 |
|
if (null !== $this->prependBodyTemplate) { |
80
|
5 |
|
$this->originalBody = $message->getBody(); |
81
|
|
|
|
82
|
5 |
|
$message->setBody(sprintf( |
83
|
5 |
|
"%s\n\n---------------------------------------------------------------------------\n\n%s", |
84
|
5 |
|
$this->templating->render($this->prependBodyTemplate), |
85
|
5 |
|
$this->originalBody |
86
|
3 |
|
)); |
87
|
3 |
|
} |
88
|
|
|
|
89
|
10 |
|
$this->lastMessage = $message; |
90
|
10 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Swift_Events_SendEvent $event |
94
|
|
|
*/ |
95
|
10 |
|
public function sendPerformed(Swift_Events_SendEvent $event) |
96
|
|
|
{ |
97
|
10 |
|
$this->restoreMessage($event->getMessage()); |
98
|
10 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Restore a changed message back to its original state. |
102
|
|
|
* |
103
|
|
|
* @param \Swift_Mime_Message $message |
104
|
|
|
*/ |
105
|
10 |
|
private function restoreMessage(\Swift_Mime_Message $message) |
106
|
|
|
{ |
107
|
10 |
|
if ($this->lastMessage === $message) { |
108
|
10 |
|
if (null !== $this->originalSubject) { |
109
|
5 |
|
$message->setSubject($this->originalSubject); |
110
|
5 |
|
$this->originalSubject = null; |
111
|
3 |
|
} |
112
|
|
|
|
113
|
10 |
|
if (null !== $this->originalBody) { |
114
|
5 |
|
$message->setBody($this->originalBody); |
115
|
5 |
|
$this->originalBody = null; |
116
|
3 |
|
} |
117
|
|
|
|
118
|
10 |
|
$this->lastMessage = null; |
119
|
6 |
|
} |
120
|
10 |
|
} |
121
|
|
|
} |
122
|
|
|
|