Files
ltsecurityinc/app/code/Caratlane/NetSuite/Model/Config/CronConfig.php
T

76 lines
2.6 KiB
PHP
Executable File

<?php
/**
* @author 18th DigiTech Team
* @copyright Copyright (c) 2020 18th DigiTech (https://www.18thdigitech.com)
* @package Caratlane_NetSuite
*/
namespace Caratlane\NetSuite\Model\Config;
class CronConfig extends \Magento\Framework\App\Config\Value
{
const CRON_STRING_PATH = 'crontab/default/jobs/caratlane_netsuite_cron_job/schedule/cron_expr';
const CRON_MODEL_PATH = 'crontab/default/jobs/caratlane_netsuite_cron_job/run/model';
protected $_configValueFactory;
protected $_runModelPath = '';
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Config\ValueFactory $configValueFactory,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
$runModelPath = '',
array $data = []
) {
$this->_runModelPath = $runModelPath;
$this->_configValueFactory = $configValueFactory;
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}
public function afterSave()
{
$time = $this->getData('netsuite/cronScheduled/fields/time/value');
$frequency = $this->getData('netsuite/cronScheduled/fields/frequency/value');
$t1 = isset($time[1])?$time[1]:0;
$t2 = isset($time[0])?$time[0]:0;
$cronExprArray = [
(int)($t1),
(int)($t2),
$frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY ? '1' : '*',
'*',
$frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY ? '1' : '*',
];
$cronExprString = join(' ', $cronExprArray);
try {
$this->_configValueFactory->create()->load(
self::CRON_STRING_PATH,
'path'
)->setValue(
$cronExprString
)->setPath(
self::CRON_STRING_PATH
)->save();
$this->_configValueFactory->create()->load(
self::CRON_MODEL_PATH,
'path'
)->setValue(
$this->_runModelPath
)->setPath(
self::CRON_MODEL_PATH
)->save();
} catch (\Exception $e) {
//throw new \Exception(__('We can\'t save the cron expression.'));
$e->getMessage();
}
return parent::afterSave();
}
}