Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee23cba

Browse files
committed
[Scheduler] add RecurringMessage::getId() and prevent duplicates
1 parent f2ff84f commit ee23cba

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

src/Symfony/Component/Scheduler/RecurringMessage.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
final class RecurringMessage
2323
{
24+
private string $id;
25+
2426
private function __construct(
2527
private readonly TriggerInterface $trigger,
2628
private readonly object $message,
@@ -51,6 +53,23 @@ public static function trigger(TriggerInterface $trigger, object $message): self
5153
return new self($trigger, $message);
5254
}
5355

56+
/**
57+
* Unique identifier for this message's context.
58+
*/
59+
public function getId(): string
60+
{
61+
if (isset($this->id)) {
62+
return $this->id;
63+
}
64+
65+
return $this->id = hash('crc32c', implode('', [
66+
$this->message::class,
67+
$this->message instanceof \Stringable ? (string) $this->message : '',
68+
$this->trigger::class,
69+
$this->trigger instanceof \Stringable ? (string) $this->trigger : '',
70+
]));
71+
}
72+
5473
public function getMessage(): object
5574
{
5675
return $this->message;

src/Symfony/Component/Scheduler/Schedule.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
namespace Symfony\Component\Scheduler;
1313

1414
use Symfony\Component\Lock\LockInterface;
15+
use Symfony\Component\Scheduler\Exception\LogicException;
1516
use Symfony\Contracts\Cache\CacheInterface;
1617

1718
/**
1819
* @experimental
1920
*/
2021
final class Schedule implements ScheduleProviderInterface
2122
{
22-
/** @var array<RecurringMessage> */
23+
/** @var array<string,RecurringMessage> */
2324
private array $messages = [];
2425
private ?LockInterface $lock = null;
2526
private ?CacheInterface $state = null;
@@ -29,8 +30,13 @@ final class Schedule implements ScheduleProviderInterface
2930
*/
3031
public function add(RecurringMessage $message, RecurringMessage ...$messages): static
3132
{
32-
$this->messages[] = $message;
33-
$this->messages = array_merge($this->messages, $messages);
33+
foreach ([$message, ...$messages] as $m) {
34+
if (isset($this->messages[$m->getId()])) {
35+
throw new LogicException('Duplicated schedule message.');
36+
}
37+
38+
$this->messages[$m->getId()] = $m;
39+
}
3440

3541
return $this;
3642
}
@@ -66,7 +72,7 @@ public function getState(): ?CacheInterface
6672
}
6773

6874
/**
69-
* @return array<RecurringMessage>
75+
* @return array<string,RecurringMessage>
7076
*/
7177
public function getRecurringMessages(): array
7278
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Scheduler\RecurringMessage;
16+
17+
final class RecurringMessageTest extends TestCase
18+
{
19+
public function testUniqueId()
20+
{
21+
$message1 = RecurringMessage::cron('* * * * *', new \stdClass());
22+
$message2 = RecurringMessage::cron('* 5 * * *', new \stdClass());
23+
24+
$this->assertSame($message1->getId(), (clone $message1)->getId());
25+
$this->assertNotSame($message1->getId(), $message2->getId());
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Scheduler\Exception\LogicException;
16+
use Symfony\Component\Scheduler\RecurringMessage;
17+
use Symfony\Component\Scheduler\Schedule;
18+
19+
class ScheduleTest extends TestCase
20+
{
21+
public function testCannotAddDuplicateMessage()
22+
{
23+
$schedule = new Schedule();
24+
$schedule->add(RecurringMessage::cron('* * * * *', new \stdClass()));
25+
26+
$this->expectException(LogicException::class);
27+
28+
$schedule->add(RecurringMessage::cron('* * * * *', new \stdClass()));
29+
}
30+
}

0 commit comments

Comments
 (0)