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