diff --git a/Cron/Clean.php b/Cron/Clean.php index bed4c56..781e5f9 100644 --- a/Cron/Clean.php +++ b/Cron/Clean.php @@ -8,31 +8,32 @@ namespace Mygento\Smtp\Cron; -use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Stdlib\DateTime; -use Mygento\Smtp\Mail\Transport; +use Mygento\Smtp\Model\Config; use Mygento\Smtp\Model\ResourceModel; class Clean { public function __construct( + private Config $config, private ResourceModel\Log $resource, private DateTime $dateTime, private DateTime\DateTime $date, - private ScopeConfigInterface $config ) { } public function execute() { - if (!$this->config->isSetFlag(Transport::XML_PATH_EMAIL_LOG)) { + $clearDays = (int) $this->config->getCleanEmailPeriod(); + + if (!$this->config->isEnabled() || $clearDays <= 0) { return; } $connection = $this->resource->getConnection(); $condition = $connection->quoteInto( 'created_at <= ?', - $this->dateTime->formatDate($this->date->gmtTimestamp() - 24 * 60 * 60) + $this->dateTime->formatDate($this->date->gmtTimestamp() - $clearDays * 24 * 60 * 60) ); $connection->delete($this->resource->getMainTable(), $condition); } diff --git a/Mail/Transport.php b/Mail/Transport.php index 816024c..23b1930 100644 --- a/Mail/Transport.php +++ b/Mail/Transport.php @@ -9,7 +9,6 @@ namespace Mygento\Smtp\Mail; use Closure; -use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\MailException; use Magento\Framework\Mail\Address; @@ -17,17 +16,16 @@ use Magento\Framework\Mail\TransportInterface; use Mygento\Smtp\Api\Data; use Mygento\Smtp\Api\LogRepositoryInterface; +use Mygento\Smtp\Model\Config; use Mygento\Smtp\Model\Source\Status; use Psr\Log\LoggerInterface; class Transport { - public const XML_PATH_EMAIL_LOG = 'system/smtp/log'; - public function __construct( private LogRepositoryInterface $repo, private Data\LogInterfaceFactory $factory, - private ScopeConfigInterface $config, + private Config $config, private LoggerInterface $logger ) { } @@ -40,7 +38,7 @@ public function __construct( */ public function aroundSendMessage(TransportInterface $subject, Closure $proceed): void { - if (!$this->config->isSetFlag(self::XML_PATH_EMAIL_LOG)) { + if (!$this->config->isEnabled()) { $proceed(); return; diff --git a/Model/Config.php b/Model/Config.php new file mode 100644 index 0000000..cac4808 --- /dev/null +++ b/Model/Config.php @@ -0,0 +1,38 @@ +scopeConfig->isSetFlag(self::XML_PATH_EMAIL_LOG); + } + + /** + * @return string|null + */ + public function getCleanEmailPeriod(): ?string + { + return $this->scopeConfig->getValue(self::XML_PATH_CLEAN_EMAIL_PERIOD); + } +} diff --git a/README.md b/README.md index ee78bc7..2e6b9a0 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ The module adds a feature to log email messages to the database even when email ## Configuration Add Yes/No field `Log Email` to `Stores -> Configuration -> Advanced -> System -> Mail Sending Settings` +Add days(int) field `Clean Email Log Every` to `Stores -> Configuration -> Advanced -> System -> Mail Sending Settings` ## Plugins * `aroundSendMessage` diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 13bfa2e..430e02f 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -7,6 +7,14 @@ Magento\Config\Model\Config\Source\Yesno + + + validate-number validate-zero-or-greater validate-digits + Day(s). If empty or zero, the Email log will not be cleaned. + + 1 + +