phpci/PHPCI/Plugin/Email.php

205 lines
5.4 KiB
PHP
Raw Normal View History

2013-05-31 13:56:35 +02:00
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
2013-05-31 13:56:35 +02:00
namespace PHPCI\Plugin;
use b8\View;
2013-10-10 02:01:06 +02:00
use PHPCI\Builder;
2014-12-04 16:48:52 +01:00
use PHPCI\Helper\Lang;
2013-10-10 02:01:06 +02:00
use PHPCI\Model\Build;
2013-05-31 13:56:35 +02:00
/**
* Email Plugin - Provides simple email capability to PHPCI.
* @author Steve Brazier <meadsteve@gmail.com>
* @package PHPCI
* @subpackage Plugins
*/
class Email implements \PHPCI\Plugin
{
/**
* @var \PHPCI\Builder
*/
protected $phpci;
/**
* @var array
*/
protected $options;
/**
* @var \Swift_Mailer
*/
protected $mailer;
/**
* @var string
*/
protected $fromAddress;
/**
* Set up the plugin, configure options, etc.
* @param Builder $phpci
* @param Build $build
* @param \Swift_Mailer $mailer
* @param array $options
*/
2014-02-27 15:04:08 +01:00
public function __construct(
Builder $phpci,
Build $build,
\Swift_Mailer $mailer,
array $options = array()
) {
2013-05-31 13:56:35 +02:00
$this->phpci = $phpci;
2013-10-10 02:01:06 +02:00
$this->build = $build;
2013-05-31 13:56:35 +02:00
$this->options = $options;
$phpCiSettings = $phpci->getSystemConfig('phpci');
$this->fromAddress = isset($phpCiSettings['email_settings']['from_address'])
? $phpCiSettings['email_settings']['from_address']
: "notifications-ci@phptesting.org";
$this->mailer = $mailer;
2013-05-31 13:56:35 +02:00
}
/**
* Connects to MySQL and runs a specified set of queries.
*/
public function execute()
{
$addresses = $this->getEmailAddresses();
// Without some email addresses in the yml file then we
// can't do anything.
if (count($addresses) == 0) {
return false;
}
$subjectTemplate = "PHPCI - %s - %s";
$projectName = $this->phpci->getBuildProjectTitle();
2013-10-10 02:01:06 +02:00
$logText = $this->build->getLog();
2013-10-10 02:01:06 +02:00
if ($this->build->isSuccessful()) {
$sendFailures = $this->sendSeparateEmails(
$addresses,
2014-12-04 16:48:52 +01:00
sprintf($subjectTemplate, $projectName, Lang::get('passing_build')),
sprintf(Lang::get('log_output')."<br><pre>%s</pre>", $logText)
);
2013-10-10 02:01:06 +02:00
} else {
$view = new View('Email/failed');
$view->build = $this->build;
$view->project = $this->build->getProject();
$emailHtml = $view->render();
$sendFailures = $this->sendSeparateEmails(
$addresses,
2014-12-04 16:48:52 +01:00
sprintf($subjectTemplate, $projectName, Lang::get('failing_build')),
$emailHtml
);
}
2013-05-31 13:56:35 +02:00
// This is a success if we've not failed to send anything.
2014-12-04 16:48:52 +01:00
$this->phpci->log(Lang::get('n_emails_sent', (count($addresses) - count($sendFailures))));
$this->phpci->log(Lang::get('n_emails_failed', count($sendFailures)));
2013-10-10 02:01:06 +02:00
return (count($sendFailures) == 0);
2013-05-31 13:56:35 +02:00
}
/**
* @param string[]|string $toAddresses Array or single address to send to
* @param string[] $ccList
* @param string $subject Email subject
* @param string $body Email body
* @return array Array of failed addresses
*/
2014-02-25 17:03:00 +01:00
public function sendEmail($toAddresses, $ccList, $subject, $body)
{
$message = \Swift_Message::newInstance($subject)
->setFrom($this->fromAddress)
->setTo($toAddresses)
->setBody($body)
->setContentType("text/html");
2014-02-25 17:03:00 +01:00
if (is_array($ccList) && count($ccList)) {
$message->setCc($ccList);
}
$failedAddresses = array();
$this->mailer->send($message, $failedAddresses);
return $failedAddresses;
}
/**
* Send out build status emails.
* @param array $toAddresses
* @param $subject
* @param $body
* @return array
*/
public function sendSeparateEmails(array $toAddresses, $subject, $body)
{
$failures = array();
2014-02-25 17:03:00 +01:00
$ccList = $this->getCcAddresses();
2013-10-10 02:01:06 +02:00
foreach ($toAddresses as $address) {
2014-02-25 17:03:00 +01:00
$newFailures = $this->sendEmail($address, $ccList, $subject, $body);
2013-10-10 02:01:06 +02:00
foreach ($newFailures as $failure) {
$failures[] = $failure;
}
}
return $failures;
}
/**
* Get the list of email addresses to send to.
* @return array
*/
protected function getEmailAddresses()
{
$addresses = array();
2013-10-10 02:01:06 +02:00
$committer = $this->build->getCommitterEmail();
2013-10-08 13:50:59 +02:00
if (isset($this->options['committer']) && !empty($committer)) {
$addresses[] = $committer;
}
if (isset($this->options['addresses'])) {
foreach ($this->options['addresses'] as $address) {
$addresses[] = $address;
}
}
if (isset($this->options['default_mailto_address'])) {
$addresses[] = $this->options['default_mailto_address'];
return $addresses;
}
return array_unique($addresses);
}
2014-02-25 17:03:00 +01:00
/**
* Get the list of email addresses to CC.
* @return array
*/
2014-02-25 17:03:00 +01:00
protected function getCcAddresses()
{
2014-02-27 15:04:08 +01:00
$ccAddresses = array();
2014-02-25 17:03:00 +01:00
if (isset($this->options['cc'])) {
foreach ($this->options['cc'] as $address) {
2014-02-27 15:04:08 +01:00
$ccAddresses[] = $address;
2014-02-25 17:03:00 +01:00
}
}
2014-02-27 15:04:08 +01:00
return $ccAddresses;
2014-02-25 17:03:00 +01:00
}
2014-02-27 15:04:08 +01:00
}