RedeliveryStamp.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  12. use Symfony\Component\Messenger\Envelope;
  13. /**
  14. * Stamp applied when a messages needs to be redelivered.
  15. */
  16. final class RedeliveryStamp implements StampInterface
  17. {
  18. private $retryCount;
  19. private $redeliveredAt;
  20. private $exceptionMessage;
  21. private $flattenException;
  22. /**
  23. * @param \DateTimeInterface|null $exceptionMessage
  24. */
  25. public function __construct(int $retryCount, $exceptionMessage = null, FlattenException $flattenException = null, \DateTimeInterface $redeliveredAt = null)
  26. {
  27. $this->retryCount = $retryCount;
  28. $this->redeliveredAt = $redeliveredAt ?? new \DateTimeImmutable();
  29. if (null !== $redeliveredAt) {
  30. trigger_deprecation('symfony/messenger', '5.2', sprintf('Using the "$redeliveredAt" as 4th argument of the "%s::__construct()" is deprecated, pass "$redeliveredAt" as second argument instead.', self::class));
  31. }
  32. if ($exceptionMessage instanceof \DateTimeInterface) {
  33. // In Symfony 6.0, the second argument will be $redeliveredAt
  34. $this->redeliveredAt = $exceptionMessage;
  35. if (null !== $redeliveredAt) {
  36. throw new \LogicException('It is deprecated to specify a redeliveredAt as 4th argument. The correct way is to specify redeliveredAt as the second argument. Using both is not allowed.');
  37. }
  38. } elseif (null !== $exceptionMessage) {
  39. trigger_deprecation('symfony/messenger', '5.2', sprintf('Using the "$exceptionMessage" parameter in the "%s" class is deprecated, use the "%s" class instead.', self::class, ErrorDetailsStamp::class));
  40. $this->exceptionMessage = $exceptionMessage;
  41. }
  42. if (null !== $flattenException) {
  43. trigger_deprecation('symfony/messenger', '5.2', sprintf('Using the "$flattenException" parameter in the "%s" class is deprecated, use the "%s" class instead.', self::class, ErrorDetailsStamp::class));
  44. }
  45. $this->flattenException = $flattenException;
  46. }
  47. public static function getRetryCountFromEnvelope(Envelope $envelope): int
  48. {
  49. /** @var self|null $retryMessageStamp */
  50. $retryMessageStamp = $envelope->last(self::class);
  51. return $retryMessageStamp ? $retryMessageStamp->getRetryCount() : 0;
  52. }
  53. public function getRetryCount(): int
  54. {
  55. return $this->retryCount;
  56. }
  57. /**
  58. * @deprecated since Symfony 5.2, use ErrorDetailsStamp instead.
  59. */
  60. public function getExceptionMessage(): ?string
  61. {
  62. trigger_deprecation('symfony/messenger', '5.2', sprintf('Using the "getExceptionMessage()" method of the "%s" class is deprecated, use the "%s" class instead.', self::class, ErrorDetailsStamp::class));
  63. return $this->exceptionMessage;
  64. }
  65. /**
  66. * @deprecated since Symfony 5.2, use ErrorDetailsStamp instead.
  67. */
  68. public function getFlattenException(): ?FlattenException
  69. {
  70. trigger_deprecation('symfony/messenger', '5.2', sprintf('Using the "getFlattenException()" method of the "%s" class is deprecated, use the "%s" class instead.', self::class, ErrorDetailsStamp::class));
  71. return $this->flattenException;
  72. }
  73. public function getRedeliveredAt(): \DateTimeInterface
  74. {
  75. return $this->redeliveredAt;
  76. }
  77. }