CollectionTime.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model\Config\Backend;
  7. use Magento\Framework\App\Cache\TypeListInterface;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\Config\Storage\WriterInterface;
  10. use Magento\Framework\App\Config\Value;
  11. use Magento\Framework\Data\Collection\AbstractDb;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\Model\Context;
  14. use Magento\Framework\Model\ResourceModel\AbstractResource;
  15. use Magento\Framework\Registry;
  16. /**
  17. * Config value backend model.
  18. */
  19. class CollectionTime extends Value
  20. {
  21. /**
  22. * The path to config setting of schedule of collection data cron.
  23. */
  24. const CRON_SCHEDULE_PATH = 'crontab/default/jobs/analytics_collect_data/schedule/cron_expr';
  25. /**
  26. * @var WriterInterface
  27. */
  28. private $configWriter;
  29. /**
  30. * @param Context $context
  31. * @param Registry $registry
  32. * @param ScopeConfigInterface $config
  33. * @param TypeListInterface $cacheTypeList
  34. * @param WriterInterface $configWriter
  35. * @param AbstractResource|null $resource
  36. * @param AbstractDb|null $resourceCollection
  37. * @param array $data
  38. */
  39. public function __construct(
  40. Context $context,
  41. Registry $registry,
  42. ScopeConfigInterface $config,
  43. TypeListInterface $cacheTypeList,
  44. WriterInterface $configWriter,
  45. AbstractResource $resource = null,
  46. AbstractDb $resourceCollection = null,
  47. array $data = []
  48. ) {
  49. $this->configWriter = $configWriter;
  50. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. *
  55. * {@inheritdoc}. Set schedule setting for cron.
  56. *
  57. * @return Value
  58. */
  59. public function afterSave()
  60. {
  61. $result = preg_match('#(?<hour>\d{2}),(?<min>\d{2}),(?<sec>\d{2})#', $this->getValue(), $time);
  62. if (!$result) {
  63. throw new LocalizedException(
  64. __('The time value is using an unsupported format. Enter a supported format and try again.')
  65. );
  66. }
  67. $cronExprArray = [
  68. $time['min'], # Minute
  69. $time['hour'], # Hour
  70. '*', # Day of the Month
  71. '*', # Month of the Year
  72. '*', # Day of the Week
  73. ];
  74. $cronExprString = join(' ', $cronExprArray);
  75. try {
  76. $this->configWriter->save(self::CRON_SCHEDULE_PATH, $cronExprString);
  77. } catch (\Exception $e) {
  78. $this->_logger->error($e->getMessage());
  79. throw new LocalizedException(__('Cron settings can\'t be saved'));
  80. }
  81. return parent::afterSave();
  82. }
  83. }