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