Skip to content

Commit

Permalink
Add settings to be able setup timeout for clean logs (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Maksim Artemenko <[email protected]>
  • Loading branch information
MarsArt and Maksim Artemenko committed Apr 12, 2024
1 parent c3629f7 commit 22b9b07
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
11 changes: 6 additions & 5 deletions Cron/Clean.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 3 additions & 5 deletions Mail/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@
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;
use Magento\Framework\Mail\EmailMessageInterface;
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
) {
}
Expand All @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* @author Mygento Team
* @copyright 2023 Mygento (https://www.mygento.com)
* @package Mygento_Smtp
*/

namespace Mygento\Smtp\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

class Config
{
private const XML_PATH_EMAIL_LOG = 'system/smtp/log';
private const XML_PATH_CLEAN_EMAIL_PERIOD = 'system/smtp/clean_email_period';

public function __construct(
private ScopeConfigInterface $scopeConfig
) {
}

/**
* @return bool
*/
public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_EMAIL_LOG);
}

/**
* @return string|null
*/
public function getCleanEmailPeriod(): ?string
{
return $this->scopeConfig->getValue(self::XML_PATH_CLEAN_EMAIL_PERIOD);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<label>Log Email</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="clean_email_period" translate="label comment" type="text" sortOrder="160" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Clean Email Log Every</label>
<validate>validate-number validate-zero-or-greater validate-digits</validate>
<comment>Day(s). If empty or zero, the Email log will not be cleaned.</comment>
<depends>
<field id="log">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down

0 comments on commit 22b9b07

Please sign in to comment.