-
Notifications
You must be signed in to change notification settings - Fork 91
/
CronConfig.php
120 lines (103 loc) · 4 KB
/
CronConfig.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace Shopsys\FrameworkBundle\Component\Cron\Config;
use DateTimeInterface;
use Shopsys\FrameworkBundle\Component\Cron\Config\Exception\CronModuleConfigNotFoundException;
use Shopsys\FrameworkBundle\Component\Cron\CronTimeResolver;
use Shopsys\FrameworkBundle\Component\Cron\Exception\InvalidCronModuleException;
use Shopsys\Plugin\Cron\IteratedCronModuleInterface;
use Shopsys\Plugin\Cron\SimpleCronModuleInterface;
class CronConfig
{
/**
* @var \Shopsys\FrameworkBundle\Component\Cron\Config\CronModuleConfig[]
*/
protected array $cronModuleConfigs;
/**
* @param \Shopsys\FrameworkBundle\Component\Cron\CronTimeResolver $cronTimeResolver
*/
public function __construct(
protected readonly CronTimeResolver $cronTimeResolver,
) {
$this->cronModuleConfigs = [];
}
/**
* @param \Shopsys\Plugin\Cron\SimpleCronModuleInterface|\Shopsys\Plugin\Cron\IteratedCronModuleInterface|mixed $service
* @param string $serviceId
* @param string $timeHours
* @param string $timeMinutes
* @param string $instanceName
* @param string|null $readableName
* @param string|null $readableFrequency
* @param int $runEveryMin
* @param int $timeoutIteratedCronSec
*/
public function registerCronModuleInstance(
$service,
string $serviceId,
string $timeHours,
string $timeMinutes,
string $instanceName,
?string $readableName = null,
?string $readableFrequency = null,
int $runEveryMin = CronModuleConfig::RUN_EVERY_MIN_DEFAULT,
int $timeoutIteratedCronSec = CronModuleConfig::TIMEOUT_ITERATED_CRON_SEC_DEFAULT,
): void {
if (!$service instanceof SimpleCronModuleInterface && !$service instanceof IteratedCronModuleInterface) {
throw new InvalidCronModuleException($serviceId);
}
$this->cronTimeResolver->validateTimeString($timeHours, 23, 1);
$this->cronTimeResolver->validateTimeString($timeMinutes, 55, 1);
$cronModuleConfig = new CronModuleConfig($service, $serviceId, $timeHours, $timeMinutes, $readableName, $readableFrequency, $runEveryMin, $timeoutIteratedCronSec);
$cronModuleConfig->assignToInstance($instanceName);
$this->cronModuleConfigs[] = $cronModuleConfig;
}
/**
* @return \Shopsys\FrameworkBundle\Component\Cron\Config\CronModuleConfig[]
*/
public function getAllCronModuleConfigs(): array
{
return $this->cronModuleConfigs;
}
/**
* @param \DateTimeInterface $roundedTime
* @return \Shopsys\FrameworkBundle\Component\Cron\Config\CronModuleConfig[]
*/
public function getCronModuleConfigsByTime(DateTimeInterface $roundedTime): array
{
$matchedCronConfigs = [];
foreach ($this->cronModuleConfigs as $cronConfig) {
if ($this->cronTimeResolver->isValidAtTime($cronConfig, $roundedTime)) {
$matchedCronConfigs[] = $cronConfig;
}
}
return $matchedCronConfigs;
}
/**
* @param string $serviceId
* @return \Shopsys\FrameworkBundle\Component\Cron\Config\CronModuleConfig
*/
public function getCronModuleConfigByServiceId(string $serviceId): CronModuleConfig
{
foreach ($this->cronModuleConfigs as $cronConfig) {
if ($cronConfig->getServiceId() === $serviceId) {
return $cronConfig;
}
}
throw new CronModuleConfigNotFoundException($serviceId);
}
/**
* @param string $instanceName
* @return \Shopsys\FrameworkBundle\Component\Cron\Config\CronModuleConfig[]
*/
public function getCronModuleConfigsForInstance(string $instanceName): array
{
$matchedCronConfigs = [];
foreach ($this->cronModuleConfigs as $cronModuleConfig) {
if ($cronModuleConfig->getInstanceName() === $instanceName) {
$matchedCronConfigs[] = $cronModuleConfig;
}
}
return $matchedCronConfigs;
}
}