2013-05-31 13:56:35 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-12 18:26:17 +02:00
|
|
|
* 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;
|
|
|
|
|
2015-04-22 13:24:00 +02:00
|
|
|
use Exception;
|
2014-02-24 22:04:08 +01:00
|
|
|
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;
|
2014-11-11 22:58:23 +01:00
|
|
|
use PHPCI\Helper\Email as EmailHelper;
|
2015-04-22 13:24:00 +02:00
|
|
|
use Psr\Log\LogLevel;
|
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;
|
|
|
|
|
|
|
|
/**
|
2014-11-11 22:58:23 +01:00
|
|
|
* @var \PHPCI\Model\Build
|
2013-06-01 10:40:05 +02:00
|
|
|
*/
|
2014-11-11 22:58:23 +01:00
|
|
|
protected $build;
|
2013-06-01 10:40:05 +02:00
|
|
|
|
|
|
|
/**
|
2014-11-11 22:58:23 +01:00
|
|
|
* @var array
|
2013-06-01 10:40:05 +02:00
|
|
|
*/
|
2014-11-11 22:58:23 +01:00
|
|
|
protected $options;
|
2013-06-01 10:40:05 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
array $options = array()
|
|
|
|
) {
|
2015-10-23 05:18:22 +02:00
|
|
|
$this->phpci = $phpci;
|
|
|
|
$this->build = $build;
|
|
|
|
$this->options = $options;
|
2013-05-31 13:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-05 14:27:26 +01:00
|
|
|
* Send a notification mail.
|
2014-11-11 22:58:23 +01:00
|
|
|
*/
|
2013-05-31 13:56:35 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
2013-06-04 22:09:16 +02:00
|
|
|
$addresses = $this->getEmailAddresses();
|
|
|
|
|
2013-06-01 14:57:39 +02:00
|
|
|
// Without some email addresses in the yml file then we
|
|
|
|
// can't do anything.
|
2013-06-04 22:09:16 +02:00
|
|
|
if (count($addresses) == 0) {
|
2013-06-01 14:57:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-12 00:20:17 +01:00
|
|
|
$buildStatus = $this->build->isSuccessful() ? "Passing Build" : "Failing Build";
|
|
|
|
$projectName = $this->build->getProject()->getTitle();
|
2013-05-31 13:56:35 +02:00
|
|
|
|
2015-04-22 13:24:00 +02:00
|
|
|
try {
|
|
|
|
$view = $this->getMailTemplate();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->phpci->log(
|
|
|
|
sprintf('Unknown mail template "%s", falling back to default.', $this->options['template']),
|
|
|
|
LogLevel::WARNING
|
|
|
|
);
|
|
|
|
$view = $this->getDefaultMailTemplate();
|
|
|
|
}
|
|
|
|
|
2014-11-11 22:58:23 +01:00
|
|
|
$view->build = $this->build;
|
|
|
|
$view->project = $this->build->getProject();
|
2015-04-22 13:24:00 +02:00
|
|
|
|
|
|
|
$layout = new View('Email/layout');
|
|
|
|
$layout->build = $this->build;
|
|
|
|
$layout->project = $this->build->getProject();
|
|
|
|
$layout->content = $view->render();
|
|
|
|
$body = $layout->render();
|
2014-12-04 16:48:52 +01:00
|
|
|
|
2014-11-11 22:58:23 +01:00
|
|
|
$sendFailures = $this->sendSeparateEmails(
|
|
|
|
$addresses,
|
|
|
|
sprintf("PHPCI - %s - %s", $projectName, $buildStatus),
|
|
|
|
$body
|
|
|
|
);
|
|
|
|
|
|
|
|
// This is a success if we've not failed to send anything.
|
|
|
|
$this->phpci->log(sprintf("%d emails sent", (count($addresses) - $sendFailures)));
|
|
|
|
$this->phpci->log(sprintf("%d emails failed to send", $sendFailures));
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2014-11-11 22:58:23 +01:00
|
|
|
return ($sendFailures === 0);
|
2013-05-31 13:56:35 +02:00
|
|
|
}
|
2013-06-01 10:40:05 +02:00
|
|
|
|
|
|
|
/**
|
2014-11-11 22:58:23 +01:00
|
|
|
* @param string $toAddress Single address to send to
|
2014-02-25 23:39:37 +01:00
|
|
|
* @param string[] $ccList
|
|
|
|
* @param string $subject Email subject
|
|
|
|
* @param string $body Email body
|
2013-06-01 10:40:05 +02:00
|
|
|
* @return array Array of failed addresses
|
|
|
|
*/
|
2015-04-22 13:24:00 +02:00
|
|
|
protected function sendEmail($toAddress, $ccList, $subject, $body)
|
2013-06-01 10:40:05 +02:00
|
|
|
{
|
2014-11-11 22:58:23 +01:00
|
|
|
$email = new EmailHelper();
|
|
|
|
|
|
|
|
$email->setEmailTo($toAddress, $toAddress);
|
|
|
|
$email->setSubject($subject);
|
|
|
|
$email->setBody($body);
|
2015-02-16 18:51:05 +01:00
|
|
|
$email->setHtml(true);
|
2014-02-25 17:03:00 +01:00
|
|
|
|
|
|
|
if (is_array($ccList) && count($ccList)) {
|
2014-11-11 22:58:23 +01:00
|
|
|
foreach ($ccList as $address) {
|
2014-11-12 13:44:56 +01:00
|
|
|
$email->addCc($address, $address);
|
2014-11-11 22:58:23 +01:00
|
|
|
}
|
2014-02-25 17:03:00 +01:00
|
|
|
}
|
|
|
|
|
2014-11-11 22:58:23 +01:00
|
|
|
return $email->send();
|
2013-06-01 10:40:05 +02:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
2014-11-11 22:58:23 +01:00
|
|
|
* Send an email to a list of specified subjects.
|
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @param array $toAddresses
|
2015-06-25 16:21:23 +02:00
|
|
|
* List of destination addresses for message.
|
2014-11-11 22:58:23 +01:00
|
|
|
* @param string $subject
|
|
|
|
* Mail subject
|
|
|
|
* @param string $body
|
|
|
|
* Mail body
|
|
|
|
*
|
|
|
|
* @return int number of failed messages
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2013-06-01 14:57:39 +02:00
|
|
|
public function sendSeparateEmails(array $toAddresses, $subject, $body)
|
|
|
|
{
|
2014-11-11 22:58:23 +01:00
|
|
|
$failures = 0;
|
2014-02-25 17:03:00 +01:00
|
|
|
$ccList = $this->getCcAddresses();
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
foreach ($toAddresses as $address) {
|
2014-11-11 22:58:23 +01:00
|
|
|
if (!$this->sendEmail($address, $ccList, $subject, $body)) {
|
|
|
|
$failures++;
|
2013-06-01 14:57:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $failures;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Get the list of email addresses to send to.
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-06-04 22:09:16 +02:00
|
|
|
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;
|
|
|
|
}
|
2013-06-04 22:09:16 +02:00
|
|
|
|
|
|
|
if (isset($this->options['addresses'])) {
|
|
|
|
foreach ($this->options['addresses'] as $address) {
|
|
|
|
$addresses[] = $address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 16:51:15 +01:00
|
|
|
if (empty($addresses) && isset($this->options['default_mailto_address'])) {
|
2013-06-04 22:09:16 +02:00
|
|
|
$addresses[] = $this->options['default_mailto_address'];
|
|
|
|
}
|
2015-01-08 16:51:15 +01:00
|
|
|
|
2015-01-08 16:48:28 +01:00
|
|
|
return array_unique($addresses);
|
2013-06-04 22:09:16 +02:00
|
|
|
}
|
2014-02-25 17:03:00 +01:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Get the list of email addresses to CC.
|
2015-04-22 13:24:00 +02:00
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @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
|
|
|
}
|
2015-04-22 13:24:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mail template used to sent the mail.
|
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
protected function getMailTemplate()
|
|
|
|
{
|
|
|
|
if (isset($this->options['template'])) {
|
|
|
|
return new View('Email/' . $this->options['template']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getDefaultMailTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default mail template.
|
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
protected function getDefaultMailTemplate()
|
|
|
|
{
|
|
|
|
$template = $this->build->isSuccessful() ? 'short' : 'long';
|
|
|
|
|
|
|
|
return new View('Email/' . $template);
|
|
|
|
}
|
2014-02-27 15:04:08 +01:00
|
|
|
}
|