forked from symfony/swiftmailer-bundle
/
MessageDataCollector.php
185 lines (161 loc) · 4.98 KB
/
MessageDataCollector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\SwiftmailerBundle\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* MessageDataCollector.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Clément JOBEILI <clement.jobeili@gmail.com>
* @author Jérémy Romey <jeremy@free-agent.fr>
*/
class MessageDataCollector extends DataCollector
{
private $container;
/**
* We don't inject the message logger and mailer here
* to avoid the creation of these objects when no emails are sent.
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->reset();
// only collect when Swiftmailer has already been initialized
if (class_exists('Swift_Mailer', false)) {
$mailers = $this->container->getParameter('swiftmailer.mailers');
foreach ($mailers as $name => $mailer) {
if ($this->container->getParameter('swiftmailer.default_mailer') == $name) {
$this->data['defaultMailer'] = $name;
}
$loggerName = sprintf('swiftmailer.mailer.%s.plugin.messagelogger', $name);
if ($this->container->has($loggerName)) {
$logger = $this->container->get($loggerName);
$this->data['mailer'][$name] = [
'messages' => [],
'messageCount' => $logger->countMessages(),
'isSpool' => $this->container->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)),
];
foreach ($logger->getMessages() as $message) {
($userContentTypeProp = new \ReflectionProperty(\Swift_Message::class, 'userContentType'))->setAccessible(true);
$message->__contentType = $userContentTypeProp->getValue($message);
$message->__base64EncodedBody = base64_encode($message->getBody());
$this->data['mailer'][$name]['messages'][] = $message;
}
$this->data['messageCount'] += $logger->countMessages();
}
}
}
}
/**
* {@inheritdoc}
*/
public function reset()
{
$this->data = [
'mailer' => [],
'messageCount' => 0,
'defaultMailer' => '',
];
}
/**
* Returns the mailer names.
*
* @return array the mailer names
*/
public function getMailers()
{
return array_keys($this->data['mailer']);
}
/**
* Returns the data collected of a mailer.
*
* @return array the data of the mailer
*/
public function getMailerData($name)
{
if (!isset($this->data['mailer'][$name])) {
throw new \LogicException(sprintf('Missing "%s" data in "%s".', $name, get_class($this)));
}
return $this->data['mailer'][$name];
}
/**
* Returns the message count of a mailer or the total.
*
* @return int the number of messages
*/
public function getMessageCount($name = null)
{
if (null === $name) {
return $this->data['messageCount'];
} elseif ($data = $this->getMailerData($name)) {
return $data['messageCount'];
}
return;
}
/**
* Returns the messages of a mailer.
*
* @return \Swift_Message[] the messages
*/
public function getMessages($name = 'default')
{
if ($data = $this->getMailerData($name)) {
return $data['messages'];
}
return [];
}
/**
* Returns if the mailer has spool.
*
* @return bool
*/
public function isSpool($name)
{
if ($data = $this->getMailerData($name)) {
return $data['isSpool'];
}
return;
}
/**
* Returns if the mailer is the default mailer.
*
* @return bool
*/
public function isDefaultMailer($name)
{
return $this->data['defaultMailer'] == $name;
}
public function extractAttachments(\Swift_Message $message)
{
$attachments = [];
foreach ($message->getChildren() as $child) {
if ($child instanceof \Swift_Attachment) {
$attachments[] = $child;
}
}
return $attachments;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'swiftmailer';
}
}