Cron.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Log Cron Backend Model
  8. */
  9. namespace Magento\Config\Model\Config\Backend\Log;
  10. /**
  11. * Cron logger configuration
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Cron extends \Magento\Framework\App\Config\Value
  17. {
  18. const CRON_STRING_PATH = 'crontab/default/jobs/log_clean/schedule/cron_expr';
  19. const CRON_MODEL_PATH = 'crontab/default/jobs/log_clean/run/model';
  20. /**
  21. * @var \Magento\Framework\App\Config\ValueFactory
  22. */
  23. protected $_configValueFactory;
  24. /**
  25. * @var string
  26. */
  27. protected $_runModelPath = '';
  28. /**
  29. * @param \Magento\Framework\Model\Context $context
  30. * @param \Magento\Framework\Registry $registry
  31. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  32. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  33. * @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
  34. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  35. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  36. * @param string $runModelPath
  37. * @param array $data
  38. */
  39. public function __construct(
  40. \Magento\Framework\Model\Context $context,
  41. \Magento\Framework\Registry $registry,
  42. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  43. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  44. \Magento\Framework\App\Config\ValueFactory $configValueFactory,
  45. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  46. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  47. $runModelPath = '',
  48. array $data = []
  49. ) {
  50. $this->_configValueFactory = $configValueFactory;
  51. $this->_runModelPath = $runModelPath;
  52. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  53. }
  54. /**
  55. * Cron settings after save
  56. *
  57. * @return $this
  58. * @throws \Magento\Framework\Exception\LocalizedException
  59. */
  60. public function afterSave()
  61. {
  62. $enabled = $this->getData('groups/log/fields/enabled/value');
  63. $time = $this->getData('groups/log/fields/time/value');
  64. $frequency = $this->getData('groups/log/fields/frequency/value');
  65. $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY;
  66. $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY;
  67. if ($enabled) {
  68. $cronExprArray = [
  69. (int)$time[1], # Minute
  70. (int)$time[0], # Hour
  71. $frequency == $frequencyMonthly ? '1' : '*', # Day of the Month
  72. '*', # Month of the Year
  73. $frequency == $frequencyWeekly ? '1' : '*', # Day of the Week
  74. ];
  75. $cronExprString = join(' ', $cronExprArray);
  76. } else {
  77. $cronExprString = '';
  78. }
  79. try {
  80. /** @var $configValue \Magento\Framework\App\Config\ValueInterface */
  81. $configValue = $this->_configValueFactory->create();
  82. $configValue->load(self::CRON_STRING_PATH, 'path');
  83. $configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save();
  84. /** @var $configValue \Magento\Framework\App\Config\ValueInterface */
  85. $configValue = $this->_configValueFactory->create();
  86. $configValue->load(self::CRON_MODEL_PATH, 'path');
  87. $configValue->setValue($this->_runModelPath)->setPath(self::CRON_MODEL_PATH)->save();
  88. } catch (\Exception $e) {
  89. throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the Cron expression.'));
  90. }
  91. return parent::afterSave();
  92. }
  93. }