Cron.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\System\Config\Backend;
  7. class Cron extends \Magento\Framework\App\Config\Value
  8. {
  9. const CRON_STRING_PATH = 'crontab/default/jobs/paypal_fetch_settlement_reports/schedule/cron_expr';
  10. const CRON_MODEL_PATH_INTERVAL = 'paypal/fetch_reports/schedule';
  11. /**
  12. * @var \Magento\Framework\App\Config\ValueFactory
  13. */
  14. protected $_configValueFactory;
  15. /**
  16. * @param \Magento\Framework\Model\Context $context
  17. * @param \Magento\Framework\Registry $registry
  18. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  19. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  20. * @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
  21. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  22. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  23. * @param array $data
  24. */
  25. public function __construct(
  26. \Magento\Framework\Model\Context $context,
  27. \Magento\Framework\Registry $registry,
  28. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  29. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  30. \Magento\Framework\App\Config\ValueFactory $configValueFactory,
  31. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  32. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  33. array $data = []
  34. ) {
  35. $this->_configValueFactory = $configValueFactory;
  36. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  37. }
  38. /**
  39. * Cron settings after save
  40. *
  41. * @return $this
  42. */
  43. public function afterSave()
  44. {
  45. $cronExprString = '';
  46. $time = explode(
  47. ',',
  48. $this->_configValueFactory->create()->load('paypal/fetch_reports/time', 'path')->getValue()
  49. );
  50. if ($this->_configValueFactory->create()->load('paypal/fetch_reports/active', 'path')->getValue()) {
  51. $interval = $this->_configValueFactory->create()->load(self::CRON_MODEL_PATH_INTERVAL, 'path')->getValue();
  52. $cronExprString = "{$time[1]} {$time[0]} */{$interval} * *";
  53. }
  54. $this->_configValueFactory->create()->load(
  55. self::CRON_STRING_PATH,
  56. 'path'
  57. )->setValue(
  58. $cronExprString
  59. )->setPath(
  60. self::CRON_STRING_PATH
  61. )->save();
  62. return parent::afterSave();
  63. }
  64. }