-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add methods DelayStamp::delayFor()
and DelayStamp::delayUntil()
.
#38480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,21 @@ | |
|
||
namespace Symfony\Component\Messenger\Stamp; | ||
|
||
use Symfony\Component\Messenger\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Apply this stamp to delay delivery of your message on a transport. | ||
*/ | ||
final class DelayStamp implements StampInterface | ||
{ | ||
public const PERIOD_SECONDS = 'seconds'; | ||
public const PERIOD_MINUTES = 'minutes'; | ||
public const PERIOD_HOURS = 'hours'; | ||
public const PERIOD_DAYS = 'days'; | ||
public const PERIOD_WEEKS = 'weeks'; | ||
public const PERIOD_MONTHS = 'months'; | ||
public const PERIOD_YEARS = 'years'; | ||
|
||
private $delay; | ||
|
||
/** | ||
|
@@ -33,16 +43,51 @@ public function getDelay(): int | |
|
||
public static function delayForSeconds(int $seconds): self | ||
{ | ||
return new self($seconds * 1000); | ||
return self::delayFor($seconds, self::PERIOD_SECONDS); | ||
} | ||
|
||
public static function delayForMinutes(int $minutes): self | ||
{ | ||
return self::delayForSeconds($minutes * 60); | ||
return self::delayFor($minutes, self::PERIOD_MINUTES); | ||
} | ||
|
||
public static function delayForHours(int $hours): self | ||
{ | ||
return self::delayForMinutes($hours * 60); | ||
return self::delayFor($hours, self::PERIOD_HOURS); | ||
} | ||
|
||
public static function delayUntil(\DateTimeInterface $executeAfter): self | ||
{ | ||
$now = (new \DateTimeImmutable())->setTimezone($executeAfter->getTimezone()); | ||
|
||
if ($now >= $executeAfter) { | ||
throw new InvalidArgumentException(sprintf('You cannot pass a date that is equal to now or is in the past. Now is "%s" and the passed date is "%s".', $now->format('Y-m-d, H:i:s'), $executeAfter->format('Y-m-d, H:i:s'))); | ||
} | ||
|
||
$diff = ($executeAfter->format('U') - $now->format('U')) * 1000; | ||
|
||
return new self($diff); | ||
} | ||
|
||
/** | ||
* @param string $period A string representing a unit symbol valid for relative formats of DateTime objects | ||
* | ||
* @see https://www.php.net/manual/en/datetime.formats.relative.php#datetime.formats.relative | ||
*/ | ||
public static function delayFor(int $units, string $period) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not understanding: why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We dont need to reinvent a way to define time intervals. We got the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, should have I to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is simply a shortcut, like the ones already exist in the class. |
||
{ | ||
if (0 >= $units) { | ||
throw new InvalidArgumentException(sprintf('The value of units has to be positive. You passed "%s".', $units)); | ||
} | ||
|
||
$allowedPeriods = [self::PERIOD_SECONDS, self::PERIOD_MINUTES, self::PERIOD_HOURS, self::PERIOD_DAYS, self::PERIOD_WEEKS, self::PERIOD_MONTHS, self::PERIOD_YEARS]; | ||
if (false === \in_array($period, $allowedPeriods)) { | ||
throw new InvalidArgumentException(sprintf('The passed period "%s" is not allowed. Allowed periods are: "%s".', $period, implode(', ', $allowedPeriods))); | ||
} | ||
|
||
$rescheduleIn = sprintf('+%s %s', $units, $period); | ||
$executeAfter = (new \DateTime())->modify($rescheduleIn); | ||
|
||
return self::delayUntil($executeAfter); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this related to your comment about
delayFor()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we dont need a way to redefine time.