MessageEvent.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 bool $rejected = false;
  24. /** @var StampInterface[] */
  25. private array $stamps = [];
  26. public function __construct(
  27. private RawMessage $message,
  28. private Envelope $envelope,
  29. private string $transport,
  30. private bool $queued = false,
  31. ) {
  32. }
  33. public function getMessage(): RawMessage
  34. {
  35. return $this->message;
  36. }
  37. public function setMessage(RawMessage $message): void
  38. {
  39. $this->message = $message;
  40. }
  41. public function getEnvelope(): Envelope
  42. {
  43. return $this->envelope;
  44. }
  45. public function setEnvelope(Envelope $envelope): void
  46. {
  47. $this->envelope = $envelope;
  48. }
  49. public function getTransport(): string
  50. {
  51. return $this->transport;
  52. }
  53. public function isQueued(): bool
  54. {
  55. return $this->queued;
  56. }
  57. public function isRejected(): bool
  58. {
  59. return $this->rejected;
  60. }
  61. public function reject(): void
  62. {
  63. $this->rejected = true;
  64. $this->stopPropagation();
  65. }
  66. public function addStamp(StampInterface $stamp): void
  67. {
  68. if (!$this->queued) {
  69. throw new LogicException(\sprintf('Cannot call "%s()" on a message that is not meant to be queued.', __METHOD__));
  70. }
  71. $this->stamps[] = $stamp;
  72. }
  73. /**
  74. * @return StampInterface[]
  75. */
  76. public function getStamps(): array
  77. {
  78. if (!$this->queued) {
  79. throw new LogicException(\sprintf('Cannot call "%s()" on a message that is not meant to be queued.', __METHOD__));
  80. }
  81. return $this->stamps;
  82. }
  83. }