From 706d9d67f6b426eab4697166292a8d7076cf64e9 Mon Sep 17 00:00:00 2001 From: Renon Stewart Date: Fri, 31 Aug 2018 15:28:05 -0400 Subject: [PATCH] Code refactor to address Issues #123 --- Block/Adminhtml/EmailTest.php | 128 +++++++++++++++++++++++++++++----- Helper/Data.php | 50 ++++++++++--- composer.json | 2 +- 3 files changed, 151 insertions(+), 29 deletions(-) diff --git a/Block/Adminhtml/EmailTest.php b/Block/Adminhtml/EmailTest.php index 84c1d79..c12f5e3 100644 --- a/Block/Adminhtml/EmailTest.php +++ b/Block/Adminhtml/EmailTest.php @@ -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 @@ -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; @@ -86,6 +174,9 @@ protected function init() $this->hash = time() . '.' . rand(300000, 900000); } + /** + * @return array + */ public function verify() { $settings = [ @@ -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)) { @@ -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 @@ -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') { @@ -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; @@ -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 diff --git a/Helper/Data.php b/Helper/Data.php index 1ed6ce2..21bee9a 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -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 @@ -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 @@ -202,7 +230,7 @@ public function getScopeConfigValue($path, $store_id = null) */ public function getStoreId() { - return $this->_storeId; + return $this->storeId; } /** @@ -210,7 +238,7 @@ public function getStoreId() */ public function setStoreId($storeId = null) { - $this->_storeId = $storeId; + $this->storeId = $storeId; } /** @@ -218,14 +246,16 @@ public function setStoreId($storeId = null) */ 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; } } diff --git a/composer.json b/composer.json index ccfbb5f..9271e23 100644 --- a/composer.json +++ b/composer.json @@ -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" ],