Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cron.php
Go to the documentation of this file.
1 <?php
11 
16 class Cron extends \Magento\Framework\App\Config\Value
17 {
18  const CRON_STRING_PATH = 'crontab/default/jobs/log_clean/schedule/cron_expr';
19 
20  const CRON_MODEL_PATH = 'crontab/default/jobs/log_clean/run/model';
21 
26 
30  protected $_runModelPath = '';
31 
43  public function __construct(
44  \Magento\Framework\Model\Context $context,
45  \Magento\Framework\Registry $registry,
47  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
48  \Magento\Framework\App\Config\ValueFactory $configValueFactory,
49  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
50  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
51  $runModelPath = '',
52  array $data = []
53  ) {
54  $this->_configValueFactory = $configValueFactory;
55  $this->_runModelPath = $runModelPath;
56  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
57  }
58 
65  public function afterSave()
66  {
67  $enabled = $this->getData('groups/log/fields/enabled/value');
68  $time = $this->getData('groups/log/fields/time/value');
69  $frequency = $this->getData('groups/log/fields/frequency/value');
70 
71  $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY;
72  $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY;
73 
74  if ($enabled) {
75  $cronExprArray = [
76  intval($time[1]), # Minute
77  intval($time[0]), # Hour
78  $frequency == $frequencyMonthly ? '1' : '*', # Day of the Month
79  '*', # Month of the Year
80  $frequency == $frequencyWeekly ? '1' : '*', # Day of the Week
81  ];
82  $cronExprString = join(' ', $cronExprArray);
83  } else {
84  $cronExprString = '';
85  }
86 
87  try {
89  $configValue = $this->_configValueFactory->create();
90  $configValue->load(self::CRON_STRING_PATH, 'path');
91  $configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save();
92 
94  $configValue = $this->_configValueFactory->create();
95  $configValue->load(self::CRON_MODEL_PATH, 'path');
96  $configValue->setValue($this->_runModelPath)->setPath(self::CRON_MODEL_PATH)->save();
97  } catch (\Exception $e) {
98  throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the Cron expression.'));
99  }
100  return parent::afterSave();
101  }
102 }
getData($key='', $index=null)
Definition: DataObject.php:119
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__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=[])
Definition: Cron.php:43