2013-11-17 22:26:38 +01: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-11-17 22:26:38 +01:00
|
|
|
|
|
|
|
namespace PHPCI\Helper;
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Class MailerFactory helps to set up and configure a SwiftMailer object.
|
|
|
|
* @package PHPCI\Helper
|
|
|
|
*/
|
2014-05-01 17:53:29 +02:00
|
|
|
class MailerFactory
|
|
|
|
{
|
2013-11-17 22:26:38 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $emailConfig;
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Set the mailer factory configuration.
|
|
|
|
* @param array $config
|
|
|
|
*/
|
|
|
|
public function __construct($config = array())
|
2013-11-17 22:26:38 +01:00
|
|
|
{
|
2014-12-08 12:25:33 +01:00
|
|
|
if (!is_array($config)) {
|
|
|
|
$config = array();
|
|
|
|
}
|
|
|
|
|
2014-05-21 16:56:16 +02:00
|
|
|
$this->emailConfig = isset($config['email_settings']) ? $config['email_settings'] : array();
|
2013-11-17 22:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an instance of Swift_Mailer based on the config.s
|
|
|
|
* @return \Swift_Mailer
|
|
|
|
*/
|
|
|
|
public function getSwiftMailerFromConfig()
|
|
|
|
{
|
2014-10-15 11:55:09 +02:00
|
|
|
$encryptionType = $this->getMailConfig('smtp_encryption');
|
|
|
|
|
|
|
|
// Workaround issue where smtp_encryption could == 1 in the past by
|
|
|
|
// checking it is a valid transport
|
|
|
|
if ($encryptionType && !in_array($encryptionType, stream_get_transports())) {
|
|
|
|
$encryptionType = null;
|
|
|
|
}
|
|
|
|
|
2013-11-17 22:26:38 +01:00
|
|
|
/** @var \Swift_SmtpTransport $transport */
|
|
|
|
$transport = \Swift_SmtpTransport::newInstance(
|
|
|
|
$this->getMailConfig('smtp_address'),
|
|
|
|
$this->getMailConfig('smtp_port'),
|
2014-10-15 11:55:09 +02:00
|
|
|
$encryptionType
|
2013-11-17 22:26:38 +01:00
|
|
|
);
|
|
|
|
$transport->setUsername($this->getMailConfig('smtp_username'));
|
|
|
|
$transport->setPassword($this->getMailConfig('smtp_password'));
|
|
|
|
|
|
|
|
return \Swift_Mailer::newInstance($transport);
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Return a specific configuration value by key.
|
|
|
|
* @param $configName
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2014-07-30 12:58:10 +02:00
|
|
|
public function getMailConfig($configName)
|
2013-11-17 22:26:38 +01:00
|
|
|
{
|
|
|
|
if (isset($this->emailConfig[$configName]) && $this->emailConfig[$configName] != "") {
|
|
|
|
return $this->emailConfig[$configName];
|
|
|
|
} else {
|
|
|
|
// Check defaults
|
|
|
|
|
2015-10-05 15:17:13 +02:00
|
|
|
switch ($configName) {
|
2013-11-17 22:26:38 +01:00
|
|
|
case 'smtp_address':
|
|
|
|
return "localhost";
|
|
|
|
case 'default_mailto_address':
|
|
|
|
return null;
|
|
|
|
case 'smtp_port':
|
|
|
|
return '25';
|
|
|
|
case 'smtp_encryption':
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-01 17:53:29 +02:00
|
|
|
}
|