improve code

This commit is contained in:
Daniel Seifert 2024-06-04 14:37:41 +02:00
parent 8900201ee8
commit 32d3b178f8
8 changed files with 76 additions and 89 deletions

View File

@ -22,7 +22,7 @@ class MailCheckMenu extends AdminListController
{ {
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailCheckMenu'; protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailCheckMenu';
public function render() public function render(): string
{ {
$this->setEditObjectId('foo'); $this->setEditObjectId('foo');
return parent::render(); return parent::render();

View File

@ -26,9 +26,9 @@ use OxidEsales\Eshop\Core\Registry;
class MailConfigCheck extends AdminDetailsController class MailConfigCheck extends AdminDetailsController
{ {
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailConfigCheck'; protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailConfigCheck';
protected $testMailAddress = 'test@example.com'; protected string $testMailAddress = 'test@example.com';
public function render() public function render(): string
{ {
$this->checkDataAreSet(); $this->checkDataAreSet();
$this->addTplParam('shop', Registry::getConfig()->getActiveShop()); $this->addTplParam('shop', Registry::getConfig()->getActiveShop());
@ -36,7 +36,7 @@ class MailConfigCheck extends AdminDetailsController
return parent::render(); return parent::render();
} }
protected function checkDataAreSet() protected function checkDataAreSet(): void
{ {
try { try {
/** @var Shop $shop */ /** @var Shop $shop */
@ -64,12 +64,12 @@ class MailConfigCheck extends AdminDetailsController
} }
} }
public function checkConfiguration() public function checkConfiguration(): void
{ {
$this->getCurrentMailer(); $this->getCurrentMailer();
} }
protected function getCurrentMailer() protected function getCurrentMailer(): void
{ {
try { try {
$shop = Registry::getConfig()->getActiveShop(); $shop = Registry::getConfig()->getActiveShop();

View File

@ -16,6 +16,7 @@ namespace D3\MailConfigChecker\Application\Controller\Admin;
use Assert\Assert; use Assert\Assert;
use D3\MailConfigChecker\Application\Model\Constants; use D3\MailConfigChecker\Application\Model\Constants;
use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException; use D3\MailConfigChecker\Application\Model\Exception\d3TranslatableLazyAssertionException;
use Exception;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController; use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\Shop; use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Email; use OxidEsales\Eshop\Core\Email;
@ -25,7 +26,7 @@ class MailTester extends AdminDetailsController
{ {
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailTester'; protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/mailTester';
public function sendMail() public function sendMail(): void
{ {
try { try {
$request = Registry::getRequest(); $request = Registry::getRequest();
@ -45,12 +46,12 @@ class MailTester extends AdminDetailsController
$mail->setFrom($from); $mail->setFrom($from);
$mail->sendEmail($to, $subject, $body); $mail->sendEmail($to, $subject, $body);
$this->addTplParam('success', true); $this->addTplParam('success', true);
} catch (\Exception $e) { } catch ( Exception $e) {
Registry::getUtilsView()->addErrorToDisplay(nl2br($e->getMessage())); Registry::getUtilsView()->addErrorToDisplay(nl2br($e->getMessage()));
} }
} }
protected function assertContent() protected function assertContent(): void
{ {
$request = Registry::getRequest(); $request = Registry::getRequest();
$lang = Registry::getLang(); $lang = Registry::getLang();
@ -76,7 +77,7 @@ class MailTester extends AdminDetailsController
->verifyNow(); ->verifyNow();
} }
public function getMailAddressList() public function getMailAddressList(): array
{ {
/** @var Shop $shop */ /** @var Shop $shop */
$shop = Registry::getConfig()->getActiveShop(); $shop = Registry::getConfig()->getActiveShop();

View File

@ -27,18 +27,18 @@ use PEAR_Error;
class SmtpChecker extends AdminDetailsController class SmtpChecker extends AdminDetailsController
{ {
protected $debug = true; protected bool $debug = true;
protected $host; protected string $host;
protected $port; protected int $port;
protected $user; protected string $user;
protected $pwd; protected string $pwd;
protected $from; protected string $from;
protected $to; protected ?string $to = null;
protected $smtp; protected Net_SMTP $smtp;
protected $action; protected string $action = '';
protected $log = []; protected array $log = [];
public function __construct() public function __construct()
{ {
@ -69,19 +69,19 @@ class SmtpChecker extends AdminDetailsController
$this->addTplParam('smtpLog', $this->log); $this->addTplParam('smtpLog', $this->log);
} }
public function getTemplateName() public function getTemplateName(): string
{ {
return '@'.Constants::OXID_MODULE_ID.'/admin/smtpCheck'; return '@'.Constants::OXID_MODULE_ID.'/admin/smtpCheck';
} }
public function render() public function render(): ?string
{ {
$this->addTplParam('shop', Registry::getConfig()->getActiveShop()); $this->addTplParam('shop', Registry::getConfig()->getActiveShop());
return parent::render(); return parent::render();
} }
public function getMailAddressList() public function getMailAddressList(): array
{ {
/** @var Shop $shop */ /** @var Shop $shop */
$shop = Registry::getConfig()->getActiveShop(); $shop = Registry::getConfig()->getActiveShop();
@ -97,7 +97,7 @@ class SmtpChecker extends AdminDetailsController
); );
} }
public function sendMail() public function sendMail(): void
{ {
try { try {
$this->hostIsAvailable(); $this->hostIsAvailable();
@ -120,7 +120,7 @@ class SmtpChecker extends AdminDetailsController
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @return void * @return void
*/ */
protected function hostIsAvailable() protected function hostIsAvailable(): void
{ {
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
Assert::that( Assert::that(
@ -133,7 +133,7 @@ class SmtpChecker extends AdminDetailsController
$this->smtp->setDebug($this->debug, [$this, 'dumpDebug']); $this->smtp->setDebug($this->debug, [$this, 'dumpDebug']);
} }
protected function connect() protected function connect(): void
{ {
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
Assert::that( Assert::that(
@ -150,7 +150,7 @@ class SmtpChecker extends AdminDetailsController
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @return void * @return void
*/ */
protected function auth() protected function auth(): void
{ {
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
Assert::that( Assert::that(
@ -163,7 +163,7 @@ class SmtpChecker extends AdminDetailsController
); );
} }
protected function setFrom() protected function setFrom(): void
{ {
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
Assert::that( Assert::that(
@ -176,8 +176,12 @@ class SmtpChecker extends AdminDetailsController
); );
} }
protected function setRecipient() protected function setRecipient(): void
{ {
if (!$this->to) {
return;
}
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
Assert::that( Assert::that(
$this->smtp->rcptTo($this->to), $this->smtp->rcptTo($this->to),
@ -189,7 +193,7 @@ class SmtpChecker extends AdminDetailsController
); );
} }
protected function sendContent() protected function sendContent(): void
{ {
if (!Registry::getRequest()->getRequestEscapedParameter('sendmail')) { if (!Registry::getRequest()->getRequestEscapedParameter('sendmail')) {
return; return;
@ -208,13 +212,13 @@ class SmtpChecker extends AdminDetailsController
); );
} }
protected function disconnect() protected function disconnect(): void
{ {
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
$this->smtp->disconnect(); $this->smtp->disconnect();
} }
public function dumpDebug($smtp, $message) public function dumpDebug($smtp, $message): void
{ {
unset($smtp); unset($smtp);
@ -224,7 +228,7 @@ class SmtpChecker extends AdminDetailsController
$this->log[$this->action][] = trim(htmlentities($message)); $this->log[$this->action][] = trim(htmlentities($message));
} }
public function formatMessage(array $arguments) public function formatMessage(array $arguments): ?string
{ {
/** @var PEAR_Error|true $response */ /** @var PEAR_Error|true $response */
$response = $arguments['value']; $response = $arguments['value'];

View File

@ -21,6 +21,7 @@ use D3\MailAuthenticationCheck\Model\DMARCResult;
use D3\MailConfigChecker\Application\Model\Constants; use D3\MailConfigChecker\Application\Model\Constants;
use D3\MailConfigChecker\Application\Model\DmarcResult as OxDmarcResult; use D3\MailConfigChecker\Application\Model\DmarcResult as OxDmarcResult;
use D3\MailConfigChecker\Application\Model\SpfResult; use D3\MailConfigChecker\Application\Model\SpfResult;
use LogicException;
use Mika56\SPFCheck\DNS\DNSRecordGetter; use Mika56\SPFCheck\DNS\DNSRecordGetter;
use Mika56\SPFCheck\Model\Query; use Mika56\SPFCheck\Model\Query;
use Mika56\SPFCheck\Model\Result; use Mika56\SPFCheck\Model\Result;
@ -33,14 +34,14 @@ class SpfChecker extends AdminDetailsController
{ {
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/spfCheck'; protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/spfCheck';
public function render() public function render(): ?string
{ {
$this->checkSpf(); $this->checkSpf();
$this->checkDmarc(); $this->checkDmarc();
return parent::render(); return parent::render();
} }
protected function checkSpf() protected function checkSpf(): void
{ {
$result = []; $result = [];
$mailDomains = $this->getMailDomains(); $mailDomains = $this->getMailDomains();
@ -54,7 +55,7 @@ class SpfChecker extends AdminDetailsController
$this->addTplParam('spf_result', $result); $this->addTplParam('spf_result', $result);
} }
protected function checkDmarc() protected function checkDmarc(): void
{ {
$result = []; $result = [];
$mailDomains = $this->getMailDomains(); $mailDomains = $this->getMailDomains();
@ -80,13 +81,13 @@ class SpfChecker extends AdminDetailsController
function ($mailAddress) { function ($mailAddress) {
$mailAddress = trim($mailAddress); $mailAddress = trim($mailAddress);
try { try {
if (! strstr($mailAddress, '@')) { if ( ! str_contains( $mailAddress, '@' ) ) {
throw oxNew(InvalidArgumentException::class); throw oxNew(InvalidArgumentException::class);
} }
$addressChunks = explode('@', $mailAddress); $addressChunks = explode('@', $mailAddress);
return array_pop($addressChunks); return array_pop($addressChunks);
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException) {
return ''; return '';
} }
}, },
@ -100,25 +101,17 @@ class SpfChecker extends AdminDetailsController
); );
} }
protected function checkSpfByDomain($domain, &$summarize) protected function checkSpfByDomain($domain, &$summarize): void
{ {
$checker = new SPFCheck(new DNSRecordGetter()); $checker = new SPFCheck(new DNSRecordGetter());
$query = new Query('', $domain); $query = new Query('', $domain);
$result = $checker->getResult($query); $result = $checker->getResult($query);
switch ($result->getResult()) { $status = match ( $result->getResult() ) {
case Result::FAIL: Result::FAIL, Result::NEUTRAL, Result::PASS, Result::SOFTFAIL => SpfResult::SET,
case Result::NEUTRAL: Result::NONE => SpfResult::MISSING,
case Result::PASS: default => SpfResult::ERROR,
case Result::SOFTFAIL: };
$status = SpfResult::SET;
break;
case Result::NONE:
$status = SpfResult::MISSING;
break;
default:
$status = SpfResult::ERROR;
}
$rawRecord = ($record = $result->getRecord()) ? $rawRecord = ($record = $result->getRecord()) ?
$record->getRawRecord() : $record->getRawRecord() :
@ -127,52 +120,40 @@ class SpfChecker extends AdminDetailsController
$summarize[$domain] = oxNew(SpfResult::class, $status, $rawRecord); $summarize[$domain] = oxNew(SpfResult::class, $status, $rawRecord);
} }
public function getSpfStatusColor(SpfResult $result) public function getSpfStatusColor(SpfResult $result): string
{ {
switch ($result->getStatus()) { return match ( $result->getStatus() ) {
case SpfResult::SET: SpfResult::SET => 'success',
return 'success'; SpfResult::ERROR => 'warning',
case SpfResult::ERROR: default => 'danger',
return 'warning'; };
default:
return 'danger';
}
} }
public function checkDmarcByDomain($domain, &$summarize) public function checkDmarcByDomain($domain, &$summarize): void
{ {
try { try {
$check = new DMARCCheck(new DNSRecordGetter()); $check = new DMARCCheck(new DNSRecordGetter());
$query = new Query('', $domain); $query = new Query('', $domain);
$record = $check->getResult($query)->getRecord(); $record = $check->getResult($query)->getRecord();
switch ( $record->getRejectPolicy()->getValue() ) { $status = match ( $record->getRejectPolicy()->getValue() ) {
case DMARCResult::REJECT_QUARANTINE: DMARCResult::REJECT_QUARANTINE, DMARCResult::REJECT_REJECT => OxDmarcResult::SET,
case DMARCResult::REJECT_REJECT: DMARCResult::REJECT_NONE => OxDmarcResult::MISSING,
$status = OxDmarcResult::SET; default => OxDmarcResult::ERROR,
break; };
case DMARCResult::REJECT_NONE:
$status = OxDmarcResult::MISSING;
break;
default:
$status = OxDmarcResult::ERROR;
}
$summarize[$domain] = oxNew( OxDmarcResult::class, $status, $record->getRawRecord()); $summarize[$domain] = oxNew( OxDmarcResult::class, $status, $record->getRawRecord());
} catch (\LogicException) { } catch ( LogicException) {
$summarize[$domain] = oxNew( OxDmarcResult::class, OxDmarcResult::MISSING, ''); $summarize[$domain] = oxNew( OxDmarcResult::class, OxDmarcResult::MISSING, '');
} }
} }
public function getDmarcStatusColor(OxDmarcResult $result) public function getDmarcStatusColor(OxDmarcResult $result): string
{ {
switch ($result->getStatus()) { return match ( $result->getStatus() ) {
case SpfResult::SET: SpfResult::SET => 'success',
return 'success'; SpfResult::ERROR => 'warning',
case SpfResult::ERROR: default => 'danger',
return 'warning'; };
default:
return 'danger';
}
} }
} }

View File

@ -30,12 +30,12 @@ class DmarcResult
$this->record = $record; $this->record = $record;
} }
public function getStatus() public function getStatus(): string
{ {
return $this->status; return $this->status;
} }
public function getRecord() public function getRecord(): ?string
{ {
return $this->record; return $this->record;
} }

View File

@ -21,8 +21,8 @@ class SpfResult
public const MISSING = 'missing'; public const MISSING = 'missing';
public const ERROR = 'error'; public const ERROR = 'error';
protected $status; protected string $status;
protected $record; protected ?string $record;
public function __construct(string $status, ?string $record = null) public function __construct(string $status, ?string $record = null)
{ {
@ -30,12 +30,12 @@ class SpfResult
$this->record = $record; $this->record = $record;
} }
public function getStatus() public function getStatus(): string
{ {
return $this->status; return $this->status;
} }
public function getRecord() public function getRecord(): ?string
{ {
return $this->record; return $this->record;
} }

View File

@ -34,6 +34,7 @@
"oxid-esales/oxideshop-ce": "7.0 - 7.1", "oxid-esales/oxideshop-ce": "7.0 - 7.1",
"pear/net_smtp": "^1.11", "pear/net_smtp": "^1.11",
"mika56/spfcheck": "^2.1.1", "mika56/spfcheck": "^2.1.1",
"d3/mailauthenticationcheck": "^0.1.0",
"beberlei/assert": "^3.3" "beberlei/assert": "^3.3"
}, },
"require-dev": { "require-dev": {