Skip to content

Commit

Permalink
Code refactor to address Issues #123
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Aug 31, 2018
1 parent f229cab commit 706d9d6
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 29 deletions.
128 changes: 110 additions & 18 deletions Block/Adminhtml/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ class EmailTest extends \Magento\Backend\Block\Template
*/
protected $hash;

/**
* Remove values from global post and store values locally
* @var array()
*/
protected $configFields = [
'active' => '',
'name' => '',
'auth' => '',
'ssl' => '',
'smtphost' => '',
'smtpport' => '',
'username' => '',
'password' => '',
'set_reply_to' => '',
'set_from' => '',
'set_return_path' => '',
'email' => '',
'from_email' => ''
];

/**
* EmailTest constructor.
* @param \Magento\Backend\Block\Template\Context $context
Expand All @@ -61,23 +81,91 @@ public function __construct(
}

/**
* @throws \Zend_Validate_Exception
* @param $id
* @return $this
*/
protected function init()
public function setStoreId($id)
{
$this->storeId = $id;
return $this;
}

/**
* @return int \ null
*/
public function getStoreId()
{
return $this->storeId;
}

/**
* @param null $key
* @return array|mixed|string
*/
public function getConfig($key = null)
{
if ($key === null) {
return $this->configFields;
} elseif (!array_key_exists($key, $this->configFields)) {
return '';
} else {
return $this->configFields[$key];
}
}

/**
* @param null $key
* @param string $value
* @return array|mixed|string
*/
public function setConfig($key, $value = null)
{
if (array_key_exists($key, $this->configFields)) {
$this->configFields[$key] = $value;
}

return $this;
}

/**
* Load default config if config is lock using "bin/magento config:set"
*/
public function loadDefaultConfig()
{
$request = $this->getRequest();
$formPostArray = (array) $request->getPost();

$this->storeId = $request->getParam('store', null);
$fields = array_keys($this->configFields);
foreach ($fields as $field) {
if (!array_key_exists($field, $formPostArray)) {
$this->setConfig($field, $this->_dataHelper->getConfigValue($field), $this->getStoreId());
} else {
$this->setConfig($field, $request->getPost($field));
}
}

//if password mask (6 stars)
if ($request->getPost('password') === '******') {
$password = $this->_dataHelper->getConfigPassword($this->storeId);
$request->setPostValue('password', $password);
if ($this->getConfig('password') === '******') {
$password = $this->_dataHelper->getConfigPassword($this->getStoreId());
$this->setConfig('password', $password);
}

$this->toAddress = $request->getPost('email') ? $request->getPost('email') : $request->getPost('username');
return $this;
}

/**
* @throws \Zend_Validate_Exception
*/
protected function init()
{
$request = $this->getRequest();
$this->setStoreId($request->getParam('store', null));

$this->fromAddress = trim($request->getPost('from_email'));
$this->loadDefaultConfig();

$this->toAddress = $this->getConfig('email') ? $this->getConfig('email') : $this->getConfig('username');

$this->fromAddress = trim($this->getConfig('from_email'));

if (!\Zend_Validate::is($this->fromAddress, 'EmailAddress')) {
$this->fromAddress = $this->toAddress;
Expand All @@ -86,6 +174,9 @@ protected function init()
$this->hash = time() . '.' . rand(300000, 900000);
}

/**
* @return array
*/
public function verify()
{
$settings = [
Expand All @@ -98,7 +189,7 @@ public function verify()
$result = $this->error();
$hasError = false;

foreach ($settings as $key => $functionName) {
foreach ($settings as $functionName) {
$result = call_user_func([$this, $functionName]);

if (array_key_exists('has_error', $result)) {
Expand Down Expand Up @@ -130,10 +221,10 @@ protected function validateServerEmailSetting()
{
$request = $this->getRequest();

$username = $request->getPost('username');
$password = $request->getPost('password');
$username = $this->getConfig('username');
$password = $this->getConfig('password');

$auth = strtolower($request->getPost('auth'));
$auth = strtolower($this->getConfig('auth'));

//if default view
//see https://github.com/magento/magento2/issues/3019
Expand All @@ -147,11 +238,11 @@ protected function validateServerEmailSetting()
}

//SMTP server configuration
$smtpHost = $request->getPost('smtphost');
$smtpHost = $this->getConfig('smtphost');

$smtpConf = [
'name' => $request->getPost('name'),
'port' => $request->getPost('smtpport')
'name' => $this->getConfig('name'),
'port' => $this->getConfig('smtpport')
];

if ($auth != 'none') {
Expand All @@ -160,14 +251,14 @@ protected function validateServerEmailSetting()
$smtpConf['password'] = $password;
}

$ssl = $request->getPost('ssl');
$ssl = $this->getConfig('ssl');
if ($ssl != 'none') {
$smtpConf['ssl'] = $ssl;
}

$transport = new \Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);

$from = trim($request->getPost('from_email'));
$from = trim($this->getConfig('from_email'));
$from = \Zend_Validate::is($from, 'EmailAddress') ? $from : $username;
$this->fromAddress = $from;

Expand Down Expand Up @@ -203,7 +294,8 @@ protected function validateMagentoEmailSetting()
$result = $this->error();

$this->_dataHelper->setTestMode(true);
$this->_dataHelper->setStoreId($this->storeId);
$this->_dataHelper->setStoreId($this->getStoreId());
$this->_dataHelper->setTestConfig($this->getConfig());

try {
$this->_email
Expand Down
50 changes: 40 additions & 10 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,41 @@
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var null $_storeId
* @var null $storeId
*/
protected $_storeId = null;
protected $storeId = null;

/** @var bool $_testMode */
protected $_testMode = false;
/** @var bool $testMode */
protected $testMode = false;

/** @var array $testConfig */
protected $testConfig = [];


/**
* @param null $key
* @return array|mixed|string
*/
public function getTestConfig($key = null)
{
if ($key === null) {
return $this->testConfig;
} elseif (!array_key_exists($key, $this->testConfig)) {
return '';
} else {
return $this->testConfig[$key];
}
}

/**
* @param null $fields
* @return $this
*/
public function setTestConfig($fields)
{
$this->testConfig = (array) $fields;
return $this;
}

/**
* @param null $store_id
Expand Down Expand Up @@ -166,8 +195,7 @@ public function getConfigValue($path, $store_id = null)
{
//send test mail
if ($this->isTestMode()) {
$request = $this->_getRequest();
return $request->getPost($path);
return $this->getTestConfig($path);
}

//return value from core config
Expand Down Expand Up @@ -202,30 +230,32 @@ public function getScopeConfigValue($path, $store_id = null)
*/
public function getStoreId()
{
return $this->_storeId;
return $this->storeId;
}

/**
* @param int/null $storeId
*/
public function setStoreId($storeId = null)
{
$this->_storeId = $storeId;
$this->storeId = $storeId;
}

/**
* @return bool
*/
public function isTestMode()
{
return (bool) $this->_testMode;
return (bool) $this->testMode;
}

/**
* @param bool $testMode
* @return Data
*/
public function setTestMode(bool $testMode)
{
$this->_testMode = $testMode;
$this->testMode = $testMode;
return $this;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"magento/framework": "100.0.*|100.1.*|101.0.*"
},
"type": "magento2-module",
"version": "2.5.3",
"version": "2.5.4",
"license": [
"proprietary"
],
Expand Down

0 comments on commit 706d9d6

Please sign in to comment.