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
7 
13 class Cron extends \Magento\Framework\App\Config\Value
14 {
15  const CRON_STRING_PATH = 'crontab/default/jobs/system_backup/schedule/cron_expr';
16 
17  const CRON_MODEL_PATH = 'crontab/default/jobs/system_backup/run/model';
18 
19  const XML_PATH_BACKUP_ENABLED = 'groups/backup/fields/enabled/value';
20 
21  const XML_PATH_BACKUP_TIME = 'groups/backup/fields/time/value';
22 
23  const XML_PATH_BACKUP_FREQUENCY = 'groups/backup/fields/frequency/value';
24 
29 
33  protected $_runModelPath = '';
34 
46  public function __construct(
47  \Magento\Framework\Model\Context $context,
48  \Magento\Framework\Registry $registry,
49  \Magento\Framework\App\Config\ScopeConfigInterface $config,
50  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
51  \Magento\Framework\App\Config\ValueFactory $configValueFactory,
52  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
53  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
54  $runModelPath = '',
55  array $data = []
56  ) {
57  $this->_runModelPath = $runModelPath;
58  $this->_configValueFactory = $configValueFactory;
59  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
60  }
61 
68  public function afterSave()
69  {
70  $enabled = $this->getData(self::XML_PATH_BACKUP_ENABLED);
71  $time = $this->getData(self::XML_PATH_BACKUP_TIME);
72  $frequency = $this->getData(self::XML_PATH_BACKUP_FREQUENCY);
73 
74  $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY;
75  $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY;
76 
77  if ($enabled) {
78  $cronExprArray = [
79  (int) $time[1], # Minute
80  (int) $time[0], # Hour
81  $frequency == $frequencyMonthly ? '1' : '*', # Day of the Month
82  '*', # Month of the Year
83  $frequency == $frequencyWeekly ? '1' : '*', # Day of the Week
84  ];
85  $cronExprString = join(' ', $cronExprArray);
86  } else {
87  $cronExprString = '';
88  }
89 
90  try {
91  $this->_configValueFactory->create()->load(
92  self::CRON_STRING_PATH,
93  'path'
94  )->setValue(
95  $cronExprString
96  )->setPath(
97  self::CRON_STRING_PATH
98  )->save();
99 
100  $this->_configValueFactory->create()->load(
101  self::CRON_MODEL_PATH,
102  'path'
103  )->setValue(
104  $this->_runModelPath
105  )->setPath(
106  self::CRON_MODEL_PATH
107  )->save();
108  } catch (\Exception $e) {
109  throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the Cron expression.'));
110  }
111  return parent::afterSave();
112  }
113 }
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:46