Move the mailer constructor logic out of the plugin and in to a factory class for reuse.
This commit is contained in:
parent
51f73458c8
commit
9d1d2443a9
|
@ -9,6 +9,7 @@
|
|||
|
||||
namespace PHPCI;
|
||||
|
||||
use PHPCI\Helper\MailerFactory;
|
||||
use PHPCI\Model\Build;
|
||||
use b8\Store;
|
||||
use b8\Config;
|
||||
|
@ -507,7 +508,7 @@ class Builder implements LoggerAwareInterface
|
|||
return $self;
|
||||
},
|
||||
null,
|
||||
'PHPCI\\Builder'
|
||||
'PHPCI\Builder'
|
||||
);
|
||||
|
||||
$this->pluginFactory->registerResource(
|
||||
|
@ -515,7 +516,16 @@ class Builder implements LoggerAwareInterface
|
|||
return $build;
|
||||
},
|
||||
null,
|
||||
'PHPCI\\Model\Build'
|
||||
'PHPCI\Model\Build'
|
||||
);
|
||||
|
||||
$this->pluginFactory->registerResource(
|
||||
function () use ($self) {
|
||||
$factory = new MailerFactory($self->getSystemConfig('phpci'));
|
||||
return $factory->getSwiftMailerFromConfig();
|
||||
},
|
||||
null,
|
||||
'\Swift_Mailer'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
58
PHPCI/Helper/MailerFactory.php
Normal file
58
PHPCI/Helper/MailerFactory.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCI\Helper;
|
||||
|
||||
|
||||
class MailerFactory {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $emailConfig;
|
||||
|
||||
public function __construct($phpCiConfig = null)
|
||||
{
|
||||
$this->emailConfig = isset($phpCiSettings['email_settings']) ?: array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of Swift_Mailer based on the config.s
|
||||
* @return \Swift_Mailer
|
||||
*/
|
||||
public function getSwiftMailerFromConfig()
|
||||
{
|
||||
/** @var \Swift_SmtpTransport $transport */
|
||||
$transport = \Swift_SmtpTransport::newInstance(
|
||||
$this->getMailConfig('smtp_address'),
|
||||
$this->getMailConfig('smtp_port'),
|
||||
$this->getMailConfig('smtp_encryption')
|
||||
);
|
||||
$transport->setUsername($this->getMailConfig('smtp_username'));
|
||||
$transport->setPassword($this->getMailConfig('smtp_password'));
|
||||
|
||||
return \Swift_Mailer::newInstance($transport);
|
||||
}
|
||||
|
||||
protected function getMailConfig($configName)
|
||||
{
|
||||
if (isset($this->emailConfig[$configName]) && $this->emailConfig[$configName] != "") {
|
||||
return $this->emailConfig[$configName];
|
||||
} else {
|
||||
// Check defaults
|
||||
|
||||
switch($configName) {
|
||||
case 'smtp_address':
|
||||
return "localhost";
|
||||
case 'default_mailto_address':
|
||||
return null;
|
||||
case 'smtp_port':
|
||||
return '25';
|
||||
case 'smtp_encryption':
|
||||
return null;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -30,34 +30,34 @@ class Email implements \PHPCI\Plugin
|
|||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $emailConfig;
|
||||
|
||||
/**
|
||||
* @var \Swift_Mailer
|
||||
*/
|
||||
protected $mailer;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $fromAddress;
|
||||
|
||||
public function __construct(Builder $phpci,
|
||||
Build $build,
|
||||
array $options = array(),
|
||||
\Swift_Mailer $mailer = null
|
||||
\Swift_Mailer $mailer,
|
||||
array $options = array()
|
||||
|
||||
)
|
||||
{
|
||||
$phpCiSettings = $phpci->getSystemConfig('phpci');
|
||||
$this->phpci = $phpci;
|
||||
$this->build = $build;
|
||||
$this->options = $options;
|
||||
$this->emailConfig = isset($phpCiSettings['email_settings']) ? $phpCiSettings['email_settings'] : array();
|
||||
|
||||
if ($mailer) {
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
else {
|
||||
$this->loadSwiftMailerFromConfig();
|
||||
}
|
||||
$phpCiSettings = $phpci->getSystemConfig('phpci');
|
||||
|
||||
$this->fromAddress = isset($phpCiSettings['email_settings']['from_address'])
|
||||
? $phpCiSettings['email_settings']['from_address']
|
||||
: "notifications-ci@phptesting.org";
|
||||
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,7 +107,7 @@ class Email implements \PHPCI\Plugin
|
|||
public function sendEmail($toAddresses, $subject, $body)
|
||||
{
|
||||
$message = \Swift_Message::newInstance($subject)
|
||||
->setFrom($this->getMailConfig('from_address'))
|
||||
->setFrom($this->fromAddress)
|
||||
->setTo($toAddresses)
|
||||
->setBody($body)
|
||||
->setContentType("text/html");
|
||||
|
@ -129,44 +129,6 @@ class Email implements \PHPCI\Plugin
|
|||
return $failures;
|
||||
}
|
||||
|
||||
protected function loadSwiftMailerFromConfig()
|
||||
{
|
||||
/** @var \Swift_SmtpTransport $transport */
|
||||
$transport = \Swift_SmtpTransport::newInstance(
|
||||
$this->getMailConfig('smtp_address'),
|
||||
$this->getMailConfig('smtp_port'),
|
||||
$this->getMailConfig('smtp_encryption')
|
||||
);
|
||||
$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];
|
||||
} else {
|
||||
// Check defaults
|
||||
|
||||
switch($configName) {
|
||||
case 'smtp_address':
|
||||
return "localhost";
|
||||
case 'default_mailto_address':
|
||||
return null;
|
||||
case 'smtp_port':
|
||||
return '25';
|
||||
case 'smtp_encryption':
|
||||
return null;
|
||||
case 'from_address':
|
||||
return "notifications-ci@phptesting.org";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getEmailAddresses()
|
||||
{
|
||||
$addresses = array();
|
||||
|
@ -188,4 +150,4 @@ class Email implements \PHPCI\Plugin
|
|||
}
|
||||
return $addresses;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -100,8 +100,8 @@ class EmailTest extends \PHPUnit_Framework_TestCase
|
|||
$this->testedEmailPlugin = new EmailPlugin(
|
||||
$this->mockCiBuilder,
|
||||
$this->mockBuild,
|
||||
$arrOptions,
|
||||
$this->mockMailer
|
||||
$this->mockMailer,
|
||||
$arrOptions
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue