Files
module-integration/SyncLog/Cron/DeleteSyncLog.php
Yair García TorresandYAIR GARCIA d44a8a8846 Cron with delete capability 7 days previous entries (#32)
* Abandoned carts fixed not updating in AC when cart is edited

* Cron with delete capability 7 days previous entries

Co-authored-by: YAIR GARCIA <yagator@MacBook-Air-de-YAIR.local>
2022-06-29 10:57:47 -05:00

71 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace ActiveCampaign\SyncLog\Cron;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
class DeleteSyncLog
{
const SYNCLOG_TABLE = "sync_log";
const XML_PATH_ACTIVE_CAMPAIGN_SYNCLOG_ENABLE = "active_campaign/synclog/synclog_delete";
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/** @var ResourceConnection */
private $connection;
/** @var DateTime */
private $dateTime;
public function __construct(
ScopeConfigInterface $scopeConfig,
ResourceConnection $connection,
DateTime $dateTime
)
{
$this->scopeConfig = $scopeConfig;
$this->connection = $connection;
$this->dateTime = $dateTime;
}
/**
* Execute the cron
*
* @return void
*/
public function execute()
{
if($this->isDeletingEnabled()) {
$connection = $this->connection->getConnection();
$tableName = $connection->getTableName(self::SYNCLOG_TABLE);
$currentDate = $this->dateTime->gmtDate("Y-m-d", strtotime('-7 days'));
$whereConditions = [
$connection->quoteInto("creation_date < ?", $currentDate)
];
$connection->delete($tableName, $whereConditions);
}
}
/**
* @param null $scopeCode
* @return bool
*/
public function isDeletingEnabled($scopeCode = null): bool
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_ACTIVE_CAMPAIGN_SYNCLOG_ENABLE,
ScopeInterface::SCOPE_STORES,
$scopeCode
);
}
}