<commit_before><?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright    Copyright 2013, Block 8 Limited.
* @license      https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link         http://www.phptesting.org/
*/

namespace PHPCI\Plugin;


/**
* Email Plugin - Provides simple email capability to PHPCI.
* @author       <NAME> <<EMAIL>>
* @package      PHPCI
* @subpackage   Plugins
*/
class Email implements \PHPCI\Plugin
{
    
    /**
     * @var \PHPCI\Builder
     */
    protected $phpci;

    /**
     * @var array
     */
    protected $options;

    /**
     * @var array
     */
    protected $emailConfig;

    /**
     * @var \Swift_Mailer
     */
    protected $mailer;

    public function __construct(\PHPCI\Builder $phpci,
                                array $options = array(),
                                \Swift_Mailer $mailer = null)
    {
        $phpCiSettings      = $phpci->getSystemConfig('phpci');
        $this->phpci        = $phpci;
        $this->options      = $options;
        $this->emailConfig  = isset($phpCiSettings['email_settings']) ? $phpCiSettings['email_settings'] : array();

        // Either a mailer will have been passed in or we load from the
        // config.
        if ($mailer === null) {
            $this->loadSwiftMailerFromConfig();
        }
        else {
            $this->mailer = $mailer;
        }
    }

    /**
    * Connects to MySQL and runs a specified set of queries.
    */
    public function execute()
    {

        return true;
    }

    /**
     * @param array|string $toAddresses   Array or single address to send to
     * @param string       $subject       Email subject
     * @param string       $body          Email body
     * @return array                      Array of failed addresses
     */
    public function sendEmail($toAddresses, $subject, $body)
    {
        $message = \Swift_Message::newInstance($subject)
            ->setFrom($this->getMailConfig('from_address'))
            ->setTo($toAddresses)
            ->setBody($body);
        $failedAddresses = array();
        $this->mailer->send($message, $failedAddresses);

        return $failedAddresses;
    }

    protected function loadSwiftMailerFromConfig()
    {
        /** @var \Swift_SmtpTransport $transport */
        $transport = \Swift_SmtpTransport::newInstance(
            $this->getMailConfig('smtp_address'),
            $this->getMailConfig('smtp_port')
        );
        $transport->setUsername($this->getMailConfig('smtp_username'));
        $transport->setPassword($this->getMailConfig('smtp_password'));

        $this->mailer = \Swift_Mailer::newInstance($transport);
    }

    protected function getMailConfig($configName)
    {
        if (isset($this->emailConfig[$configName])
            && $this->emailConfig[$configName] != "")
        {
            return $this->emailConfig[$configName];
        }
        // Check defaults
        else {
            switch($configName) {
                case 'smtp_address':<commit_msg>Email plugin now looks in the yaml file for an addresses setting. All these addresses will be mailed with a pass or fail message.<commit_after><?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright    Copyright 2013, Block 8 Limited.
* @license      https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link         http://www.phptesting.org/
*/

namespace PHPCI\Plugin;


/**
* Email Plugin - Provides simple email capability to PHPCI.
* @author       <NAME> <<EMAIL>>
* @package      PHPCI
* @subpackage   Plugins
*/
class Email implements \PHPCI\Plugin
{
    
    /**
     * @var \PHPCI\Builder
     */
    protected $phpci;

    /**
     * @var array
     */
    protected $options;

    /**
     * @var array
     */
    protected $emailConfig;

    /**
     * @var \Swift_Mailer
     */
    protected $mailer;

    public function __construct(\PHPCI\Builder $phpci,
                                array $options = array(),
                                \Swift_Mailer $mailer = null)
    {
        $phpCiSettings      = $phpci->getSystemConfig('phpci');
        $this->phpci        = $phpci;
        $this->options      = $options;
        $this->emailConfig  = isset($phpCiSettings['email_settings']) ? $phpCiSettings['email_settings'] : array();

        // Either a mailer will have been passed in or we load from the
        // config.
        if ($mailer === null) {
            $this->loadSwiftMailerFromConfig();
        }
        else {
            $this->mailer = $mailer;
        }
    }

    /**
    * Connects to MySQL and runs a specified set of queries.
    */
    public function execute()
    {
        // Without some email addresses in the yml file then we
        // can't do anything.
        if (!isset($this->options['addresses'])) {
            return false;
        }

        $addresses = $this->options['addresses'];
        $sendFailures = array();

        if($this->phpci->getSuccessStatus()) {
            $body = "";
            $sendFailures = $this->sendSeparateEmails(
                $addresses,
                "PASSED",
                $body
            );
        }
        else {
            $body = "";
            $sendFailures = $this->sendSeparateEmails(
                $addresses,
                "FAILED",
                $body
            );
        }

        // This is a success if we've not failed to send anything.
        return (count($sendFailures) == 0);
    }

    /**
     * @param array|string $toAddresses   Array or single address to send to
     * @param string       $subject       Email subject
     * @param string       $body          Email body
     * @return array                      Array of failed addresses
     */
    public function sendEmail($toAddresses, $subject, $body)
    {
        $message = \Swift_Message::newInstance($subject)
            ->setFrom($this->getMailConfig('from_address'))
            ->setTo($toAddresses)
            ->setBody($body);
        $failedAddresses = array();
        $this->mailer->send($message, $failedAddresses);

        return $failedAddresses;
    }

    public function sendSeparateEmails(array $toAddresses, $subject, $body)
    {
        $failures = array();
        foreach($toAddresses as $address) {
            $newFailures = $this->sendEmail($address, $subject, $body);
            foreach($newFailures as $failure) {
                $failures[] = $failure;
            }
        }
        return $failures;
    }

    protected function loadSwiftMailerFromConfig()
    {
        /** @var \Swift_SmtpTransport $transport */
        $transport = \Swift_SmtpTransport::newInstance(
            $this->getMailConfig('smtp_address'),
            $this->getMailConfig('smtp_port')
        );
        $transport->setUsername($this->getMailConfig('smtp_username'));
        $transport->setPassword($this->getMailConfig('smtp_password'));

        $this->mailer = \Swift_Mailer::newInstance($transport);
    }

    protected function getMailConfig($configName)
    {
        if (isset($this->emailConfig[$configName])
            && $this->emailConfig[$configName] != "")
        {
            return $this->emailConfig[$configName];
        }
        // Check defaults
        else {
            switch($configName) {
                case 'smtp_address':
{'fraction_non_alphanumeric': 0.12646657571623465, 'fraction_numerical': 0.0017735334242837653, 'mean_word_length': 1.8614363778298204, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 60, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3789591', 'n_tokens_mistral': 2104, 'n_tokens_neox': 2003, 'n_words': 449}