Cron.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Backend Model for Currency import options
  8. */
  9. namespace Magento\Config\Model\Config\Backend\Currency;
  10. /**
  11. * Cron job configuration for currency
  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/currency_rates_update/schedule/cron_expr';
  19. /**
  20. * @var \Magento\Framework\App\Config\ValueFactory
  21. */
  22. protected $_configValueFactory;
  23. /**
  24. * @param \Magento\Framework\Model\Context $context
  25. * @param \Magento\Framework\Registry $registry
  26. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  27. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  28. * @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
  29. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  30. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  31. * @param array $data
  32. */
  33. public function __construct(
  34. \Magento\Framework\Model\Context $context,
  35. \Magento\Framework\Registry $registry,
  36. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  37. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  38. \Magento\Framework\App\Config\ValueFactory $configValueFactory,
  39. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  40. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  41. array $data = []
  42. ) {
  43. $this->_configValueFactory = $configValueFactory;
  44. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  45. }
  46. /**
  47. * After save handler
  48. *
  49. * @return $this
  50. * @throws \Exception
  51. */
  52. public function afterSave()
  53. {
  54. $time = $this->getData('groups/import/fields/time/value');
  55. $frequency = $this->getData('groups/import/fields/frequency/value');
  56. $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY;
  57. $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY;
  58. $cronExprArray = [
  59. (int)$time[1], # Minute
  60. (int)$time[0], # Hour
  61. $frequency == $frequencyMonthly ? '1' : '*', # Day of the Month
  62. '*', # Month of the Year
  63. $frequency == $frequencyWeekly ? '1' : '*', # Day of the Week
  64. ];
  65. $cronExprString = join(' ', $cronExprArray);
  66. try {
  67. /** @var $configValue \Magento\Framework\App\Config\ValueInterface */
  68. $configValue = $this->_configValueFactory->create();
  69. $configValue->load(self::CRON_STRING_PATH, 'path');
  70. $configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save();
  71. } catch (\Exception $e) {
  72. throw new \Exception(__('We can\'t save the Cron expression.'));
  73. }
  74. return parent::afterSave();
  75. }
  76. }