MessageEvents.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Mime\RawMessage;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class MessageEvents
  16. {
  17. private $events = [];
  18. private $transports = [];
  19. public function add(MessageEvent $event): void
  20. {
  21. $this->events[] = $event;
  22. $this->transports[$event->getTransport()] = true;
  23. }
  24. public function getTransports(): array
  25. {
  26. return array_keys($this->transports);
  27. }
  28. /**
  29. * @return MessageEvent[]
  30. */
  31. public function getEvents(string $name = null): array
  32. {
  33. if (null === $name) {
  34. return $this->events;
  35. }
  36. $events = [];
  37. foreach ($this->events as $event) {
  38. if ($name === $event->getTransport()) {
  39. $events[] = $event;
  40. }
  41. }
  42. return $events;
  43. }
  44. /**
  45. * @return RawMessage[]
  46. */
  47. public function getMessages(string $name = null): array
  48. {
  49. $events = $this->getEvents($name);
  50. $messages = [];
  51. foreach ($events as $event) {
  52. $messages[] = $event->getMessage();
  53. }
  54. return $messages;
  55. }
  56. }