ChatChannel.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Channel;
  11. use Symfony\Component\Notifier\Exception\LogicException;
  12. use Symfony\Component\Notifier\Message\ChatMessage;
  13. use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
  14. use Symfony\Component\Notifier\Notification\Notification;
  15. use Symfony\Component\Notifier\Recipient\Recipient;
  16. /**
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. *
  19. * @experimental in 5.1
  20. */
  21. class ChatChannel extends AbstractChannel
  22. {
  23. public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
  24. {
  25. if (null === $transportName) {
  26. throw new LogicException('A Chat notification must have a transport defined.');
  27. }
  28. $message = null;
  29. if ($notification instanceof ChatNotificationInterface) {
  30. $message = $notification->asChatMessage($recipient, $transportName);
  31. }
  32. if (null === $message) {
  33. $message = ChatMessage::fromNotification($notification);
  34. }
  35. $message->transport($transportName);
  36. if (null === $this->bus) {
  37. $this->transport->send($message);
  38. } else {
  39. $this->bus->dispatch($message);
  40. }
  41. }
  42. public function supports(Notification $notification, Recipient $recipient): bool
  43. {
  44. return true;
  45. }
  46. }