NotificationEvents.php 1.5 KB

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