/**
  * Send scheduled newsletters to all active subscribers.
  *
  * @todo Add event listener to swift in order to try resending the newsletter.
  *
  * @throws InvalidArgumentException
  *
  * @param array $arguments
  * @param array $options
  *
  * @return void
  */
 protected function execute($arguments = array(), $options = array())
 {
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
     try {
         $from = sfNewsletterPluginConfiguration::getFromEmail();
     } catch (InvalidArgumentException $e) {
         $this->logSection($this->name, $e->getMessage(), 30, 'ERROR');
         throw $e;
     }
     $newsletters = NewsletterPeer::retrieveScheduled(new DateTime($options['schedule']));
     if (empty($newsletters)) {
         $this->logSection($this->name, 'There are no newsletters on schedule.');
         return;
     }
     /* @var $eachNewsletter Newsletter */
     foreach ($newsletters as $eachNewsletter) {
         try {
             // get recipient list
             $recipientList = NewsletterRecipientList::createInstanceActiveSubscribers();
             $recipientList->addTo($from);
             // send the mail using swift
             try {
                 $mailer = new Swift(new Swift_Connection_NativeMail());
                 $message = new Swift_Message($eachNewsletter->getSubject(), $eachNewsletter->getContent(), $eachNewsletter->getContentType()->getMimeType());
                 $sent = $mailer->send($message, $recipientList, $from);
                 $mailer->disconnect();
                 if ($sent < count($recipientList)) {
                     $this->logSection($this->name, sprintf(sfNewsletterPluginConfiguration::EXCEPTION_SWIFT_ERROR . ' Error: Email has not reached all recipients. Successfully sent to %d of %d recipients.', $sent, count($recipientList)), null, 'ERROR');
                 }
             } catch (Exception $e) {
                 $mailer->disconnect();
                 $this->logSection($this->name, sfNewsletterPluginConfiguration::EXCEPTION_SWIFT_ERROR . ' Error: ' . $e->getMessage(), null, 'ERROR');
                 throw $e;
             }
         } catch (RuntimeException $e) {
             $this->logSection($this->name, $e->getMessage());
             throw $e;
         }
     }
 }
require_once dirname(__FILE__) . '/../bootstrap/task.php';
# load fixtures of this plugin
$propelData->loadData(sfConfig::get('sf_plugins_dir') . '/sfNewsletterPlugin/data/fixtures');
$limeTest = new lime_test(6, new lime_output_color());
sfConfig::set('sf_newsletterplugin_from', '');
$task = new SendScheduledNewsletterTask($dispatcher, $formatter);
try {
    $task->run(array(), array());
    $limeTest->fail('InvalidArgumentException not thrown.');
} catch (InvalidArgumentException $e) {
    $limeTest->is($e->getMessage(), sfNewsletterPluginConfiguration::EXCEPTION_NO_SENDER, 'Caught correct Exception.');
}
sfConfig::set('sf_newsletterplugin_from', 'invalid-email');
try {
    $task->run(array(), array());
    $limeTest->fail('InvalidArgumentException not thrown.');
} catch (InvalidArgumentException $e) {
    $limeTest->like($e->getMessage(), '/' . sfNewsletterPluginConfiguration::EXCEPTION_INVALID_SENDER . '/', 'Caught correct Exception.');
}
sfConfig::set('sf_newsletterplugin_from', '*****@*****.**');
$task->run(array(), array());
$logs = $logger->getLogEntries();
$limeTest->like($logs[2], '/There are no newsletters on schedule./', 'Task exits while no newsletter are given.');
$newsletter = NewsletterPeer::retrieveByName('first newsletter');
$limeTest->ok($newsletter->setScheduledAt(new DateTime('-7 hours'))->save(), 'Scheduled Newsletter.');
$limeTest->is(count(NewsletterPeer::retrieveScheduled(new DateTime('-6 hours'))), 1, 'Found scheduled Newsletter.');
$task->run(array(), array('schedule="-6 hours"'));
$logs = $logger->getLogEntries();
$limeTest->unlike($logs[3], '/' . sfNewsletterPluginConfiguration::EXCEPTION_SWIFT_ERROR . '/', 'Email sent successfully.');
// @todo Add test checking POP3 to verify the email really got there!