Skip to content

[SwiftmailerBridge] Added mailers #8005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 92 additions & 17 deletions src/Symfony/Bridge/Swiftmailer/DataCollector/MessageDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
*
* @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;
private $isSpool;

/**
* Constructor.
Expand All @@ -34,45 +34,120 @@ 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;
}

/**
* {@inheritdoc}
*/
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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
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'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong but it seems that the "isSpool" key may not exist as it is set in a if statement and not at the end of the collectmethod as before.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benja-M-1 you're right but this method depends ont the fact that MailerData is filled or not... so, first it should throw a LogicErrorException if MailerData we're looking for is not set because we have to collect data before.

And then it could test the isSpool key indeed... or at collect time $datacould be initialized with default values.

@jeremyFreeAgent what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes a LogicException is good. Thanks @benja-M-1 and @shouze !

}

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;
}

/**
Expand Down