From 3566348ae449a418af896715eaeef4b7960a31ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Romey?= Date: Fri, 10 May 2013 15:42:53 +0200 Subject: [PATCH] [SwiftmailerBridge] Added mailers --- .../DataCollector/MessageDataCollector.php | 109 +++++++++++++++--- 1 file changed, 92 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Bridge/Swiftmailer/DataCollector/MessageDataCollector.php b/src/Symfony/Bridge/Swiftmailer/DataCollector/MessageDataCollector.php index 3ae96a276f090..3a0686628b3cf 100644 --- a/src/Symfony/Bridge/Swiftmailer/DataCollector/MessageDataCollector.php +++ b/src/Symfony/Bridge/Swiftmailer/DataCollector/MessageDataCollector.php @@ -21,11 +21,11 @@ * * @author Fabien Potencier * @author Clément JOBEILI + * @author Jérémy Romey */ class MessageDataCollector extends DataCollector { private $container; - private $isSpool; /** * Constructor. @@ -34,12 +34,10 @@ class MessageDataCollector extends DataCollector * to avoid the creation of these objects when no emails are sent. * * @param ContainerInterface $container A ContainerInterface instance - * @param Boolean $isSpool */ - public function __construct(ContainerInterface $container, $isSpool) + public function __construct(ContainerInterface $container) { $this->container = $container; - $this->isSpool = $isSpool; } /** @@ -47,32 +45,109 @@ public function __construct(ContainerInterface $container, $isSpool) */ public function collect(Request $request, Response $response, \Exception $exception = null) { + $this->data = array( + 'mailer' => array(), + 'messageCount' => 0, + 'defaultMailer' => '', + ); // only collect when Swiftmailer has already been initialized if (class_exists('Swift_Mailer', false)) { - $logger = $this->container->get('swiftmailer.plugin.messagelogger'); - $this->data['messages'] = $logger->getMessages(); - $this->data['messageCount'] = $logger->countMessages(); - } else { - $this->data['messages'] = array(); - $this->data['messageCount'] = 0; + $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] = array( + 'messages' => $logger->getMessages(), + 'messageCount' => $logger->countMessages(), + 'isSpool' => $this->container->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)), + ); + $this->data['messageCount'] += $logger->countMessages(); + } + } } + } - $this->data['isSpool'] = $this->isSpool; + /** + * Returns the mailer names. + * + * @return array The mailer names. + */ + public function getMailers() + { + return array_keys($this->data['mailer']); } - public function getMessageCount() + /** + * Returns the data collected of a mailer. + * + * @return array The data of the mailer. + */ + + public function getMailerData($name) { - return $this->data['messageCount']; + if (!isset($this->data['mailer'][$name])) { + throw new \LogicException(sprintf("Missing %s data in %s", $name, get_class())); + } + + return $this->data['mailer'][$name]; } - public function getMessages() + /** + * Returns the message count of a mailer or the total. + * + * @return int The number of messages. + */ + public function getMessageCount($name = null) { - return $this->data['messages']; + if (is_null($name)) { + return $this->data['messageCount']; + } elseif ($data = $this->getMailerData($name)) { + return $data['messageCount']; + } + + return null; } - public function isSpool() + /** + * Returns the message of a mailer. + * + * @return array The messages. + */ + public function getMessages($name) + { + if ($data = $this->getMailerData($name)) { + return $data['messages']; + } + + return array(); + } + + /** + * Returns if the mailer has spool. + * + * @return boolean + */ + public function isSpool($name) + { + if ($data = $this->getMailerData($name)) { + return $data['isSpool']; + } + + return null; + } + + /** + * Returns if the mailer is the default mailer. + * + * @return boolean + */ + public function isDefaultMailer($name) { - return $this->data['isSpool']; + return $this->data['defaultMailer'] == $name; } /**