/DataCollector/MessageDataCollector.php

https://github.com/symfony/SwiftmailerBundle · PHP · 193 lines · 110 code · 24 blank · 59 comment · 12 complexity · 708e1465c6672b24ee330bef72092e69 MD5 · raw file

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