ErrorDetailsStamp.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Exception\HandlerFailedException;
  13. use Throwable;
  14. /**
  15. * Stamp applied when a messages fails due to an exception in the handler.
  16. */
  17. final class ErrorDetailsStamp implements StampInterface
  18. {
  19. /** @var string */
  20. private $exceptionClass;
  21. /** @var int|mixed */
  22. private $exceptionCode;
  23. /** @var string */
  24. private $exceptionMessage;
  25. /** @var FlattenException|null */
  26. private $flattenException;
  27. /**
  28. * @param int|mixed $exceptionCode
  29. */
  30. public function __construct(string $exceptionClass, $exceptionCode, string $exceptionMessage, FlattenException $flattenException = null)
  31. {
  32. $this->exceptionClass = $exceptionClass;
  33. $this->exceptionCode = $exceptionCode;
  34. $this->exceptionMessage = $exceptionMessage;
  35. $this->flattenException = $flattenException;
  36. }
  37. public static function create(Throwable $throwable): self
  38. {
  39. if ($throwable instanceof HandlerFailedException) {
  40. $throwable = $throwable->getPrevious();
  41. }
  42. $flattenException = null;
  43. if (class_exists(FlattenException::class)) {
  44. $flattenException = FlattenException::createFromThrowable($throwable);
  45. }
  46. return new self(\get_class($throwable), $throwable->getCode(), $throwable->getMessage(), $flattenException);
  47. }
  48. public function getExceptionClass(): string
  49. {
  50. return $this->exceptionClass;
  51. }
  52. public function getExceptionCode()
  53. {
  54. return $this->exceptionCode;
  55. }
  56. public function getExceptionMessage(): string
  57. {
  58. return $this->exceptionMessage;
  59. }
  60. public function getFlattenException(): ?FlattenException
  61. {
  62. return $this->flattenException;
  63. }
  64. public function equals(?self $that): bool
  65. {
  66. if (null === $that) {
  67. return false;
  68. }
  69. if ($this->flattenException && $that->flattenException) {
  70. return $this->flattenException->getClass() === $that->flattenException->getClass()
  71. && $this->flattenException->getCode() === $that->flattenException->getCode()
  72. && $this->flattenException->getMessage() === $that->flattenException->getMessage();
  73. }
  74. return $this->exceptionClass === $that->exceptionClass
  75. && $this->exceptionCode === $that->exceptionCode
  76. && $this->exceptionMessage === $that->exceptionMessage;
  77. }
  78. }