From 9d1d2443a925e8817beaed61e2683ceac4153fb1 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sun, 17 Nov 2013 21:26:38 +0000 Subject: [PATCH] Move the mailer constructor logic out of the plugin and in to a factory class for reuse. --- PHPCI/Builder.php | 14 ++++++- PHPCI/Helper/MailerFactory.php | 58 +++++++++++++++++++++++++ PHPCI/Plugin/Email.php | 72 ++++++++------------------------ Tests/PHPCI/Plugin/EmailTest.php | 4 +- 4 files changed, 89 insertions(+), 59 deletions(-) create mode 100644 PHPCI/Helper/MailerFactory.php diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index fe5b8601..01ff41a3 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -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' ); } } diff --git a/PHPCI/Helper/MailerFactory.php b/PHPCI/Helper/MailerFactory.php new file mode 100644 index 00000000..33bb8b74 --- /dev/null +++ b/PHPCI/Helper/MailerFactory.php @@ -0,0 +1,58 @@ +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 ""; + } + } + } + +} \ No newline at end of file diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php index 83a41033..81671e1c 100644 --- a/PHPCI/Plugin/Email.php +++ b/PHPCI/Plugin/Email.php @@ -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; } -} +} \ No newline at end of file diff --git a/Tests/PHPCI/Plugin/EmailTest.php b/Tests/PHPCI/Plugin/EmailTest.php index 6fd5663e..1343068c 100644 --- a/Tests/PHPCI/Plugin/EmailTest.php +++ b/Tests/PHPCI/Plugin/EmailTest.php @@ -100,8 +100,8 @@ class EmailTest extends \PHPUnit_Framework_TestCase $this->testedEmailPlugin = new EmailPlugin( $this->mockCiBuilder, $this->mockBuild, - $arrOptions, - $this->mockMailer + $this->mockMailer, + $arrOptions ); }