DelayStamp.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Messenger\Stamp;
  11. /**
  12. * Apply this stamp to delay delivery of your message on a transport.
  13. */
  14. final class DelayStamp implements StampInterface
  15. {
  16. private $delay;
  17. /**
  18. * @param int $delay The delay in milliseconds
  19. */
  20. public function __construct(int $delay)
  21. {
  22. $this->delay = $delay;
  23. }
  24. public function getDelay(): int
  25. {
  26. return $this->delay;
  27. }
  28. public static function delayFor(\DateInterval $interval): self
  29. {
  30. $now = new \DateTimeImmutable();
  31. $end = $now->add($interval);
  32. return new self(($end->getTimestamp() - $now->getTimestamp()) * 1000);
  33. }
  34. public static function delayUntil(\DateTimeInterface $dateTime): self
  35. {
  36. return new self(($dateTime->getTimestamp() - time()) * 1000);
  37. }
  38. }