From c5de1eb53c8b05315b9a58e63c81181a31ba7173 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Thu, 8 Oct 2020 16:52:42 +0200 Subject: [PATCH] [Messenger] Add DelayStamp::delayFor() and DelayStamp::delayUntil() --- src/Symfony/Component/Messenger/CHANGELOG.md | 2 +- .../Component/Messenger/Stamp/DelayStamp.php | 14 +++++------ .../Messenger/Tests/Stamp/DelayStampTest.php | 24 +++++++++++-------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Component/Messenger/CHANGELOG.md b/src/Symfony/Component/Messenger/CHANGELOG.md index 9237367f44e23..814e1625dc6a5 100644 --- a/src/Symfony/Component/Messenger/CHANGELOG.md +++ b/src/Symfony/Component/Messenger/CHANGELOG.md @@ -6,7 +6,7 @@ CHANGELOG * The `RedeliveryStamp` will no longer be populated with error data. This information is now stored in the `ErrorDetailsStamp` instead. * Added `FlattenExceptionNormalizer` to give more information about the exception on Messenger background processes. The `FlattenExceptionNormalizer` has a higher priority than `ProblemNormalizer` and it is only used when the Messenger serialization context is set. -* Added factory methods to `DelayStamp`. +* Added factory methods `DelayStamp::delayFor(\DateInterval)` and `DelayStamp::delayUntil(\DateTimeInterface)`. * Removed the exception when dispatching a message with a `DispatchAfterCurrentBusStamp` and not in a context of another dispatch call * Added `WorkerMessageRetriedEvent` * Added `WorkerMessageReceivedEvent::setEnvelope()` and made event mutable diff --git a/src/Symfony/Component/Messenger/Stamp/DelayStamp.php b/src/Symfony/Component/Messenger/Stamp/DelayStamp.php index d5a4839b1e512..fb5cc118fd9a7 100644 --- a/src/Symfony/Component/Messenger/Stamp/DelayStamp.php +++ b/src/Symfony/Component/Messenger/Stamp/DelayStamp.php @@ -31,18 +31,16 @@ public function getDelay(): int return $this->delay; } - public static function delayForSeconds(int $seconds): self + public static function delayFor(\DateInterval $interval): self { - return new self($seconds * 1000); - } + $now = new \DateTimeImmutable(); + $end = $now->add($interval); - public static function delayForMinutes(int $minutes): self - { - return self::delayForSeconds($minutes * 60); + return new self(($end->getTimestamp() - $now->getTimestamp()) * 1000); } - public static function delayForHours(int $hours): self + public static function delayUntil(\DateTimeInterface $dateTime): self { - return self::delayForMinutes($hours * 60); + return new self(($dateTime->getTimestamp() - time()) * 1000); } } diff --git a/src/Symfony/Component/Messenger/Tests/Stamp/DelayStampTest.php b/src/Symfony/Component/Messenger/Tests/Stamp/DelayStampTest.php index f739c9526177d..19ef0825250ca 100644 --- a/src/Symfony/Component/Messenger/Tests/Stamp/DelayStampTest.php +++ b/src/Symfony/Component/Messenger/Tests/Stamp/DelayStampTest.php @@ -19,21 +19,25 @@ */ class DelayStampTest extends TestCase { - public function testSeconds() + public function testDelayFor() { - $stamp = DelayStamp::delayForSeconds(30); + $stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 seconds')); $this->assertSame(30000, $stamp->getDelay()); - } - - public function testMinutes() - { - $stamp = DelayStamp::delayForMinutes(30); + $stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 minutes')); $this->assertSame(1800000, $stamp->getDelay()); + $stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 hours')); + $this->assertSame(108000000, $stamp->getDelay()); + + $stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('-5 seconds')); + $this->assertSame(-5000, $stamp->getDelay()); } - public function testHours() + public function testDelayUntil() { - $stamp = DelayStamp::delayForHours(30); - $this->assertSame(108000000, $stamp->getDelay()); + $stamp = DelayStamp::delayUntil(new \DateTimeImmutable('+30 seconds')); + $this->assertSame(30000, $stamp->getDelay()); + + $stamp = DelayStamp::delayUntil(new \DateTimeImmutable('-5 seconds')); + $this->assertSame(-5000, $stamp->getDelay()); } }