Exemplo n.º 1
0
 /**
  * Send email with error log data.
  *
  * @param Job              $job      The Job whose log we are about to send.
  * @param array(LogRecord) $messages An array of LogRecords with the error information.
  */
 protected function sendNotifications(Job $job, $messages)
 {
     $container = $this->getContainer();
     $adminEmail = $container->get('doctrine')->getRepository('BinovoElkarBackupBundle:User')->find(User::SUPERUSER_ID)->getEmail();
     if ($container->hasParameter('mailer_from') && $container->getParameter('mailer_from') != "") {
         $fromEmail = $container->getParameter('mailer_from');
     } else {
         $fromEmail = $adminEmail;
     }
     $idClient = $job->getClient()->getId();
     $idJob = $job->getId();
     $translator = $container->get('translator');
     $recipients = array();
     $engine = $container->get('templating');
     $filteredMessages = array();
     foreach ($messages as $aMessage) {
         if ($aMessage->getLevel() >= $job->getMinNotificationLevel()) {
             $filteredMessages[] = $aMessage;
         }
     }
     if (count($filteredMessages) && $job->getNotificationsTo()) {
         // we have something to send and people willing to receive it
         foreach ($job->getNotificationsTo() as $recipient) {
             // decode emails
             switch ($recipient) {
                 case Job::NOTIFY_TO_ADMIN:
                     $recipients[] = $adminEmail;
                     break;
                 case Job::NOTIFY_TO_OWNER:
                     $recipients[] = $job->getOwner()->getEmail();
                     break;
                 case Job::NOTIFY_TO_EMAIL:
                     $recipients[] = $job->getNotificationsEmail();
                     break;
                 default:
                     // do nothing
             }
         }
         $message = \Swift_Message::newInstance()->setSubject($translator->trans('Log for backup from job %joburl%', array('%joburl%' => $job->getUrl()), 'BinovoElkarBackup'))->setFrom(array($fromEmail => 'ElkarBackup'))->setTo($recipients)->setBody($engine->render('BinovoElkarBackupBundle:Default:logreport.html.twig', array('base' => gethostname(), 'job' => $job, 'messages' => $filteredMessages)), 'text/html');
         try {
             $container->get('mailer')->send($message);
         } catch (Exception $e) {
             $this->err('Command was unable to send the notification message: %exception%', array('%exception%' => $e->getMessage()));
         }
     }
 }