Cron.php 4.0 KB

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