Repository navigation
Expand file tree
/
Copy pathRecurringMessage.php
More file actions
140 lines (119 loc) · 5.03 KB
/
Copy pathRecurringMessage.php
File metadata and controls
140 lines (119 loc) · 5.03 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Scheduler;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Scheduler\Exception\InvalidArgumentException;
use Symfony\Component\Scheduler\Generator\MessageContext;
use Symfony\Component\Scheduler\Trigger\AbstractDecoratedTrigger;
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
use Symfony\Component\Scheduler\Trigger\MessageProviderInterface;
use Symfony\Component\Scheduler\Trigger\PeriodicalTrigger;
use Symfony\Component\Scheduler\Trigger\StaticMessageProvider;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
final class RecurringMessage implements MessageProviderInterface
{
private string $id;
private function __construct(
private readonly TriggerInterface $trigger,
private readonly MessageProviderInterface $provider,
) {
}
/**
* Sets the trigger frequency.
*
* Supported frequency formats:
*
* * An integer to define the frequency as a number of seconds;
* * An ISO 8601 duration format;
* * A relative date format as supported by \DateInterval;
* * A \DateInterval instance.
*
* @param MessageProviderInterface|object $message A message provider that yields messages or a static message that will be dispatched on every trigger
*
* @see https://en.wikipedia.org/wiki/ISO_8601#Durations
* @see https://php.net/datetime.formats#datetime.formats.relative
*/
public static function every(string|int|\DateInterval $frequency, object $message, string|\DateTimeImmutable|null $from = null, string|\DateTimeImmutable $until = new \DateTimeImmutable('3000-01-01')): self
{
return self::trigger(new PeriodicalTrigger($frequency, $from, $until), $message);
}
/**
* @param MessageProviderInterface|object $message A message provider that yields messages or a static message that will be dispatched on every trigger
*/
public static function cron(string $expression, object $message, \DateTimeZone|string|null $timezone = null): self
{
// a hashed part starts a field ("#", "#(0-2)", "#daily"...); "1#2" is the standard "nth weekday" syntax
if (!preg_match('/(?:^|\s)#/', $expression)) {
return self::trigger(CronExpressionTrigger::fromSpec($expression, null, $timezone), $message);
}
$subject = self::unwrap($message);
if (!$subject instanceof \Stringable) {
throw new InvalidArgumentException('A message must be stringable to use "hashed" cron expressions.');
}
return self::trigger(CronExpressionTrigger::fromSpec($expression, (string) $subject, $timezone), $message);
}
/**
* @param MessageProviderInterface|object $message A message provider that yields messages or a static message that will be dispatched on every trigger
*/
public static function trigger(TriggerInterface $trigger, object $message): self
{
if ($message instanceof MessageProviderInterface) {
return new self($trigger, $message);
}
$subject = self::unwrap($message);
$description = $subject::class;
if ($subject instanceof \Stringable) {
try {
$description .= " ($subject)";
} catch (\Exception) {
}
}
return new self($trigger, new StaticMessageProvider([$message], strtr(substr(base64_encode(hash('xxh128', serialize($message), true)), 0, 7), '/+', '._'), $description));
}
public function withJitter(int $maxSeconds = 60): self
{
return new self(new JitterTrigger($this->trigger, $maxSeconds), $this->provider);
}
/**
* Unique identifier for this message's context.
*/
public function getId(): string
{
if (isset($this->id)) {
return $this->id;
}
$trigger = $this->trigger instanceof AbstractDecoratedTrigger ? $this->trigger->inner() : $this->trigger;
return $this->id = hash('crc32c', implode('', [
$this->provider::class,
$this->provider->getId(),
$this->trigger::class,
(string) $this->trigger,
$trigger instanceof CronExpressionTrigger ? $trigger->getTimezone() : '',
$trigger instanceof PeriodicalTrigger ? $trigger->getScheduleBounds() : '',
]));
}
public function getMessages(MessageContext $context): iterable
{
return $this->provider->getMessages($context);
}
public function getProvider(): MessageProviderInterface
{
return $this->provider;
}
public function getTrigger(): TriggerInterface
{
return $this->trigger;
}
private static function unwrap(object $message): object
{
return $message instanceof Envelope ? $message->getMessage() : $message;
}
}