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/currency_rates_update/schedule/cron_expr';
19 
24 
35  public function __construct(
36  \Magento\Framework\Model\Context $context,
37  \Magento\Framework\Registry $registry,
39  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
40  \Magento\Framework\App\Config\ValueFactory $configValueFactory,
41  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
42  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
43  array $data = []
44  ) {
45  $this->_configValueFactory = $configValueFactory;
46  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
47  }
48 
53  public function afterSave()
54  {
55  $time = $this->getData('groups/import/fields/time/value');
56  $frequency = $this->getData('groups/import/fields/frequency/value');
57 
58  $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY;
59  $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY;
60 
61  $cronExprArray = [
62  intval($time[1]), # Minute
63  intval($time[0]), # Hour
64  $frequency == $frequencyMonthly ? '1' : '*', # Day of the Month
65  '*', # Month of the Year
66  $frequency == $frequencyWeekly ? '1' : '*', # Day of the Week
67  ];
68 
69  $cronExprString = join(' ', $cronExprArray);
70 
71  try {
73  $configValue = $this->_configValueFactory->create();
74  $configValue->load(self::CRON_STRING_PATH, 'path');
75  $configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save();
76  } catch (\Exception $e) {
77  throw new \Exception(__('We can\'t save the Cron expression.'));
78  }
79  return parent::afterSave();
80  }
81 }
__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, array $data=[])
Definition: Cron.php:35
getData($key='', $index=null)
Definition: DataObject.php:119
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12