Skip to content

Commit

Permalink
convert email classes to use instance config trait
Browse files Browse the repository at this point in the history
Minor code restructuring aound config variables
  • Loading branch information
AD7six committed Mar 29, 2014
1 parent 78c7871 commit 2430bcd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 77 deletions.
23 changes: 7 additions & 16 deletions src/Network/Email/AbstractTransport.php
Expand Up @@ -16,18 +16,22 @@
*/
namespace Cake\Network\Email;

use Cake\Core\InstanceConfigTrait;

/**
* Abstract transport for sending email
*
*/
abstract class AbstractTransport {

use InstanceConfigTrait;

/**
* Configurations
* Default config for this class
*
* @var array
*/
protected $_config = array();
protected $_defaultConfig = [];

/**
* Send mail
Expand All @@ -42,23 +46,10 @@ abstract public function send(Email $email);
*
* @param array $config The configuration data for the transport.
*/
public function __construct($config = null) {
public function __construct($config = []) {
$this->config($config);
}

/**
* Set the config
*
* @param array $config
* @return array Returns configs
*/
public function config($config = null) {
if (is_array($config)) {
$this->_config = $config + $this->_config;
}
return $this->_config;
}

/**
* Help to convert headers in string
*
Expand Down
60 changes: 29 additions & 31 deletions src/Network/Email/SmtpTransport.php
Expand Up @@ -24,6 +24,21 @@
*/
class SmtpTransport extends AbstractTransport {

/**
* Default config for this class
*
* @var array
*/
protected $_defaultConfig = [
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => null,
'password' => null,
'client' => null,
'tls' => false
];

/**
* Socket to SMTP server
*
Expand Down Expand Up @@ -64,29 +79,6 @@ public function send(Email $email) {
return $this->_content;
}

/**
* Set the configuration
*
* @param array $config
* @return array Returns configs
*/
public function config($config = null) {
if ($config === null) {
return $this->_config;
}
$default = array(
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => null,
'password' => null,
'client' => null,
'tls' => false
);
$this->_config = array_merge($default, $this->_config, $config);
return $this->_config;
}

/**
* Connect to SMTP Server
*
Expand All @@ -100,8 +92,10 @@ protected function _connect() {
}
$this->_smtpSend(null, '220');

if (isset($this->_config['client'])) {
$host = $this->_config['client'];
$config = $this->_config;

if (isset($config['client'])) {
$host = $config['client'];
} elseif ($httpHost = env('HTTP_HOST')) {
list($host) = explode(':', $httpHost);
} else {
Expand All @@ -110,13 +104,13 @@ protected function _connect() {

try {
$this->_smtpSend("EHLO {$host}", '250');
if ($this->_config['tls']) {
if ($config['tls']) {
$this->_smtpSend("STARTTLS", '220');
$this->_socket->enableCrypto('tls');
$this->_smtpSend("EHLO {$host}", '250');
}
} catch (Error\SocketException $e) {
if ($this->_config['tls']) {
if ($config['tls']) {
throw new Error\SocketException('SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.');
}
try {
Expand All @@ -134,13 +128,14 @@ protected function _connect() {
* @throws \Cake\Error\SocketException
*/
protected function _auth() {
if (isset($this->_config['username']) && isset($this->_config['password'])) {
$config = $this->_config;
if (isset($config['username']) && isset($config['password'])) {
$authRequired = $this->_smtpSend('AUTH LOGIN', '334|503');
if ($authRequired == '334') {
if (!$this->_smtpSend(base64_encode($this->_config['username']), '334')) {
if (!$this->_smtpSend(base64_encode($config['username']), '334')) {
throw new Error\SocketException('SMTP server did not accept the username.');
}
if (!$this->_smtpSend(base64_encode($this->_config['password']), '235')) {
if (!$this->_smtpSend(base64_encode($config['password']), '235')) {
throw new Error\SocketException('SMTP server did not accept the password.');
}
} elseif ($authRequired == '504') {
Expand Down Expand Up @@ -231,10 +226,13 @@ protected function _smtpSend($data, $checkCode = '250') {
if ($data !== null) {
$this->_socket->write($data . "\r\n");
}

$timeout = $this->_config['timeout'];

while ($checkCode !== false) {
$response = '';
$startTime = time();
while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $this->_config['timeout'])) {
while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $timeout)) {
$response .= $this->_socket->read();
}
if (substr($response, -2) !== "\r\n") {
Expand Down
43 changes: 13 additions & 30 deletions src/Network/Http/Client.php
Expand Up @@ -14,6 +14,7 @@
namespace Cake\Network\Http;

use Cake\Core\App;
use Cake\Core\InstanceConfigTrait;
use Cake\Error;
use Cake\Network\Http\Cookies;
use Cake\Network\Http\Request;
Expand Down Expand Up @@ -87,12 +88,15 @@
*/
class Client {

use InstanceConfigTrait;

/**
* Stored configuration for the client.
* Default configuration for the client.
*
* @var array
*/
protected $_config = [
protected $_defaultConfig = [
'adapter' => 'Cake\Network\Http\Adapter\Stream',
'host' => null,
'port' => null,
'scheme' => 'http',
Expand Down Expand Up @@ -143,42 +147,21 @@ class Client {
* @param array $config Config options for scoped clients.
*/
public function __construct($config = []) {
$adapter = 'Cake\Network\Http\Adapter\Stream';
if (isset($config['adapter'])) {
$adapter = $config['adapter'];
unset($config['adapter']);
}
if (isset($config['cookieJar'])) {
$this->_cookies = $config['cookieJar'];
unset($config['cookieJar']);
}
if (empty($this->_cookies)) {
$this->_cookies = new Cookies();
}

$this->config($config);

$adapter = $this->_config['adapter'];
$this->config('adapter', null);
if (is_string($adapter)) {
$adapter = new $adapter();
}
$this->_adapter = $adapter;
}

/**
* Get or set additional config options.
*
* Setting config will use Hash::merge() for appending into
* the existing configuration.
*
* @param array|null $config Configuration options. null to get.
* @return this|array
*/
public function config($config = null) {
if ($config === null) {
return $this->_config;
if (!empty($this->_config['cookieJar'])) {
$this->_cookies = $this->_config['cookieJar'];
$this->config('cookieJar', null);
} else {
$this->_cookies = new Cookies();
}
$this->_config = Hash::merge($this->_config, $config);
return $this;
}

/**
Expand Down

0 comments on commit 2430bcd

Please sign in to comment.