MessageEvent.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Mailer\Event;
  11. use Symfony\Component\Mailer\Envelope;
  12. use Symfony\Component\Mailer\Exception\LogicException;
  13. use Symfony\Component\Messenger\Stamp\StampInterface;
  14. use Symfony\Component\Mime\RawMessage;
  15. use Symfony\Contracts\EventDispatcher\Event;
  16. /**
  17. * Allows the transformation of a Message and the Envelope before the email is sent.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. final class MessageEvent extends Event
  22. {
  23. private RawMessage $message;
  24. private Envelope $envelope;
  25. private string $transport;
  26. private bool $queued;
  27. private bool $rejected = false;
  28. /** @var StampInterface[] */
  29. private array $stamps = [];
  30. public function __construct(RawMessage $message, Envelope $envelope, string $transport, bool $queued = false)
  31. {
  32. $this->message = $message;
  33. $this->envelope = $envelope;
  34. $this->transport = $transport;
  35. $this->queued = $queued;
  36. }
  37. public function getMessage(): RawMessage
  38. {
  39. return $this->message;
  40. }
  41. public function setMessage(RawMessage $message): void
  42. {
  43. $this->message = $message;
  44. }
  45. public function getEnvelope(): Envelope
  46. {
  47. return $this->envelope;
  48. }
  49. public function setEnvelope(Envelope $envelope): void
  50. {
  51. $this->envelope = $envelope;
  52. }
  53. public function getTransport(): string
  54. {
  55. return $this->transport;
  56. }
  57. public function isQueued(): bool
  58. {
  59. return $this->queued;
  60. }
  61. public function isRejected(): bool
  62. {
  63. return $this->rejected;
  64. }
  65. public function reject(): void
  66. {
  67. $this->rejected = true;
  68. $this->stopPropagation();
  69. }
  70. public function addStamp(StampInterface $stamp): void
  71. {
  72. if (!$this->queued) {
  73. throw new LogicException(sprintf('Cannot call "%s()" on a message that is not meant to be queued.', __METHOD__));
  74. }
  75. $this->stamps[] = $stamp;
  76. }
  77. /**
  78. * @return StampInterface[]
  79. */
  80. public function getStamps(): array
  81. {
  82. if (!$this->queued) {
  83. throw new LogicException(sprintf('Cannot call "%s()" on a message that is not meant to be queued.', __METHOD__));
  84. }
  85. return $this->stamps;
  86. }
  87. }