SmsChannel.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\Message\SmsMessage;
  12. use Symfony\Component\Notifier\Notification\Notification;
  13. use Symfony\Component\Notifier\Notification\SmsNotificationInterface;
  14. use Symfony\Component\Notifier\Recipient\Recipient;
  15. use Symfony\Component\Notifier\Recipient\SmsRecipientInterface;
  16. /**
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. *
  19. * @experimental in 5.1
  20. */
  21. class SmsChannel extends AbstractChannel
  22. {
  23. public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
  24. {
  25. $message = null;
  26. if ($notification instanceof SmsNotificationInterface) {
  27. $message = $notification->asSmsMessage($recipient, $transportName);
  28. }
  29. if (null === $message) {
  30. $message = SmsMessage::fromNotification($notification, $recipient);
  31. }
  32. if (null !== $transportName) {
  33. $message->transport($transportName);
  34. }
  35. if (null === $this->bus) {
  36. $this->transport->send($message);
  37. } else {
  38. $this->bus->dispatch($message);
  39. }
  40. }
  41. public function supports(Notification $notification, Recipient $recipient): bool
  42. {
  43. return $recipient instanceof SmsRecipientInterface && '' !== $recipient->getPhone();
  44. }
  45. }