Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectionTime.php
Go to the documentation of this file.
1 <?php
7 
17 
21 class CollectionTime extends Value
22 {
26  const CRON_SCHEDULE_PATH = 'crontab/default/jobs/analytics_collect_data/schedule/cron_expr';
27 
31  private $configWriter;
32 
43  public function __construct(
44  Context $context,
48  WriterInterface $configWriter,
50  AbstractDb $resourceCollection = null,
51  array $data = []
52  ) {
53  $this->configWriter = $configWriter;
54  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
55  }
56 
64  public function afterSave()
65  {
66  $result = preg_match('#(?<hour>\d{2}),(?<min>\d{2}),(?<sec>\d{2})#', $this->getValue(), $time);
67 
68  if (!$result) {
69  throw new LocalizedException(
70  __('The time value is using an unsupported format. Enter a supported format and try again.')
71  );
72  }
73 
74  $cronExprArray = [
75  $time['min'], # Minute
76  $time['hour'], # Hour
77  '*', # Day of the Month
78  '*', # Month of the Year
79  '*', # Day of the Week
80  ];
81 
82  $cronExprString = join(' ', $cronExprArray);
83 
84  try {
85  $this->configWriter->save(self::CRON_SCHEDULE_PATH, $cronExprString);
86  } catch (\Exception $e) {
87  $this->_logger->error($e->getMessage());
88  throw new LocalizedException(__('Cron settings can\'t be saved'));
89  }
90 
91  return parent::afterSave();
92  }
93 }
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(Context $context, Registry $registry, ScopeConfigInterface $config, TypeListInterface $cacheTypeList, WriterInterface $configWriter, AbstractResource $resource=null, AbstractDb $resourceCollection=null, array $data=[])